在2017年05月20日写了一篇文章记录了手动申请SSL加密证书和相关配置信息,还包括301重定向; 文章地址:https://nico.cc/archives/20/
这篇文章将记录在LNMP环境
下实现SSL加密和301重定向的过程
如果在创建虚拟主机时没有添加SSL加密证书,使用命令 lnmp ssl add
可以添加域名证书,并实现自动续期。
设置301重定向
在/usr/local/nginx/conf/vhost/
目录下找到需要配置的文件,我的配置文件如下 ,需要三个server段
0x01 第一个server段,实现了http://www.920.ai
和http://920.ai
跳转到https://nico.cc
server
{
listen 80;
listen [::]:80;
server_name www.920.ai 920.ai;
return 301 https://nico.cc$request_uri;
}
注意:$request_uri 为必须设置的参数
0x02 第二个server段,实现了https://www.920.ai
跳转到https://nico.cc
server
{
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.920.ai;
return 301 https://nico.cc$request_uri;
ssl on;
ssl_certificate /usr/local/nginx/conf/ssl/www.920.ai/fullchain.cer;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.920.ai/www.920.ai.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5"
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
}
注意:需要添加SSL相关配置信息,否则无法访问
0x03 第三个server段
server
{
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 920.ai;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.920.ai;
ssl on;
ssl_certificate /usr/local/nginx/conf/ssl/www.920.ai/fullchain.cer;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.920.ai/www.920.ai.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
include rewrite/typecho.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log /home/wwwlogs/www.920.ai.log;
}
注意:SSL配置信息与第二段相同
rewrite和return两种重定向方式的区别:
第一种:使用rewrite:rewrite ^(.*) https://nico.cc$1 permanent;
第二种:使用return: return 301 https://nico.cc$request_uri;
return性能更优,通过301状态码和$request_uri参数,直接告诉Nginx这是个301重定向和抓取指定URI。