簡體   English   中英

無法使用Nginx + Unicorn服務於多個Rails應用程序

[英]Unable to serve multiple rails applications with Nginx + Unicorn

我知道這是一個非常普遍的問題,但是這次我一直在努力解決一個奇怪的問題:

我想在同一VPS(ubuntu 14.04)上提供兩個Rails 4應用程序。 我遵循本指南成功開發了一個應用程序。 我的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?

問題是您的2個Nginx服務器塊正在偵聽相同的域名。

將位置塊/ app2和unicorn_app2移到site-available / app1中

並刪除site-available / app2

答案顯示了一個示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM