重写规则在 Nginx 上不起作用

重写规则在 Nginx 上不起作用

我想从/sitemap.xml 重定向到/sitemap_index.xml。

因此,我联系了插件开发人员并收到了以下重写规则。

    # START Nginx Rewrites for Rank Math Sitemaps
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    # END Nginx Rewrites for Rank Math Sitemaps

我把这个重写规则放在两个地方,如下所示,但它不起作用。


server {

    # START Nginx Rewrites for Rank Math Sitemaps
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    # END Nginx Rewrites for Rank Math Sitemaps
}

server {
    location / {
        try_files $uri $uri/ /index.php?$args;       
        # START Nginx Rewrites for Rank Math Sitemaps
        rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
       # END Nginx Rewrites for Rank Math Sitemaps
    }
}

由于我的Nginx服务器配置有问题,所以我尝试过其他的nginx服务器,但是也不管用。

nginx version: nginx/1.21.6
nginx version: nginx/1.21.4

有人能告诉我应该改变哪个部分才能解决这个问题吗?

或者

还有其他方法可以重定向吗?

完整的服务器块


fastcgi_cache_path /dev/shm/nginx/ levels=1:2 keys_zone=seoartgallery.com:100m max_size=1g inactive=1440m use_temp_path=off;

map $http_accept $suffix {
~image/webp    .webp;
default        '';
    }


server {
    listen       80;
    root /var/www/seoartgallery.com/htdocs;
    server_name  seoartgallery.com www.seoartgallery.com cdn.seoartgallery.com www.cdn.seoartgallery.com;
     add_header  Strict-Transport-Security "max-age=0;";
    
    location = /seo2.txt{}
    location = /seo1.txt{}
    location = /daumreviews.txt{}
    location = /naverseo.txt{}
 
    location = /npay_product_info{
    }
    
    location = /npay_product_info.xml{
    }
    
    location ~ \.(jpg|jpeg|gif|webp)$ {
        try_files $uri =404;
        break;
    }
  
   location /{
            return 301 https://$server_name$request_uri;
        }

}


server {
    listen       80;
    server_name artist.seoartgallery.com www.artist.seoartgallery.com;
    location /{
            return 301 https://$server_name$request_uri;
        }

}



##여기서부터 세오아트 본 홈페이지 설정




