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

2017-08-12 09:45:15 +08:00
 roustar31

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

4098 次点击
所在节点    问与答
24 条回复
Binarization
2017-08-12 09:58:37 +08:00
example:

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

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

一般是 www 可以 CNAME 到主域名

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/382361

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX