NGINX 忽略服务器块中的上游

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下的文件,尽管根据此,其他两个服务器的位置指令都与请求的 url 更匹配/usr/share/nginx/htmlrootdigitalocean.com社区教程:

当尝试确定向哪个服务器块发送请求时,Nginx 将首先尝试根据 listen 指令的特殊性来决定 [...] 重要的是要理解,Nginx 仅在需要区分与 listen 指令中的相同特殊性级别匹配的服务器块时才会评估 server_name 指令

根据 uWSGI 一切正常,并且两个(Django)项目在各自的调试服务器下独立运行良好(通过manage.py runserver

为什么 NGINX 会忽略我的上游指令?此外,如果我仅在默认服务器块下包含 listen 指令,它们可以正常工作,但我需要将它们放在自己的服务器块上,因为我必须将不同的子域重定向到每个应用程序。

答案1

您没有server_name指定任何虚拟主机。因此,nginx 选择默认虚拟主机,该虚拟主机listen 80 default_server在主 nginx 配置中使用指令指定。

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

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/hosts或文件。C:\Windows\SYSTEM32\drivers\etc\hosts

相关内容