【问题标题】:How to find out if request is HTTPS or HTTP in express.js with apache reversed proxy?如何使用 apache 反向代理确定 express.js 中的请求是 HTTPS 还是 HTTP?
【发布时间】:2017-06-20 20:49:52
【问题描述】:

我正在尝试验证请求以查看它是 HTTPS 还是 HTTP。我的 express 应用程序在端口 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

【问题讨论】:

    标签: node.js apache http express https


    【解决方案1】:

    您可能需要明确告诉 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')
    

    【讨论】:

    • 我试过你的方法。但是我从 apache2 收到此错误:无效命令“RequestHeader”,可能拼写错误或由未包含在服务中的模块定义
    • 我必须使用命令 a2enmod headers 启用 apache 中的标头,现在 req.secure 在 https 调用上返回 true。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2012-05-08
    • 2020-11-24
    • 2018-02-22
    • 2023-01-21
    相关资源
    最近更新 更多