无法使用Nginx + Unicorn为多个rails应用程序提供服务

时间:2015-06-01 21:25:43

标签: ruby-on-rails nginx unicorn

我知道这是一个非常普遍的问题,但这次我和一个奇怪的人一直在苦苦挣扎:

我想在同一个VPS(ubuntu 14.04)上提供两个Rails 4应用程序。我跟着this guide获得了一个成功的应用程序。我的app1工作正常。但不是app2。

错误就是这个错误(/var/log/nginx/error.log):

  

目录索引“/ srv / app1 / public / app2 /”是禁止的

一般nginx.conf

    # Run nginx as www-data.
    user www-data;
    # One worker process per CPU core is a good guideline.
    worker_processes 1;
    # The pidfile location.
    pid /var/run/nginx.pid;

    # For a single core server, 1024 is a good starting point. Use `ulimit -n` to
    # determine if your server can handle more.
    events {
        worker_connections 1024;
    }



    http {


        ##
        # Basic Settings
        ##

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

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

        ##
        # Logging Settings
        ##

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

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";
        gzip_http_version 1.1;
        gzip_proxied any;
        gzip_min_length 500;
        gzip_types text/plain text/xml text/css
            text/comma-separated-values text/javascript
            application/x-javascript application/atom+xml;

        ##
        # Unicorn Rails
        ##

        # The socket here must match the socket path that you set up in unicorn.rb.

        upstream unicorn_app2 {
            server unix:/srv/app2/tmp/unicorn.app2.sock fail_timeout=0;
        }

        upstream unicorn_app1 {
            server unix:/srv/app1/tmp/unicorn.app1.sock fail_timeout=0;
        }


        ##
        # Virtual Host Configs
        ##

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

站点可用/ APP1

    server {
        listen 80;
        server_name _
                      public.ip.of.vps; # Replace this with your site's domain.
        keepalive_timeout 300;
        client_max_body_size 4G;

        root /srv/app1/public; # Set this to the public folder location of your Rails application.

        location /app1 {
            try_files $uri @unicorn_app1;
        }

        location @unicorn_app1 {
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header Host $http_host;
                  proxy_set_header X-Forwarded_Proto $scheme;
                  proxy_redirect off;
                  # This passes requests to unicorn, as defined in /etc/nginx/nginx.conf
                  proxy_pass http://unicorn_app1;
                  proxy_read_timeout 300s;
                  proxy_send_timeout 300s;
                  auth_basic "Restricted";                                #For Basic Auth
                  auth_basic_user_file /etc/nginx/.htpasswd;  #For Basic Auth
        }

        location ~ ^/assets/ {
                  #gzip_static on; # to serve pre-gzipped version
                  expires max;
                  add_header Cache-Control public;
        }


        # You can override error pages by redirecting the requests to a file in your
        # application's public folder, if you so desire:
        error_page 500 502 503 504 /500.html;
        location = /500.html {
                  root /srv/app1/public;
        }
    }

站点可用/ APP2

    server {
        listen 80;
        server_name __
                      public.ip.of.vps; # Replace this with your site's domain.
            keepalive_timeout 300;
        client_max_body_size 4G;

        root /srv/app2/public; # Set this to the public folder location of your Rails application.

        location /app2 {
            try_files $uri @unicorn_app2;
        }

        location @unicorn_app2 {
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header Host $http_host;
                  proxy_set_header X-Forwarded_Proto $scheme;
                  proxy_redirect off;
                  # This passes requests to unicorn, as defined in /etc/nginx/nginx.conf
                  proxy_pass http://unicorn_app2;
                  proxy_read_timeout 300s;
                  proxy_send_timeout 300s;
                  auth_basic "Restricted";                                #For Basic Auth
                  auth_basic_user_file /etc/nginx/.htpasswd;  #For Basic Auth
        }

        location ~ ^/assets/ {
                  #gzip_static on; # to serve pre-gzipped version
                  expires max;
                  add_header Cache-Control public;
        }


        # You can override error pages by redirecting the requests to a file in your
        # application's public folder, if you so desire:
        error_page 500 502 503 504 /500.html;
        location = /500.html {
                  root /srv/app2/public;
        }
    }

为什么nginx在app1的公共文件夹中寻找app2?

1 个答案:

答案 0 :(得分:0)

问题是您的2个nginx服务器块正在侦听相同的域名。

将location blocks / app2和unicorn_app2移动到site-available / app1

删除site-available / app2

answer显示了一个示例。