How configure nginx for rails app as subdomain?

情到浓时终转凉″ 提交于 2019-12-10 12:25:49

问题


I developed rails app and it's working on domain mpm.head-system.com On my VPS the app is located in /home/mobile_market path.

This is nginx.conf:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

        server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/xml text/css text/comma-separated-values;
        upstream app_server { 
        server 127.0.0.1:8080 fail_timeout=0;
    }

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

This is sites-enabled/default config:

server {
    listen   80;
    root /home/mobile_market/public/;
    server_name mpm.head-system.com;
    index index.htm index.html;

    location / {
        try_files $uri/index.html $uri.html $uri @app;
    }

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
        try_files $uri @app;
    }

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
  }

}

Everything works fine, and now I have a new rails app (redmine) on the same VPS.
Now I setup redmine on the same VPS and it works on the 3000 port - mpm.head-system.com:3000

How can I change nginx.conf to setup my redmine app on subdomain like - redmine.head-system.com ? How connect application, which running on other port, as subdomain? (because in /etc/hosts I can set IP without port only). I know that I need to use proxy_pass and virtual host, but I don't know how :(

Please, help...


回答1:


Add new confing file to /etc/nginx/sites-enabled: redmine.config

server {
    listen   80;
    root /home/redmine/;
    server_name redmine.head-system.com;
    index index.htm index.html;

    location / {
        try_files $uri/index.html $uri.html $uri @app;
    }

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
        try_files $uri @app;
    }

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://localhost:3000;
  }

}


来源:https://stackoverflow.com/questions/24898518/how-configure-nginx-for-rails-app-as-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!