NGINX 提供所有静态文件并将其他文件转发给代理而不/提供 403?

NGINX Serve all static files and forward others to proxy without / giving 403?

我正在尝试配置 nginx 服务器以充当节点应用程序的代理。我想要的是让 nginx 提供任何静态文件,并将所有其他路径转发到 Node.js 应用程序(包括 / 路径)。

我看过以下 question/answers:How to serve all existing static files directly with NGINX, but proxy the rest to a backend server.

按照指示,我已按如下方式配置我的站点配置:

server {
  server_name subdom.domain.com;
  listen 80;
  location / {
    root /var/www/application/public_html;
    try_files $uri $uri/ @application;
  }
  location @application {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://localhost:3000;
  }
}

然而,当浏览到 subdom.domain.com/ 时,我收到了 403 禁止访问。我猜这是因为 nginx 找不到默认文档,但我希望将默认文档发送到代理,知道如何做到这一点吗?

这应该可以完成工作:

  server {
    server_name subdom.domain.com;
    listen 80;

    location / {
        try_files $uri;

        proxy_set_header X - Real - IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
        proxy_pass http: //localhost:3000;

    }
  }

每个 URL 的简要说明它会找到一个文件,它将为该文件提供服务 (try_file),对于其他所有内容,它都会尝试代理