roustar31
V2EX  ›  问与答

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

  •  
  •   roustar31 · Aug 12, 2017 · 5093 views
    This topic created in 3373 days ago, the information mentioned may be changed or developed.

    前端: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 站点提交也提交不了。 请问有什么可行的解决方案?

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

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

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

    一般是 www 可以 CNAME 到主域名
    matsuz
    matsuz
    Aug 12, 2017 via Android
    21
    server 段加个 if 判断,如果域名不是 www 就 301 跳转
    DJason
    DJason
    Aug 13, 2017 via iPhone
    22
    你说的是这样的吗 aitbx.com
    roustar31
    roustar31
    OP
    Aug 15, 2017
    23
    @DJason 不是
    roustar31
    roustar31
    OP
    Aug 15, 2017
    24
    @Kilerd 有五毛钱的关系
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4873 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 65ms · UTC 08:54 · PVG 16:54 · LAX 00:54 · JFK 03:54
    ♥ Do have faith in what you're doing.