如何从静态文件提供 / 服务,但其余的根目录则从代理(NodeJS)提供?

如何从静态文件提供 / 服务,但其余的根目录则从代理(NodeJS)提供?

我尝试了以下方法:

    # this *should* serve the root from a static index.html file
    location = / {
            try_files $uri $uri.html $uri/ /index.html;
    }

    # resources are served directly by nginx
    location /img {
    }
    location /css {
    }

    # all other URLs are served dynamically by nodejs
    location / {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Scheme $scheme;
    }

location /imglocation /css子句location /有效。

我遇到了麻烦location = /。我添加了$uri $uri.html $uri/因为tryfiles不会仅/index.html作为单一参数接受。

请求/被传递到上游,而不是由提供(静态)index.html 文件的 nginx 来应答。

如何将根目录重定向到代理服务器(NodeJS),同时从静态文件提供文字“/”?

答案1

我用另一种方法修复了它:

    # any url that is not served by a file is passed to nodejs
    location / {
            try_files $uri $uri.html $uri/ @backend;
    }

    # all other URLs are served dynamically by nodejs
    location @backend {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Scheme $scheme;
    }

相关内容