node.js - Node+Express+NGINX 应用程序返回 localhost 而不是域

标签 node.js express nginx

我有一个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/

相关文章:

php - 如何设置 Nginx URI 以修复重定向到指定位置的空 URI

javascript - 尽管设置了 header ,但 CORS 不起作用

node.js - Heroku node_modules 未找到

javascript - 在 promise 中运行同步功能

javascript - Node 服务器接收文件上传作为字符串 : '[object ArrayBuffer]'

javascript - 使用 require 设置原型(prototype)自执行函数时浏览器上下文

javascript - 快速路由触发 : "Can' t set headers after they are sent.“错误

nginx - 307 临时重定向不起作用 nginx 1.18

c++ - Node.js 和 C/C++ 集成 : how to properly implement callbacks?

javascript - 如何配置安全性以允许仅通过 nodejs 中的身份验证访问 swagger url