如何使用apache反向代理查找express.js中的请求是HTTPS还是HTTP?

时间:2017-06-20 20:49:53

标签: node.js apache http express https

我正在尝试验证请求以查看它是HTTPS还是HTTP。我的快速应用程序在端口1337上运行.Apache作为反向代理,在端口80上没有ssl证书,在端口443上用于https请求。

我尝试过以下方法:

console.log("Is secure HTTPS: "+req.secure);
console.log("Is encrypted: "+req.connection.encrypted);
console.log("Proxy "+req.header('x-forwarded-proto'));
console.log(req.headers['x-forwarded-proto'])

使用https从chrome请求时,会打印:

Is secure HTTPS: false
Is encrypted: undefined
Proxy undefined
undefined

1 个答案:

答案 0 :(得分:0)

您可能需要明确告诉Apache设置x-forwarded-proto

<VirtualHost *:443>
    RequestHeader set X-Forwarded-Proto "https"
    ...
</VirtualHost>

然后您可以使用req.headers['x-forwarded'proto']。只有在您设置了“信任代理”时,Express才会设置req.secure字段,例如

app.set('trust proxy', 'loopback')