Leif160519的blog Leif160519的blog

——————

目录
nginx设置301重定向
/  

nginx设置301重定向


设置www.github.icu强制跳转github.icu域名,首先保证www.github.icugithub.icu解析到同一个IP地址:

image.png

其次修改nginx配置文件中的server_name部分,新增如下判断:

if ($host ~* www.github.icu) {
    rewrite ^(.*)$ https://github.icu$1 permanent;
    }

之后使用nginx -s reload重载nginx配置即可。

注意:因为我是80强制跳转443,故以上判断只需要在443上添加即可,其他情况酌情参考修改:

server {
    listen 443 ssl;
    server_name github.icu;
    if ($host ~ "\d+\.\d+\.\d+\.\d") {
        return https://github.icu;
    }
    if ($host ~* www.github.icu) {
    rewrite ^(.*)$ https://github.icu$1 permanent;
    }
    ssl_certificate /etc/nginx/ssl/github.icu.pem;
    ssl_certificate_key /etc/nginx/ssl/github.icu.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ...

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题nginx设置301重定向
作  者Leif160519
出  处https://github.icu/articles/2020/03/11/1583898044469.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!