因发现搜索引擎将不带www和带www域名全部收录了,而且也不一样,

这里将不带www和带www的网站全部重定向到 https://www.yxjc123.com

配置如下:

server {
    listen  80;
    server_name yxjc123.com;
    rewrite ^/(.*)$ https://www.yxjc123.com/$1 permanent;
}

server {
    listen  80;
    server_name www.yxjc123.com;
    rewrite ^/(.*)$ https://www.yxjc123.com/$1 permanent;
}

增加 https://yxjc123.com 跳转到 htts://www.yxjc123.com

server {
       
        listen       443 ssl;    
        server_name www.yxjc123.com yxjc123.com;
        ssl_certificate     /data/ssl/yxjc123.com.pem;
        ssl_certificate_key  /data/ssl/yxjc123.com.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        # 这里是配置的关键
        if ( $host != 'www.yxjc123.com' ) {
            rewrite ^(.*)$ https://www.yxjc123.com$1 permanent; 
        }
}

使用curl命令测试一下:

curl -I yxjc123.com/post/4k4n7q 

返回如下:

Nginx 301 重定向配置

curl -I www.yxjc123.com/post/4k4n7q

Nginx 301 重定向配置

补充 https://yxjc123.com 跳转到 htts://www.yxjc123.com

curl -I https://yxjc123.com/post/4k4n7q
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 18 Mar 2024 13:18:08 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://www.yxjc123.com/post/4k4n7q