当我调试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()
    }
  })

其他回答

我也一直在这个问题上挣扎。看来HSTS只针对域名。所以如果你是在本地机器上开发,使用IP地址要容易得多。所以我从localhost切换到127.0.0.1

我在Chrome浏览器上遇到了同样的问题,我尝试使用BigJump的解决方案,但没有成功。

我通过强制硬刷新来解决这个问题,如本文所示(最初来自SuperUser的回答)。

确保你的地址栏正在使用http方案,然后执行这些步骤,可能需要几次:

打开开发人员工具面板(CTRL+SHIFT+I) 点击并按住重载图标/右键单击重载图标。 将打开一个菜单。 从菜单中选择第三个选项(“空缓存和硬加载”)

对于像我这样在本地主机上运行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()
    }
  })

这是目前最快的解决方案(17-3-2018):

关闭所有Chrome选项卡/窗口,并在命令行上运行以下内容:(或将其添加为短代码)

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors

chrome://net-internals/#hsts 

在“删除域安全策略”中输入localhost,按“删除”键。

现在转到

chrome://settings/clearBrowserData 

选中“缓存的图像和文件”复选框,然后按“清除数据”按钮。