Nginx 配置只匹配基本路径,否则为 returns 404。为什么?

Nginx configuration only matches base paths, returns 404 otherwise. Why?

我有一个 DigitalOcean Droplet 设置可以同时处理 3 个网站。对于基本路径(例如:www.johndoe.com/),一切都完全按照它应该的方式工作,但我在斜杠 returns 之后添加的任何内容都是 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 个网站都是节点应用程序,它们在我机器的本地主机上都运行良好。

我不知道为什么会这样,因为我对 NGINX 的一切都是一个完全的菜鸟。非常感谢您的帮助!

我认为你的 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