Node.js app doesn't load when location path is

2019-08-23 09:51发布

问题:

For some reason, my Node.js app running on 127.0.0.1:8081 requires /.

If I start with this, BOTH / and /projectb are able to load the page.

server {
        listen 80;

        location / {
                proxy_pass http://127.0.0.1:8081;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

        location /projectb {
                proxy_pass http://127.0.0.1:8081/;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

When I change it to this, /projectb is no longer able to fully load the page. When I look at the network tab, some (but not all) requests fail, for example some images, .js, some wrong endpoints.

server {
        listen 80;

        location /somethingelse {
                proxy_pass http://127.0.0.1:8080/;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

        location /projectb {  # I'd expect this to still work.
                proxy_pass http://127.0.0.1:8081/;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

To summarize, it works when I hit the port directly 123.456.78.910:8081, and when I have BOTH / and /projectb proxy to http://127.0.0.1:8081/, and when I proxy to / by itself, but not when I proxy to /projectb by itself.

Why might this be the case?

回答1:

If location /projectb doesn't work without location / then it means that location /projectb is not configured properly. Have you tried removing location /projectb to see if it works with just location /?

What endpoint your nginx should proxy requests to? Is it http://127.0.0.1:8081/ or http://127.0.0.1:8081/projectb? I have a feeling that it's http://127.0.0.1:8081/projectb.

Please try to remove the trailing slash from proxy_pass http://127.0.0.1:8081/; line in the second snippet.



标签: node.js nginx