问题引入
刚刚我不是才把博客网站的Http协议升级成Https协议么, 我贴我博客的链接当然是https://www.imashimaro.com 这样啦. 但是!!! 万万没想到啊, 当我继续使用原来的http协议(www.imashimaro.com) 进行访问时, 问题出现了, 它并没有按照我预想的那样打开我的博客网站, 而是访问了nginx服务器默认的index.html.
解决问题
为了解决这个问题, 我进行了百度, 尽管有比较多的教程, 但是悲催的我还是掉进了坑, 不然也不会写这篇博文了, 写此博文引以为戒.
首先, 打开nginx配置文件
在80端口的服务里添加一句核心代码
—> rewrite ^(.*)$ https://$host$1 permanent;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| server { listen 80 default_server; listen [::]:80 default_server; server_name www.你的域名.com; root /usr/share/nginx/html; rewrite ^(.*)$ https://$host$1 permanent; include /etc/nginx/default.d/*.conf;
location / { }
error_page 404 /404.html; location = /40x.html { }
error_page 500 502 503 504 /50x.html; location = /50x.html { } }
server { listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; server_name www.你的域名.com; root /usr/share/nginx/html;
ssl_protocols TLSv1.2; ssl_certificate "path/to/server.crt"; ssl_certificate_key "/path/to/server.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
include /etc/nginx/default.d/*.conf;
location / { }
error_page 404 /404.html; location = /40x.html { }
error_page 500 502 503 504 /50x.html; location = /50x.html { } }
|
然后, 然后就没然后了, 没了!!!
想不到吧, 其实非常简单, 但是新手会经常犯些低级错误, 导致没成功 .
其实还有一步的, 就是使用nginx -s reload
命令使nginx重新加载配置文件, 使刚才修改的配置生效.
踩坑点
踩坑点一
看到别人的location里面满满当当的,我的里面却啥也没有, 就以为这个也是要配置的.
一通复制粘贴后的结果就是:
踩坑点二
之前还曾遇到过这个问题, 在浏览器地址中输入www.imashimaro.com, 虽然是把请求重定向成Https了, 但是URL却变成了https://www.imashimaro.com/www.imashimaro.com, 后来不知道咋搞的给整好了
结语
总之就是千万要细心
, 细心能为我们减少很多的工作量, 有时候仅仅一个字母的错误, 就可能会花费我们几小时的时间去寻找,排查这个错误, 实在得不偿失. 所以, 细心点吧, 小伙伴们.
