如果原始请求不存在,nginx 重定向失败

nginx redirect fails if original request is non-existent

问题陈述: 当原始请求是一个不存在的位置时,nginx 似乎没有重定向。所有其他重定向似乎工作正常。也有可能 php 没有在重定向页面上执行脚本。下面提供了 nginx 配置文件。

问题:我缺少什么以及我需要做什么才能完成这项工作?难不成nginx不支持从不存在的页面重定向?

一些上下文可能会有帮助:这是一个典型的被重定向的请求:

73.234.24.22 - - [30/May/2022:16:15:05 +0000] "GET /postevent3.asp?dta=05%2f30%2f22%2016%3a15%3a22%20507%20%2b149642656%20%2d254084882%20%2d0001787%20682829361 HTTP/1.1" 302 154 "-" "-"

请注意,没有针对此请求的页面显示;该请求被转储到缓冲区中,经过处理并写入数据库。请参阅此 post 的末尾以获取 nginx 调试日志的摘录。

改变URL是不切实际的,因为世界上散布着几十个单位。大多数单位无法远程更新。

现在不用的原站,用的是apache。当前站点使用nginx。我不太熟悉网络服务器,所以我可能错过了一些明显的东西。所有想法将不胜感激。

我还在 80 端口服务器块中尝试了以下操作:

location / {
    try_files $uri $uri/ /data/submit_legacy.php?$query_string;
}

nginx 配置文件------------------------------------ ----------------------------------

server {
    listen 80;
    listen [::]:80;
    root /var/www/example.org;
    server_name example.org www.example.org ;
    index index.php index.html index.htm;
    expires    -1;
    rewrite_log on;
    error_log /var/www/example.org/data/http-org-submit.log debug;

#=# ========================================================================= #=#       
#=# Intercept request for postevent3.asp and send it data/submit_legacy.php,  #=# 
#=# be sure to change to return 301 when testing is complete                  #=#       
#=# ========================================================================= #=#       

    location = /postevent3.asp {
        return 302 https://example.org/data/submit_legacy.php?$query_string; 
    }       

    location / {
        return 302 https://$host$request_uri;
    }
}

server {
    listen [::]:443 ssl http2; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot
    root /var/www/example.org;
    server_name example.org www.example.org;
    index index.php index.html index.htm;
    expires    -1;
    
    rewrite_log on;
    error_log /var/www/example.org/data/https-org-submit.log debug;

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

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

    ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypta/ssl-dhparams.pem; # managed by Certbot
    add_header Strict-Transport-Security "max-age=31536000;  includeSubDomains";
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    
    client_max_body_size 100M;  
}

从nginx调试文件中提取:------------------------------------ ----------------------------

