Nginx 如何使用正则表达式掩码删除 URL 的一部分来进行重定向?

Nginx 如何使用正则表达式掩码删除 URL 的一部分来进行重定向?

我需要从 URL 重定向到 example.com/some-urltext-nav.html(仅删除“-nav”)。我在块example.com/some-urltext.html中尝试了一些正则表达式,但什么也没发生……serverrewrite ^.*?-nav\.html$/

server {
    server_name example.com cdn.example.com www.example.com;
    charset off;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    include /etc/nginx/vhosts-includes/*.conf;
    include /etc/nginx/vhosts-resources/example.com/*.conf;
    access_log /var/www/httpd-logs/example.com.access.log;
    error_log /var/www/httpd-logs/example.com.error.log notice;
    ssi on;
    set $root_path /var/www/main/data/www/example.com;
    

    
    
    location / {
    
        location ~ [^/]\.ph(p\d*|tml)$ {
            try_files /does_not_exists @php;
        }
        location ~ ^/(?<capture>.+)-nav\.html$ {
        return 302 /$capture.html;
        }

        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
            expires 365d;
        }
    }
    gzip on;
    gzip_comp_level 5;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    location @php {
        fastcgi_index index.php;
        fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
        fastcgi_pass unix:/var/www/php-fpm/19.sock;
        fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
        try_files $uri =404;
        include fastcgi_params;
    }
    root $root_path;
    listen 95.955.43.229:80;
}
server {
    server_name example.com www.example.com;
    ssl_certificate "/var/www/httpd-cert/main/example.com-to-23-10-2021.crtca";
    ssl_certificate_key "/var/www/httpd-cert/main/example.com-to-23-10-2021.key";
    ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    add_header Strict-Transport-Security "max-age=31536000;";
    ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
    charset off;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    include /etc/nginx/vhosts-includes/*.conf;
    include /etc/nginx/vhosts-resources/example.com/*.conf;
    access_log /var/www/httpd-logs/example.com.access.log;
    error_log /var/www/httpd-logs/example.com.error.log notice;
    ssi on;
    set $root_path /var/www/main/data/www/example.com;
    gzip on;
    gzip_comp_level 5;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    if (!-e $request_filename) { rewrite / /index.php last; }
    
    
    
    location / {
        
        location ~ [^/]\.ph(p\d*|tml)$ {
            try_files /does_not_exists @php;
        }
        
        location ~ ^/(?<capture>.+)-nav\.html$ {
        return 302 /$capture.html;
        }
        
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
            expires 365d;
        }
    }
    location @php {
        fastcgi_index index.php;
        fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
        fastcgi_pass unix:/var/www/php-fpm/19.sock;
        fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
        try_files $uri =404;
        include fastcgi_params;
    }
    root $root_path;
    listen 95.955.43.229:443 ssl http2;
}

答案1

以下代码片段应该符合您的期望:

location ~ ^(?<prefix>/.+?)-nav(?<suffix>\.html)$ {
    return 301 https://$host$prefix$suffix;
}

正则表达式将关键字前后的部分捕获到变量中,然后用于形成return语句URL。

更新:

您的配置有

location ~ [^/]\.ph(p\d*|tml)$ {

在上述规则之前。此规则匹配以 结尾的任何路径ml。因此,nginx 会将其用于location以 结尾的任何请求html

您需要交换两个location块的位置以确保 nginx 评估第一个块。

此外,这个正则表达式看起来相当“漏洞百出”。您可能需要重新考虑一下您到底想让正则表达式匹配什么,并确保它只匹配那些项目。

相关内容