配置好了https,并开启了hsts,如何首次访问也强制走https?

配置好了https,但每次第一次访问走的是http。也即如果一个用户从来没有以 HTTPS 方式访问过我们的网站呢,那显然就没有机会得到 HSTS 响应头,从而还是有可能以 HTTP 的方式进行首次访问。

阅读 9.7k
4 个回答

谷歌在浏览器安全方面总是走在前面,因此它维护了一个预载入列表 (请先科学上网)给 Chrome 使用,这个列表会硬编码到 Chrome 浏览器中。后来,Firefox、Safari、 IE 11 和 Edge 也加入了。所以,各大浏览器都支持同一个列表了。这样以来加入到这个列表当中的网站就可以确保在任何情况下都走https了,包括首次。
twitter.com
如图,查询twitter.com加入了这个列表,所以在地址栏输入twitter.com的时候,即便没有访问过,第一次浏览器也强制走了https,而不是server端强制跳转。

详细了解可以参照此篇文章(引自linux.cn)

这个不行,你没法要求用户第一次就一定用https来访问,

配置conf配置文件,将所有80端口跳转到443,

第二种方法,HSTS

server {
    listen       80;
    server_name 你的域名;
    rewrite ^/(.*) https://你的域名/$1 permanent;
}
server
    {
        listen 80;
        listen 443 ssl;
        ssl on;
        ssl_certificate /root/t3.crt;
        ssl_certificate_key /root/t3.key;
        server_name t3.zy62.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/rootpath/;

        include other.conf;
        #error_page   404   /404.html;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        location ~ [^/]\.php(/|$)
        {
            # comment try_files $uri =404; to enable pathinfo
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            #include pathinfo.conf;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        access_log  /home/日志文件  access;
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题