2022/05/30 17:35:15 [debug] 37669#37669: *1 write new buf t:1 f:0 000055BC271BE300, pos 000055BC271BE300, size: 390 file: 0, size: 0
2022/05/30 17:35:15 [debug] 37669#37669: *1 http write filter: l:0 f:0 s:390
2022/05/30 17:35:15 [debug] 37669#37669: *1 http output filter "/postevent3.asp?dta=05%2f30%2f22%2017%3a35%3a32%20564%20%2b188886495%20%2b025481359%20%2b0005990%20570742577"
2022/05/30 17:35:15 [debug] 37669#37669: *1 http copy filter: "/postevent3.asp?dta=05%2f30%2f22%2017%3a35%3a32%20564%20%2b188886495%20%2b025481359%20%2b0005990%20570742577"
2022/05/30 17:35:15 [debug] 37669#37669: *1 image filter
2022/05/30 17:35:15 [debug] 37669#37669: *1 xslt filter body
2022/05/30 17:35:15 [debug] 37669#37669: *1 http postpone filter "/postevent3.asp?dta=05%2f30%2f22%2017%3a35%3a32%20564%20%2b188886495%20%2b025481359%20%2b0005990%20570742577" 000055BC271BE5D0
2022/05/30 17:35:15 [debug] 37669#37669: *1 write old buf t:1 f:0 000055BC271BE300, pos 000055BC271BE300, size: 390 file: 0, size: 0
2022/05/30 17:35:15 [debug] 37669#37669: *1 write new buf t:0 f:0 0000000000000000, pos 000055BC260A7AC0, size: 92 file: 0, size: 0
2022/05/30 17:35:15 [debug] 37669#37669: *1 write new buf t:0 f:0 0000000000000000, pos 000055BC260A7E20, size: 62 file: 0, size: 0
2022/05/30 17:35:15 [debug] 37669#37669: *1 http write filter: l:1 f:0 s:544
2022/05/30 17:35:15 [debug] 37669#37669: *1 http write filter limit 0
2022/05/30 17:35:15 [debug] 37669#37669: *1 writev: 544 of 544
2022/05/30 17:35:15 [debug] 37669#37669: *1 http write filter 0000000000000000
2022/05/30 17:35:15 [debug] 37669#37669: *1 http copy filter: 0 "/postevent3.asp?dta=05%2f30%2f22%2017%3a35%3a32%20564%20%2b188886495%20%2b025481359%20%2b0005990%20570742577"
2022/05/30 17:35:15 [debug] 37669#37669: *1 http finalize request: 0, "/postevent3.asp?dta=05%2f30%2f22%2017%3a35%3a32%20564%20%2b188886495%20%2b025481359%20%2b0005990%20570742577" a:1, c:1
2022/05/30 17:35:15 [debug] 37669#37669: *1 set http keepalive handler
2022/05/30 17:35:15 [debug] 37669#37669: *1 http close request
2022/05/30 17:35:15 [debug] 37669#37669: *1 http log handler
2022/05/30 17:35:15 [debug] 37669#37669: *1 free: 000055BC271E7D10, unused: 10
2022/05/30 17:35:15 [debug] 37669#37669: *1 free: 000055BC271BDE40, unused: 1875
2022/05/30 17:35:15 [debug] 37669#37669: *1 free: 000055BC2719E1B0
2022/05/30 17:35:15 [debug] 37669#37669: *1 hc free: 0000000000000000
2022/05/30 17:35:15 [debug] 37669#37669: *1 hc busy: 0000000000000000 0
2022/05/30 17:35:15 [debug] 37669#37669: *1 tcp_nodelay
2022/05/30 17:35:15 [debug] 37669#37669: *1 reusable connection: 1
2022/05/30 17:35:15 [debug] 37669#37669: *1 event timer add: 32: 65000:84579871
2022/05/30 17:35:15 [debug] 37669#37669: *1 http keepalive handler
2022/05/30 17:35:15 [debug] 37669#37669: *1 malloc: 000055BC2719E1B0:1024
2022/05/30 17:35:15 [debug] 37669#37669: *1 recv: eof:1, avail:-1
2022/05/30 17:35:15 [debug] 37669#37669: *1 recv: fd:32 0 of 1024
2022/05/30 17:35:15 [info] 37669#37669: *1 client 93.181.63.177 closed keepalive connection
2022/05/30 17:35:15 [debug] 37669#37669: *1 close http connection: 32
2022/05/30 17:35:15 [debug] 37669#37669: *1 event timer del: 32: 84579871
2022/05/30 17:35:15 [debug] 37669#37669: *1 reusable connection: 0
2022/05/30 17:35:15 [debug] 37669#37669: *1 free: 000055BC2719E1B0
2022/05/30 17:35:15 [debug] 37669#37669: *1 free: 000055BC271E7B00, unused: 136
2022/05/30 17:35:15 [debug] 37669#37669: *2 http header: "Host: www.example.org"
2022/05/30 17:35:15 [debug] 37669#37669: *2 http header done
2022/05/30 17:35:15 [debug] 37669#37669: *2 event timer del: 32: 84575067
2022/05/30 17:35:15 [debug] 37669#37669: *2 generic phase: 0
2022/05/30 17:35:15 [debug] 37669#37669: *2 rewrite phase: 1
2022/05/30 17:35:15 [debug] 37669#37669: *2 test location: "/"
2022/05/30 17:35:15 [debug] 37669#37669: *2 test location: "postevent3.asp"
2022/05/30 17:35:15 [debug] 37669#37669: *2 using configuration "=/postevent3.asp"
2022/05/30 17:35:15 [debug] 37669#37669: *2 http cl:-1 max:1048576
2022/05/30 17:35:15 [debug] 37669#37669: *2 rewrite phase: 3
2022/05/30 17:35:15 [debug] 37669#37669: *2 http set discard body
2022/05/30 17:35:15 [debug] 37669#37669: *2 http script copy: "https://example.org/data/submit_legacy.php?"
2022/05/30 17:35:15 [debug] 37669#37669: *2 http script var: "dta=10%2F14%2F02%2017%3A35%3A24%2000096%20%2B204823178%20%2B089853375%20%2B0008360%20634488566%20%0D%0A10%2F14%2F02%2017%3A35%3A27%2000096%20%2B204823175%20%2B089853369%20%2B0008402%20392294994%20%0D%0A10%2F14%2F02%2017%3A35%3A31%2000096%20%2B204823170%20%2B089853326%20%2B0008462%20215533034%20%0D%0A10%2F14%2F02%2017%3A35%3A32%2000096%20%2B204823166%20%2B089853317%20%2B0008472%20020574633%20%0D%0A"
2022/05/30 17:35:15 [debug] 37669#37669: *2 http finalize request: 302, "/postevent3.asp?dta=10%2F14%2F02%2017%3A35%3A24%2000096%20%2B204823178%20%2B089853375%20%2B0008360%20634488566%20%0D%0A10%2F14%2F02%2017%3A35%3A27%2000096%20%2B204823175%20%2B089853369%20%2B0008402%20392294994%20%0D%0A10%2F14%2F02%2017%3A35%3A31%2000096%20%2B204823170%20%2B089853326%20%2B0008462%20215533034%20%0D%0A10%2F14%2F02%2017%3A35%3A32%2000096%20%2B204823166%20%2B089853317%20%2B0008472%20020574633%20%0D%0A" a:1, c:1
2022/05/30 17:35:15 [debug] 37669#37669: *2 http special response: 302, "/postevent3.asp?dta=10%2F14%2F02%2017%3A35%3A24%2000096%20%2B204823178%20%2B089853375%20%2B0008360%20634488566%20%0D%0A10%2F14%2F02%2017%3A35%3A27%2000096%20%2B204823175%20%2B089853369%20%2B0008402%20392294994%20%0D%0A10%2F14%2F02%2017%3A35%3A31%2000096%20%2B204823170%20%2B089853326%20%2B0008462%20215533034%20%0D%0A10%2F14%2F02%2017%3A35%3A32%2000096%20%2B204823166%20%2B089853317%20%2B0008472%20020574633%20%0D%0A"
2022/05/30 17:35:15 [debug] 37669#37669: *2 http set discard body
2022/05/30 17:35:15 [debug] 37669#37669: *2 xslt filter header
2022/05/30 17:35:15 [debug] 37669#37669: *2 HTTP/1.1 302 Moved Temporarily
Server: nginx/1.18.0 (Ubuntu)
Date: Mon, 30 May 2022 17:35:15 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: https://example.org/data/submit_legacy.php?dta=10%2F14%2F02%2017%3A35%3A24%2000096%20%2B204823178%20%2B089853375%20%2B0008360%20634488566%20%0D%0A10%2F14%2F02%2017%3A35%3A27%2000096%20%2B204823175%20%2B089853369%20%2B0008402%20392294994%20%0D%0A10%2F14%2F02%2017%3A35%3A31%2000096%20%2B204823170%20%2B089853326%20%2B0008462%20215533034%20%0D%0A10%2F14%2F02%2017%3A35%3A32%2000096%20%2B204823166%20%2B089853317%20%2B0008472%20020574633%20%0D%0A
Expires: Mon, 30 May 2022 17:35:14 GMT
Cache-Control: no-cache

如果我猜测启动此请求的软件无法解释 302 HTTP 重定向代码并发出新请求是正确的,那么您唯一的方法是在 HTTP 块本身内部处理请求。而不是你现在的

location = /postevent3.asp {
    return 302 https://example.org/data/submit_legacy.php?$query_string; 
}       

你可以查看这一段配置:

location = /postevent3.asp {
    rewrite ^ /data/submit_legacy.php break;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$uri;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

如果使用此配置,您的请求将开始出现在您的数据库中,那肯定意味着我最初的猜测是正确的。