我正在尝试识别我的 Express 应用程序是否通过 https 协议(protocol)提供服务。
使用nginx处理认证和加密(在同一台机器上),并转发请求,req.protocol
计算为http
甚至当使用 https 并且工作正常时。
我已经尝试了以下两种方法(分别):
app.set('trust proxy', 'loopback');
和
app.enable('trust proxy');
然而 req.protocol
仍然报告 http
。
什么给了?
这是req.header
:
{ 'x-real-ip': '196.38.239.10',
'x-forwarded-for': '196.38.239.10',
host: 'idwork.co',
'x-nginx-proxy': 'true',
connection: 'close',
'content-length': '0',
'cache-control': 'no-cache',
origin: 'file://',
'content-type': 'application/json',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Postman/4.3.2 Chrome/47.0.2526.73 Electron/0.36.2 Safari/537.36',
'postman-token': 'redacted',
accept: '*/*',
'accept-encoding': 'gzip, deflate',
'accept-language': 'en-US' }
这是我的相关(?)nginx 转发规则:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass 127.0.0.1:1234;
proxy_redirect off;
}
最佳答案
我需要将它添加到我的 nginx block 中:
proxy_set_header X-Forwarded-Proto https;
🙈
关于node.js - req.protocol 从不在 nginx 代理后面提供 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38028042/