当我调试Visual Studio项目使用Chrome浏览器尝试重定向到https相当于我的web地址。我没有在web项目中启用SSL,并且开始URL是http URL。当我使用FireFox或IE进行调试时,我没有这个问题。

我重新安装了Chrome浏览器,解决了这个问题一天。没有下载任何插件的问题再次发生了第二天。

什么是使Chrome重定向localhost到https?

网络巡检显示: 请求URL:数据:text / html, chromewebdata 请求头 显示临时标题 User-Agent:Mozilla/5.0 (Windows NT 6.3;WOW64) AppleWebKit/537.36 (KHTML,像Gecko) Chrome/36.0.1985.143 Safari/537.36

这些选项卡中没有预览和响应数据。


当前回答

对于像我这样在本地主机上运行Node.js express服务器的人来说,我有这段代码可以将http重定向到https:

const server = express() 
  .use((req, res, next) => {
    if (req.headers['x-forwarded-proto'] != 'https') {
      res.redirect('https://' + req.headers.host + req.url)
    } else {
      next()
    }
  })

你必须确保它不会重定向localhost:

const server = express() 
  .use((req, res, next) => {
    if (req.headers['x-forwarded-proto'] != 'https' && req.headers['host'].indexOf("localhost") == -1) {
      res.redirect('https://' + req.headers.host + req.url)
    } else {
      next()
    }
  })

其他回答

对于像我这样在本地主机上运行Node.js express服务器的人来说,我有这段代码可以将http重定向到https:

const server = express() 
  .use((req, res, next) => {
    if (req.headers['x-forwarded-proto'] != 'https') {
      res.redirect('https://' + req.headers.host + req.url)
    } else {
      next()
    }
  })

你必须确保它不会重定向localhost:

const server = express() 
  .use((req, res, next) => {
    if (req.headers['x-forwarded-proto'] != 'https' && req.headers['host'].indexOf("localhost") == -1) {
      res.redirect('https://' + req.headers.host + req.url)
    } else {
      next()
    }
  })

对我来说,下面的操作在Chrome 90中是有效的。我的应用程序在localhost:3000上打开了一个本地webpack服务器,自动重定向到HTTPS,我得到了ERR_SSL_PROTOCOL_ERROR。

我点击URL旁边的小信息图标,从菜单下拉菜单打开网站设置。在列表中,“不安全”内容被设置为“阻止”(默认值)。

我把这个改为允许,只是重新加载http版本,它加载正常。

希望这能帮助到人们。

打开Chrome开发人员工具->进入网络->选择“禁用缓存->重新加载”

Chrome 63(自2017年12月发布)将强制所有以。dev(和。foo)结尾的域通过预加载的HTTP严格传输安全(HSTS)头重定向到HTTPS。你可以在这里找到更多信息。

尝试了上面提到的所有方法(浏览器首选项、hsts等),但都不适合我。

我通过在主机别名后面添加一个.localhost来解决这个问题。在我的例子中,这个文件在windows下:%windir%\system32\drivers\etc\hosts

是这样的:

127.0.0.1    myproject.localhost
127.0.0.1    dev.project.localhost