【问题标题】:Node+Express+NGINX application returning localhost instead of domainNode+Express+NGINX 应用程序返回 localhost 而不是域
【发布时间】:2017-09-14 15:53:51
【问题描述】:

我有一个 node+express 网站在我的 ubuntu 服务器上运行,端口为 10000,nginx 在端口 80 上使用 proxy_pass 到 localhost:10000。我的问题是,当我以 express 方式询问主机时,它返回 localhost 而不是我的域名。我使用 nginx 代理,因此我可以管理机器上指向不同应用程序的多个域。

有没有办法在我的 node+express 服务器上保留原始主机名,同时仍在 nginx 中使用 proxy_pass?

【问题讨论】:

    标签: node.js express nginx


    【解决方案1】:

    默认情况下,nginx 将上游请求中的 Host 标头设置为出现在 proxy_pass 语句中的主机名。在这种情况下localhost

    您需要使用proxy_set_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 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-20
      • 2021-02-10
      • 2019-01-31
      • 2020-06-07
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      相关资源
      最近更新 更多