server {
    listen       443 ssl http2;
    listen [::]:443 ssl http2;
    server_name  seoartgallery.com www.seoartgallery.com link.seoartgallery.com www.link.seoartgallery.com;
    root /var/www/seoartgallery.com/htdocs;
 
    index  index.php index.html index.htm;
    
    # START Nginx Rewrites for Rank Math Sitemaps
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    # END Nginx Rewrites for Rank Math Sitemaps




    ssl_certificate "/etc/letsencrypt/live/seoartgallery.com/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/seoartgallery.com/privkey.pem";
    ssl_dhparam "/etc/ssl/certs/dhparam.pem";
    
    # Set caches, protocols, and accepted ciphers. This config will merit an A+ SSL Labs score.
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 3m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5';
    ssl_session_tickets off;
    ssl_ecdh_curve X25519:prime256v1:secp521r1:secp384r1;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/letsencrypt/live/seoartgallery.com/chain.pem;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;
    ssl_buffer_size 4k;




  location ~ /wp-content/(?<path>.+)\.(?<ext>jpe?g|png|gif)$  {
        add_header Vary Accept;
                expires 365d;
       try_files /wp-content/uploads-webpc/$path.$ext$suffix /wp-content/$path.$ext$suffix $uri =404;
    }


    client_body_in_file_only clean;
    client_body_buffer_size 16k;
    send_timeout 30s;
    
    access_log off;
    error_log  /var/log/nginx/seoartgallery.com.error.log;

        set $skip_cache 0;

        if ($request_method = POST) {
                set $skip_cache 1;
               set $skip_reason "POST";

        }
        if ($query_string != "") {
                set $skip_cache 1;
               set $skip_reason "QUERY_STRING";

        }
        if ( $cookie_woocommerce_items_in_cart = "1" ){ 
        set $skip_cache 1; 
       set $skip_reason WP_WooCommerce;

        } 
        if ( $cookie_woocommerce_cart_hash = "1" ){ 
        set $skip_cache 1; 
       set $skip_reason WP_WooCommerce;

        } 
        if ( $cookie_wp_woocommerce_session_[a-z0-9] = "1" ){ 
        set $skip_cache 1; 
           set $skip_reason WP_WooCommerce;
        } 
        if ( $cookie_wp_automatewoo_visitor_[a-z0-9] = "1" ){ 
        set $skip_cache 1; 
           set $skip_reason automatewoo;
        } 
        if ( $cookie_wp_automatewoo_session_started = "1" ){ 
        set $skip_cache 1; 
           set $skip_reason automatewoo;
        } 
        
    # WooCommerce-specific cache skip rules
    if ($request_uri ~* "/my-account-2.*|/thskan.*|/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
    set $skip_cache 1;
       set $skip_reason WP_WooCommerce;
    }

    if ( $arg_add-to-cart != "" ) { 
    set $skip_cache 1; 
       set $skip_reason add-to-cart;
    }
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wp_automatewoo_session_started|wp_automatewoo_visitor_[a-z0-9]|arlsf_c|wp_automatewoo_[a-z0-9]|yith_wcwl_products|wordpress_no_cache|wordpress_logged_in|woocommerce_items_in_cart|woocommerce_cart_hash|tinv_wishlistkey") {
            set $skip_cache 1;
           set $skip_reason cookie;
    }
    
    if ($request_uri ~* "(/my-account-2/|/thskan/|/my-account/|pum-[a-z0-9]|pum_[a-z0-9]|/checkout/|/cart/|/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
            set $skip_cache 1;
           set $skip_reason uri;

    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        fastcgi_read_timeout 300;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_buffers 64 16k; # default 8 4k
 
        #이 아래는 fastcgi 추가로 입력한것.
        fastcgi_buffer_size 32k;
        
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache seoartgallery.com;
        fastcgi_cache_valid 200 1440m;
        
        fastcgi_cache_min_uses 1;
        fastcgi_cache_lock on;
        
        add_header X-FastCGI-Cache $upstream_cache_status;
        add_header WP-Bullet-Skip $skip_reason;

        fastcgi_cache_valid 404 1m;
        fastcgi_cache_valid 301 302 500 502 504 30m;

       include fastcgi_params;
    }   
        



    
    # Block dot file (.htaccess .htpasswd .svn .git .env and so on.)
    location ~ /\. {
        deny all;
    }
 
    # Block (log file, binary, certificate, shell script, sql dump file) access.
    location ~* \.(log|binary|pem|enc|crt|conf|cnf|sql|sh|key|yml|lock)$ {
        deny all;
    }
    
 
    # Block access
    location ~* (composer\.json|composer\.lock|composer\.phar|contributing\.md|license\.txt|readme\.rst|readme\.md|readme\.txt|copyright|artisan|gulpfile\.js|package\.json|phpunit\.xml|access_log|error_log|gruntfile\.js)$ {
        deny all;
    }
 
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
 
location /test5{
        try_files $uri $uri/ /test5/index.php?$args;

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }

}
 
   location ^~/.well-known/apple-developer-merchantid-domain-association {
        allow all;
  }


     location ~* \.(?:ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
    add_header Access-Control-Allow-Origin "*";
    expires max;
    }
 
     client_max_body_size 100M;
     
     

    location / {
        try_files $uri $uri/ /index.php?$args;       
        add_header Access-Control-Allow-Origin *;
        

    }

    location = /wp-login.php {
    limit_req  zone=one burst=5 nodelay;
    }

    # Allow Lets Encrypt Domain Validation Program
    location ^~ /.well-known/acme-challenge/ {
        allow all;
    }
    
    
      # Rocket-Nginx configuration
    include rocket-nginx/conf.d/default.conf;
     include optimization/*.conf;
    #include conf.d/Fastcgi.conf;
}




相关内容