我有一个node+express网站在我的ubuntu服务器上运行在端口10000上,nginx在端口80上使用proxy_pass到localhost:10000。我的问题是,当我在 Express 中请求主机时,它返回 localhost 而不是我的域名。我使用 nginx 代理,这样我就可以管理计算机上指向不同应用程序的多个域。
有没有办法在我的node+express服务器上保留原始主机名,同时仍然在nginx中使用proxy_pass?
最佳答案
默认情况下,nginx
将上游请求中的 Host header 设置为 proxy_pass
语句中出现的主机名。在本例中localhost
。
您需要使用 proxy_set_header
指令显式设置主机 header 。
例如,我总是设置这个组:
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 "";
参见this document了解更多。
关于node.js - Node+Express+NGINX 应用程序返回 localhost 而不是域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43478436/