Nginx配置仅匹配基本路径,否则返回404。为什么?

时间:2019-01-23 23:09:32

标签: nginx digital-ocean

我有一个DigitalOcean Droplet设置,可以同时处理3个网站。一切都与基本路径完全相同(例如:www.johndoe.com/),但是我在斜杠后添加的所有内容都会返回404页面(例如:www.johndoe.com/about)。

这是我3个网站的配置文件之一(由certbot生成):

    server {
        listen 443 ssl;

        root /usr/share/nginx/holidayhomes7;

        server_name holidayhomes7.com www.holidayhomes7.com;

        location / {
                try_files $uri $uri/ =404;
                proxy_pass http://localhost:8080;
        }

        # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/holidayhomes7.com/fullchain.pem; 

        # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/holidayhomes7.com/privkey.pem; 
    }

    server {
        if ($host = www.holidayhomes7.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot


        if ($host = holidayhomes7.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot

        listen 80;
        listen [::]:80;

        server_name holidayhomes7.com www.holidayhomes7.com;
        return 404; # managed by Certbot
    }

我尝试更换了

try_files $uri $uri/ =404;

作者

try_files $uri /path_to_html_file;

无济于事。

顺便说一下,这3个网站都是节点应用程序,它们在我的机器的localhost上都可以正常运行。

我不知道为什么在有关NGINX的所有事情中都会出现这种情况。非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您的try_files正在接管,并且从未达到proxy_pass部分。

在示例的第一个server块中尝试以下操作:

location / {
    try_files $uri $uri/ @app;
}

location @app {
    # depending on what OS you use, and how you've installed Nginx
    # there may be an include here to do as well, such as:
    # include proxy_params;
    proxy_pass http://localhost:8080;
}

这应该尝试在服务器上找到与URI相匹配的文件,如果找不到,请通过proxy_pass将请求传递给NodeJS。