ghost博客域名不带www重定向到带www

  ·   ghost     浏览量:

之前的博客域名加了www之后就访问不了,感觉很不舒服,后来几经波折才知道可以同时解析两个,只要改改nginx的配置就行了,原理语法那些我也不太懂,都是白骨谷歌出来的,直接copy,改改就行了

同时访问www和不带www

server {
    listen 80;
    listen [::]:80;

    server_name w3more.cn www.w3more.cn;
    root /var/www/ghost/system/nginx-root;
    
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}

不带www跳转到带www

server {
    listen 80;
    listen [::]:80;

    server_name w3more.cn www.w3more.cn;
    root /var/www/ghost/system/nginx-root;
    if (  $host != 'www.w3more.cn'  )  {
       rewrite ^/(.*)$ http://www.w3more.cn/$1 permanent;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}

带www到不带www

server {
    listen 80;
    listen [::]:80;

    server_name w3more.cn;
    root /var/www/ghost/system/nginx-root;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}
server {
    server_name  www.w3more.cn;
    rewrite ^(.*)$ http://w3more.cn$1 permanent;
}

问题

修改完以后后可能会遇到调用api跨域问题,还要修改一下
/etc/nginx/snippets
下的ssl-params文件,添加上一句

add_header 'Access-Control-Allow-Origin' '*';

重启nginx/服务器OK



收起 >>
ghost博客域名不带www重定向到带www