nginx 如何在 404 处理程序中处理 POST 请求?

nginx 如何在 404 处理程序中处理 POST 请求?

在我的/etc/nginx/sites-enabled/devdb.easy-ads.com我有:

server {
        root /srv/http/devdb.easy-ads.com/www;
        index index.html index.htm index.nginx-debian.html index.php;
        server_name devdb.easy-ads.com;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }


        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
        error_page 404 = /index.php;

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/devdb.easy-ads.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/devdb.easy-ads.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

这对于非 404 uri 工作正常,并且对于 GET 请求(无论是否为 404)工作正常,但所有 404 POST 请求在发送到 index.php 之前都会转换为 GET 请求,我该如何停止这种情况?在 php 端,它$_POST是一个空数组(即使它不应该是空数组),并且$_SERVER['REQUEST_METHOD']错误地包含GET...

答案1

...由于某些未知原因,

        error_page 404 = /index.php;

强制所有请求都变为 GET 请求,但将其更改为

        error_page 404 = @the404block;
        location @the404block {
                try_files $uri $uri/ /index.php$is_args$args;
        }

解决了这个问题。

这是因为nginx的设计者在设计nginx的时候喝了太多的伏特加。

相关内容