问题
I have a node+express website running on my ubuntu server on port 10000 with nginx on port 80 using a proxy_pass to localhost:10000. My issue is that when I ask for the host in express it returns localhost instead of my domain name. I use the nginx proxy so I can manage several domains on the machine pointing to different applications.
Is there a way to keep the original host name on my node+express server while still using proxy_pass in nginx?
回答1:
By default, nginx
sets the Host header in the upstream request to the hostname appearing in the proxy_pass
statement. In this case localhost
.
You need to set the Host header explicitly using the proxy_set_header
directive.
For example, I always set this group:
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
See this document for more.
来源:https://stackoverflow.com/questions/43478436/nodeexpressnginx-application-returning-localhost-instead-of-domain