大网数据广告活动
服务器运维问答

How to debug big redirect chain in nginx. Unexpected redirect

· 52CE 我爱测

I have a task to make a large site migration now. And i have a large number of redirects in config. But I cannot understand why one redirect causing second redirect in chain. Even if any other redirects commented/disabled. So my …

问题原文

原帖链接 · Insspb · CC BY-SA 4.0

I have a task to make a large site migration now. And i have a large number of redirects in config. But I cannot understand why one redirect causing second redirect in chain. Even if any other redirects commented/disabled.

So my config for location:

server {

...
index index.php;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}
location ~* /index.php {
    if ($args ~ "^page=flights&start_city=([\+\w\d\s]*)(&.*)?$"){
        return 301 $scheme://$server_name/flights?departure_city=$arg_start_city;
    }
    if ($is_args){
        return 301 $scheme://$server_name/$1;
    }

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

What do I expect? I expect that old links like

https://example.com/index.php?page=flights&start_city=Berlin&s_loc_lat=&s_loc_long=

Become:

https://example.com/flights?departure_city=Berlin

And at the same time links like:

https://example.com/index.php?someoldpar=someoldkey&someoldpar2=someoldkey2

Become:

    https://example.com/

What I got when testing this config?

. I got this redirection chain for first link:

 1. GET https://example.com/index.php?page=flights&start_city=Berlin&s_loc_lat=&s_loc_long=
 2. 301 https://example.com/flights?departure_city=Berlin
 3. 301 https://example.com/

How to exclude step 3 for links that already redirected? Lavarel under backend.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

Your try_files statement rewrites the URI to /index.php and appends the query string. Your if ($is_args) statement redirects any URI with a query string. You have a redirection loop.

The location ~* /index.php block cannot differentiate between an original request containing index.php and the result of the try_files default.

You should perform your tests on the $request_uri variable, which will always contain the original request together with the query string. You can use an if or a map. With more than a few regular expressions, a map is the preferred solution.

For example:

map $request_uri $redirect {
    default 0;
    ~^/index.php\?page=flights&start_city=(?<startcity>[\+\w\d\s]*)(&.*)?$ /flights?departure_city=$startcity;
    ~^/index.php\?                                                         /;
}

server {
    ...
    if ($redirect) {
        return 301 $redirect;
    }

You can remove your location ~* /index.php block, as it's no longer required. Regular expressions are evaluated in order until a matching rule is found, so order the rules with most specific first and least specific last. Use named captures, as the numeric captures can go out of scope. You do not need to specify the scheme or domain name if they are the same. The map directive sits outside of the `server block. See this document for details.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。