node.js如何配置当用户输入http时自动跳到https请求?
发布于 6 年前 作者 maitian2333 3985 次浏览 来自 问答

在配置https的时候,怎么做到当用户在浏览器上输入http请求时自动跳到https,比如,用户输入:http://www.baidu.com,然后浏览器会变成https://www.baidu.com呢? 我自己配置的https只能通过https访问,输入http是无法访问?请问怎么解决?

5 回复

http 需要 监听80端口,然后301跳转到https (https 默认443 端口)

这跟node没关系, nginx一搞就好

server {
    listen 80;
    listen 443;
    server_name www.xxxxxx.com;
    ssl on;
    ssl_certificate xxxxxxxxxxxx.crt;
    ssl_certificate_key xxxxxxxxxxxx.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
    ssl_prefer_server_ciphers on;

    if ($scheme = http) {
        return 301 https://$host$request_uri;
    }

    //............
}

前端跳转 =。= window.location.href ,我这样干过,瞬间访问量翻倍。

回到顶部