【问题标题】:Req.protocol is always http and not httpsReq.protocol 始终是 http 而不是 https
【发布时间】:2019-12-19 09:51:52
【问题描述】:

我最近使用 Express Web 服务器将 NodeJS 应用程序从一个托管服务提供商迁移到另一个托管服务提供商。

自从切换后,我所有的 req.protocol 都以 HTTP 而不是 HTTPS 返回。我已经在新主机上配置了 SSL 证书,它正在使用 .htaccess 文件强制 SSL。

我一直在我的应用程序中启用信任代理。

app.enable('trust proxy)';

但是,我尝试禁用此功能,但并没有解决我的问题。

启用信任代理后我收到的请求标头:

{ host: '127.0.0.1:58535',
'upgrade-insecure-requests': '1',
'user-agent':
 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*


 /*;q=0.8,application/signed-exchange;v=b3',
'sec-fetch-site': 'none',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
cookie:
 '_ga=GA1.2.1627492895.1565633625; _gid=GA1.2.952960627.1565633625; 
 _hjid=aa79d062-c91e-483d-8654-e2d11b4b8707; _hjIncludedInSample=1',
'if-none-match': 'W/"7d4b-dYhPiTxJki+OCl9FjTfsJQnUkSM"',
'x-forwarded-for': 'X.X.X.X',
'x-forwarded-host': 'example.example.com',
'x-forwarded-server': 'example.example.com',
connection: 'close' }

我没有使用 NGINX。为什么我的应用没有检测到 https 作为协议?

【问题讨论】:

  • Hox 你开始你的快递应用了吗? http 还是 https 模块?
  • 我没有明确指定任何一个,只是 app.listen(port).. 所以我默认使用 http

标签: node.js express


【解决方案1】:

经过一些调试,似乎是 Apache Web 服务器的配置问题。我使用的网络主机有一些自定义配置,这使得修改 apache 配置而不破坏服务器上运行的服务变得很困难。

最后,我决定学习 Nginx 并将其实现为我的 nodeJS 应用程序的 Web 代理。使用 app.enable('信任代理)';在我的代码中允许将正确的协议传递给应用程序。

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 2019-02-27
    • 2017-05-19
    • 2019-11-09
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    相关资源
    最近更新 更多