【问题标题】:NGINX strict redirect without query parameters无查询参数的 NGINX 严格重定向
【发布时间】:2020-09-28 16:08:22
【问题描述】:

我有一个网址:

https://www.example.com/test.html

这个 URL 可以有如下查询参数:

https://www.example.com/test.html?cat=11&rating=5

我只想重定向:

https://www.example.com/test.html

收件人:

https://www.example.com/test2

当我这样做时:

location = /test.html {
    return 301 https://example.com/test2;
}

带有参数的 URL 也会重定向: https://www.example.com/test.html?cat=11&rating=5

重定向到:https://example.com/test2

是否可以在不影响参数的情况下严格重定向 URL?

更新:我仍然希望 https://www.example.com/test.html?cat=11&rating=5 正常工作,但 https://www.example.com/test.html 应该重定向到 /test2

更新:NGINX 的本地 docker 机器服务器部分

server {
    listen      80;
    listen      [::]:80;
    server_name magento.test;

    set $MAGE_ROOT /var/www/php;

    set $maintenance off;

    if (-f $MAGE_ROOT/maintenance.enable){
        set $maintenance on;
    }

    include /var/www/php/nginx.conf;
}

更新:

root $MAGE_ROOT;
index index.php index.html index.htm;
    
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
#add_header "X-UA-Compatible" "IE=Edge";
add_header 'X-Content-Type-Options' 'nosniff';    

# Deny access to sensitive files
location /.user.ini {
    deny all;
}


# PHP entry point for setup application
location ~* ^/setup($|/) {
    root $MAGE_ROOT;
    location ~ ^/setup/index.php {
        fastcgi_pass   php:9000;

        fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
        fastcgi_param  PHP_VALUE "memory_limit=4000M \n max_execution_time=600";
        fastcgi_read_timeout 600s;
        fastcgi_connect_timeout 600s;

        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ ^/setup/(?!pub/). {
        deny all;
    }

    location ~ ^/setup/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}

# PHP entry point for update application
location ~* ^/update($|/) {
    root $MAGE_ROOT;

    location ~ ^/update/index.php {
        fastcgi_split_path_info ^(/update/index.php)(/.+)$;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        include        fastcgi_params;
    }

    # Deny everything but index.php
    location ~ ^/update/(?!pub/). {
        deny all;
    }

    location ~ ^/update/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

location /pub/ {
    location ~ ^/pub/media/(downloadable|customer|import|custom_options|theme_customization/.*\.xml) {
        deny all;
    }
    alias $MAGE_ROOT/pub/;
    add_header X-Frame-Options "SAMEORIGIN";
}

location /static/ {
    # Uncomment the following line in production mode
    # expires max;

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
        rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|html|json)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
        }
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;

        if (!-f $request_filename) {
        rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
    }    

    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/ {
    try_files $uri $uri/ /get.php$is_args$args;

    location ~ ^/media/theme_customization/.*\.xml {
        deny all;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/customer/ {
    deny all;
}

location /media/downloadable/ {
    deny all;
}

location /media/import/ {
    deny all;
}

location /media/custom_options/ {
    deny all;
}

location /errors/ {
    location ~* \.xml$ {
        deny all;
    }
}

# PHP entry point for main application
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
    try_files $uri =404;
    fastcgi_pass   php:9000;
    fastcgi_buffers 1024 4k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=4000M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/xml+rss
    image/svg+xml;
gzip_vary on;

# Banned locations (only reached if the earlier PHP entry point regexes don't match)
location ~* (\.php$|\.phtml$|\.htaccess$|\.git) {
    deny all;
}

【问题讨论】:

    标签: nginx redirect


    【解决方案1】:

    查询参数不受 locationrewrite 指令的测试,它们仅适用于 HTTP 请求的规范化 URI。做你想做的唯一方法是检查$args(或$is_args)变量:

    location = /test.html {
        if ($is_args = '') {
            return 301 /test2;
        }
        ... # default request processing
    }
    

    如果您显示完整的 server 块,也许我可以建议更优化的解决方案。例如,如果你只有一个 location 块,这样的事情应该可以工作:

    location / {
        if ($is_args = '') {
            rewrite ^/test\.html$ /test2 permanent;
        }
        ... # default request processing
    }
    

    但是,此配置可能需要注意。我不知道各种浏览器在缓存重定向时是否考虑了查询字符串,并且无论查询字符串是否存在,它都可能导致对/test.html 的任何请求的无条件重定向。为避免这种情况,我建议使用 302 临时重定向:

    return 302 /test2;
    

    rewrite ^/test\.html$ /test2 redirect;
    

    更新

    您可以将此检查添加到主配置文件中的server 上下文中:

    server {
        listen      80;
        listen      [::]:80;
        server_name magento.test;
    
        set $MAGE_ROOT /var/www/php;
    
        set $maintenance off;
    
        if (-f $MAGE_ROOT/maintenance.enable){
            set $maintenance on;
        }
    
        if ($is_args = '') {
            rewrite ^/test\.html$ /test2 permanent;
        }
    
        include /var/www/php/nginx.conf;
    }
    

    location / { ... } magento 配置文件块:

    location / {
        if ($is_args = '') {
            rewrite ^/test\.html$ /test2 permanent;
        }
        try_files $uri $uri/ /index.php$is_args$args;
    }
    

    【讨论】:

    • @KalvinKlien 1. $args 变量值不包含问号,正确的指令是return 301 /hardware.html?$args;。 2. 你确定这不是缓存结果吗?您可以从命令行使用curl 之类的内容检查实际的服务器响应吗?无论如何,我将在我的沙盒上检查这个,我会告诉你我在测试后得到了什么。
    • @KalvinKlien 哦,当然,由于您将/hardware.html?some_query_string 重定向到自身,因此您在此配置中遇到了无限循环。我的意思是在浏览器内部缓存(它们像任何其他静态内容一样缓存 301 重定向),而不是 nginx 缓存。您需要像处理任何其他请求一样处理此 /hardware.html 请求,但我需要查看您的整个 server 块以建议如何处理。
    • @KalvinKlien 再一次 - locationrewrite 指令都适用于 HTTP 请求的规范化 URI,而不是整个请求字符串。您无法使用locationrewrite 指令检查查询字符串,规范化的URI 不包含该字符串,无论查询字符串是否存在,它都只包含/hardware.html。您不需要在 nginx 正则表达式中屏蔽 / 符号(尽管您可以根据需要这样做),但是如果您不屏蔽点符号,它将匹配任何字符。
    • @KalvinKlien 我需要看看你的location 块,这个没有给我任何东西。可能他们在/var/www/php/nginx.conf 里面?看看我的第二个例子,我使用的是location / { ... },而不是/location = test.html { ... }。那是因为我想处理任何其他请求默认方式,包括带有查询参数的/hardware.html
    • @KalvinKlien 您可以在任何location 块之外的server 上下文中直接尝试if ($is_args = '') { rewrite ^/hardware\.html$ /test-page permanent; },将其添加到include /var/www/php/nginx.conf; 行之前。
    猜你喜欢
    • 2015-01-20
    • 2020-10-20
    • 1970-01-01
    • 2014-12-10
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多