遇到一个很久都无法解决的问题,就是我的网站在开启证书后,想把80端口的域名、不带www的域名全部301跳转到带SSL证书的www域名,看我下面的例子:
http://dobunkan.com 301 到 https://www.sobuer.com
http://www.dobunkan.com 301 到 https://www.sobuer.com
https://dobunkan.com 301 到 https://www.sobuer.com
看起来很简单的问题,但是解决起来非常麻烦,因为nginx重写规则不支持 if(条件1 || 条件2),今天想到了一个很笨的解决办法,思路是这样的,把80端口、443端口的顶级域名、443端口的长域名分开写,这样就达到目的了,请看我的例子:
server { listen 80; server_name www.dobunkan.com dobunkan.com; rewrite ^(.*)$ https://www.sobuer.com$1 permanent; } server { listen 443 ssl; ssl_certificate /www/host/domAIn/vhost/cert/domain.dobunkan.com/fullchain.pem; ssl_certificate_key /www/host/domain/vhost/cert/domain.dobunkan.com/privkey.pem; server_name dobunkan.com; return 301 https://www.sobuer.com$request_uri; } server { listen 443 ssl; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/domain.dobunkan.com/public_html; ssl_certificate /www/host/domain/vhost/cert/domain.dobunkan.com/fullchain.pem; ssl_certificate_key /www/host/domain/vhost/cert/domain.dobunkan.com/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; error_page 497 https://$host$request_uri; server_name www.dobunkan.com; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-73.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/host/domain/vhost/rewrite/domain.dobunkan.com.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log off; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; error_log off; access_log off; } access_log /www/wwwlogs/domain.dobunkan.com.log; error_log /www/wwwlogs/domain.dobunkan.com.error.log; }
非常笨的方法顺利的解决了nginx301跳转的问题,如果你有更好的方法,请在下方留言!!!
原文链接:https://www.sobuer.com/server/147,转载请注明出处。
评论0