个人博客Nginx强制跳转Https配置

今日因需要将自己的博客域名加入https支持,并把之前的HTTP访问强制跳转到HTTPS。

1、nginx支持https

如果起初nginx编译时没有添加ssl支持,需要对nginx重新编译,增加http_ssl_module模块

1
2
3
4
5
6
7
1.配置
./configure
--prefix=/usr/local/nginx
--with-http_stub_status_module
--with-http_ssl_module
2.编译安装
make && make install

2、配置https域及SSL证书

没有证书的可以考虑腾讯云申请免费证书,但仅限单域名,申请地址:https://cloud.tencent.com/product/ssl,
申请通过后,下载nginx专用证书。
以下为https证书相关配置

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 443 ssl;
server_name www.www.codegrok.top;
ssl_certificate ../ssl/***.crt;
ssl_certificate_key ../ssl/***.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
location / {
root html;
index index.html index.htm;
}
}

配置好后,测试https是否可以正常访问。

3、http强制跳转https

此处因http下无其他信息,单纯配置了跳转

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name www.www.codegrok.top;
#方式一
return 301 https://$server_name$request_uri;
#方式二
#rewrite ^/(.*)$ https://www.www.codegrok.top/$1 permanent;
#方式三
#rewrite ^ https://www.www.codegrok.top$request_uri? permanent;
}

博客内容较简单,仅为个人配置记录,有问题可留言咨询。有更好的方案,也请多多指教。