V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
roustar31
V2EX  ›  问与答

开启 HSTS 后,如何让顶级域名 https 跳转 www?

  •  
  •   roustar31 · 2017-08-12 09:45:15 +08:00 · 4049 次点击
    这是一个创建于 2448 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前端:Nginx,开启了 HTTPS。配置如下: server { listen 80; listen 443 ssl http2; ssl_certificate /usr/local/nginx/conf/ssl/www.test.com.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/www.test.com.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_timeout 10m; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_buffer_size 1400; add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"; ssl_stapling on; ssl_stapling_verify on; server_name www.test.com test.com; access_log off; index index.html index.htm index.php; root /data/wwwroot/www.test.com; if ($ssl_protocol = "") { return 301 https://$host$request_uri; } if ($host != www.test.com) { return 301 $scheme://www.test.com$request_uri; } include /usr/local/nginx/conf/rewrite/discuz.conf; location ~ [^/].php(/|$) { #fastcgi_pass remote_php_ip:9000; fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } location ~ ..(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ ..(js|css)?$ { expires 7d; access_log off; } location ~ /.ht { deny all; } }

    现在的问题是: 浏览器输入 test.com 可以直接转到 https://www.test.com ;输入 https://test.com 提示无法连接( ERR_CONNECTION_REFUSED ) 去谷歌的那个 HSTS 站点提交也提交不了。 请问有什么可行的解决方案?

    第 1 条附言  ·  2017-08-12 11:27:28 +08:00
    感谢大家的回复,解决方案是这样的:
    顶级域名解析到服务器。
    nginx 的 80 配置 301 到 https,注意不要带 www.否则无法提交到 https://hstspreload.org
    24 条回复    2017-08-15 08:44:02 +08:00
    Binarization
        1
    Binarization  
       2017-08-12 09:58:37 +08:00 via Android
    example:

    server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
    }
    Ryans
        2
    Ryans  
       2017-08-12 09:59:53 +08:00
    不是将顶级域名 CNAME 到 www ?
    ETiV
        3
    ETiV  
       2017-08-12 10:39:45 +08:00 via iPhone
    答案就是 1 楼,但 $server_name is 前记得加 『 www.』
    roustar31
        4
    roustar31  
    OP
       2017-08-12 10:48:10 +08:00
    @Binarization
    没用,一样输入 https://test.com 提示无法连接
    roustar31
        5
    roustar31  
    OP
       2017-08-12 10:48:29 +08:00
    @ETiV 加了 www.的,没用
    msg7086
        6
    msg7086  
       2017-08-12 10:52:01 +08:00   ❤️ 1
    首先。

    你裸域解析做成啥样了?
    cnZary
        7
    cnZary  
       2017-08-12 10:52:15 +08:00   ❤️ 1
    你 dns 解析过去没?
    roustar31
        8
    roustar31  
    OP
       2017-08-12 10:54:55 +08:00
    @Ryans 顶级域名是 301 到了 www 的
    roustar31
        9
    roustar31  
    OP
       2017-08-12 10:56:23 +08:00
    @msg7086 顶级域名 301 跳到 https://www.test.com 。是不是应该改成 http 的?或者 A 指向服务器 IP ?
    imlonghao673
        10
    imlonghao673  
       2017-08-12 11:07:16 +08:00 via Android   ❤️ 1
    我大概知道了
    楼主使用了 DNS 平台提供的 URL 转发,所以不支持 HTTPS
    :)
    insoxin
        11
    insoxin  
       2017-08-12 11:08:28 +08:00 via Android
    @roustar31 伪静态,源码 301 好多
    imlonghao673
        12
    imlonghao673  
       2017-08-12 11:13:33 +08:00 via Android   ❤️ 1
    解决方案:
    去 DNS 那里使用 A 记录解析过去你自己的服务器
    roustar31
        13
    roustar31  
    OP
       2017-08-12 11:18:35 +08:00
    @imlonghao673 本地 hosts 指定了 test.com 的 IP 为服务器,发现马上就跳转到 www 的 https 了,的确是 dns 301 的问题。
    感谢大家,问题已经解决
    ErnestChan
        14
    ErnestChan  
       2017-08-12 11:23:56 +08:00
    cloudxns 301
    roustar31
        15
    roustar31  
    OP
       2017-08-12 11:25:36 +08:00
    @ETiV 如果要提交谷歌的 HSTS LIST,不能要 www.否则会提示不是跳到顶级域名的 https
    roustar31
        16
    roustar31  
    OP
       2017-08-12 11:26:11 +08:00
    roustar31
        17
    roustar31  
    OP
       2017-08-12 11:29:41 +08:00
    @ErnestChan 我就是这家的 dns。。。
    CRVV
        18
    CRVV  
       2017-08-12 13:18:56 +08:00
    @Ryans
    把顶级域名设成 CNAME 会有奇奇怪怪的问题,一般不这么干
    https://serverfault.com/questions/613829
    Kilerd
        19
    Kilerd  
       2017-08-12 14:14:38 +08:00 via iPhone
    这跟 HSTS 有半毛钱关系吗?
    Ryans
        20
    Ryans  
       2017-08-12 14:37:50 +08:00
    @CRVV ok,记混了。

    一般是 www 可以 CNAME 到主域名
    matsuz
        21
    matsuz  
       2017-08-12 19:05:17 +08:00 via Android
    server 段加个 if 判断,如果域名不是 www 就 301 跳转
    DJason
        22
    DJason  
       2017-08-13 02:38:23 +08:00 via iPhone
    你说的是这样的吗 aitbx.com
    roustar31
        23
    roustar31  
    OP
       2017-08-15 08:27:09 +08:00
    @DJason 不是
    roustar31
        24
    roustar31  
    OP
       2017-08-15 08:44:02 +08:00
    @Kilerd 有五毛钱的关系
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1044 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:17 · PVG 06:17 · LAX 15:17 · JFK 18:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.