NGINX忽略服务器块中的上游

这是我的/etc/nginx/nginx.conf文件的样子(按照nginx -T ):

 # configuration file /etc/nginx/nginx.conf: # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } } 

这是/etc/nginx/conf.d/sv1.conf的内容:

 upstream sv1 { server unix:/var/www/app/app1/app1.sock; } server { listen 80; location /app1 { uwsgi_pass sv1; include /var/www/app/app1/uwsgi_params; } location /app1-static { alias /var/www/app/app1/static/; } location /app1-media { alias /var/www/app/app1/media/; } } 

这是/etc/nginx/conf.d/sv2.conf的内容:

 upstream sv2 { server unix:/var/www/app/app2/app2.sock; } server { listen 80; location /app2 { uwsgi_pass sv2; include /var/www/app/app2/uwsgi_params; } location /app2-static { alias /var/www/app/app2/static/; } location /app2-media { alias /var/www/app/app2/media/; } } 

但是,当我在<my-ip-address>/app1<my-ip-address>/app2上访问任何应用程序时,出现404错误,并且日志中显示以下内容:

 "/usr/share/nginx/html/app1/index.html" is not found (2: No such file or directory) 

根据这个数字app1/index.html显然是在第一个服务器的root指令定义的/usr/share/nginx/html目录下寻找一个app1/index.html文件,尽pipe其他服务器的位置指令更好地匹配了请求的url。 .com社区教程:

当试图确定要发送请求的服务器块时,Nginx将首先尝试根据listen指令的特殊性来决定[…]理解Nginx将只在需要区分时才评估server_name指令在侦听指令中与特定级别相匹配的服务器块之间

根据uWSGI一切工作正常,两个(Django)项目在其自己的debugging服务器(通过manage.py runserver )独立运行,

为什么NGINX忽略我的上游指令? 此外,如果我只是在默认服务器块下包含listen指令,它们工作的很好,但是我需要它们在自己的服务器块上,因为我必须将不同的子域redirect到每个应用程序。

您的虚拟主机中没有指定server_name 。 因此,nginx会select默认的虚拟主机,在主要的nginxconfiguration中使用listen 80 default_server指令指定。

您需要像这样指定虚拟主机:

 upstream sv1 { server unix:/var/www/app/app1/app1.sock; } server { listen 80; server_name app1.example.com; location /app1 { uwsgi_pass sv1; include /var/www/app/app1/uwsgi_params; } location /app1-static { alias /var/www/app/app1/static/; } location /app1-media { alias /var/www/app/app1/media/; } } 

然后您需要使用域名访问应用程序。 如果您没有设置域名的DNS条目,则需要在用于访问应用程序的计算机上编辑/etc/hostsC:\Windows\SYSTEM32\drivers\etc\hosts文件。