2015-06-01 47 views
0

我知道这是一个很常见的问题,但我一直在这个时候挣扎天,一个奇怪的一个:无法服务于多个电源轨的应用与Nginx的+独角兽

我想为两条轨道上的4个应用程序相同的VPS(Ubuntu 14.04)。我跟着this guide为一个应用程序成功。我的app1工作正常。但不是app2。

该错误是这一个(/var/log/nginx/error.log):的 “/ SRV/APP1 /公共/ 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; 
     } 
    } 

网站可用/ APP 2

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的公用文件夹寻找APP 2?

回答

0

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

移动位置块/ APP2和unicorn_app2进入位点可用/ APP1

和删除位点可用/ APP2

answer显示了一个例子。

+0

而且您应该为2个资产目录拥有2个不同的位置块 – tangrufus

相关问题