- A+
所属分类:挨踢技术
秋辰前面发表了一篇《CentOS7 LNMP环境安装Let’s Encrypt免费SSL证书》文章,但是好多朋友不知道怎么设置https的301重定向,这里分享一下我的301重定向,使你无论你输入http://www.onqc.com/、http://onqc.com/还是https://onqc.com/全部重定向打开https://www.onqc.com,这样更有利于SEO优化。
打开/usr/local/nginx/conf/vhost/你的绑定网站.conf(比如我的是onqc.com.conf),可以用Dreamweaver(收费)或Notepad++(免费)等软件打开并修改(不要用系统自带的记事本)。
- server {
- listen 80;
- listen 443 ssl http2;
- ssl_certificate /usr/local/nginx/conf/ssl/onqc.com.crt; //修改SSL证书的绝对路径(后缀有可能不同)
- ssl_certificate_key /usr/local/nginx/conf/ssl/onqc.com.key; //修改SSL证书的绝对路径(后缀有可能不同)
- 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=15768000;
- ssl_stapling on;
- ssl_stapling_verify on;
- server_name www.onqc.com onqc.com; //修改自己的网址
- access_log /home/wwwlogs/onqc.com_nginx.log combined; //修改网站日志的绝对路径
- index index.html index.htm index.php;
- root /home/wwwroot/onqc.com; //修改网站目录的绝对路径
- if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
- if ($host != www.onqc.com) { return 301 $scheme://www.onqc.com$request_uri; } //修改自己的网址
- include /usr/local/nginx/conf/rewrite/wordpress.conf; //修改网站伪静态规则的绝对路径
- #error_page 404 /404.html;
- #error_page 502 /502.html;
- 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;
- }
- }
以上就是完整的http跳转“https://”301重定向规则,修改上传以后别忘了重启一下Nginx
- service nginx restart