如何使用我的 nodejs-express 应用程序处理被重定向为 https 的 http 请求?

How to handle http requests which are getting redirected as https using my nodejs-express app?

我正在网站中注入一些脚本标签,源代码如 http:localhost:3000/css/my-page-css.css 。虽然它适用于几乎所有网站,但有一个特定网站以某种方式将我所有的 http 请求发送为 https。我该如何处理这种情况?

我还在我的 nodejs 应用程序上配置了一个 https 服务器,它监听端口 8443,http 监听 3000。但是,当我注入我的脚本标签时,它们有指向端口 3000 的 src URLS。因此,即使我在我的 nodejs 应用程序上配置了 https,它也不会工作,因为它会监听不同的端口。

您正在使用 HTTP 严格传输安全 (HSTS)

使用 the securityheader.com website on your URL 或 Chrome 开发者工具,我们看到以下 HTTP Header 被您的网站发回:

Strict-Transport-Security   max-age=7889238

此 HTTP Header 将在您的网络服务器中配置,并且是您的网络服务器告诉浏览器的一种方式 "For the next 7889238 seconds only use HTTPS on this domain. If someone tries to use HTTP (either by typing or by clicking on a link) then automatically switch HTTP to HTTPS before you send it on to the server."

这是一项安全功能,因为当前默认(如果未明确给出方案)是 HTTP。这允许网站所有者切换默认设置,甚至可以防止它被切换回来。

HSTS 是在域级别设置的,不可能为一个端口(例如 443)打开它但不能为另一个端口(例如 3000)打开它 - 它要么为该域打开,要么关闭。

如果您真的想使用 HTTP,那么您需要删除此 header 并从您的浏览器中删除此 header 的记忆值。虽然 chrome 允许您通过在 URL 中键入 chrome://net-internals/#hsts 并使用删除选项来执行此操作,但最简单的方法是将最大年龄从 7889238 更改为 0,然后再次加载网站。然后完全删除header。

这对于像 localhost 这样的网站来说尤其烦人,在这些网站上您代理请求并无意中将其设置为该虚拟主机名。您应该查看您的节点代理服务器是否允许您剥离该 HTTP header。 Some might say it would be better if browser makers ignored HSTS for localhost, however I think it would be better if developers just stopped fighting HTTPS and used that even for development environments using a self-signed certificate that is added to your local trust store. This was you can avoid problems like mixed content, and also use features that are HTTPS only (including Brotli, HTTP/2, Geo Location...etc.) while developing (though some browsers like Chrome still allow these on http://localhost).

或者 set up a local DNS alias for each of your dev sites 并根据相关网站的需要使用或不使用 HTTPS。