为什么REQUEST_METHOD误报为" GET"?

时间:2014-07-15 13:50:36

标签: php http nginx

我在本地开发网站上使用nginx + php5-fpm。在构建表单时,我遇到了一个非常奇怪的问题。

当我发布POST时,似乎$ _SERVER ['REQUEST_METHOD']有时被误报为“GET”。但它变得陌生:如果URL包含魔术词“block”,这似乎只会发生。

例如,如果我运行以下请求:

POST /block HTTP/1.1
Host: dev.bla.com
Content-Type: application/x-www-form-urlencoded
[...]

服务器错误地将此报告为GET请求,因为$ _SERVER上的var_dump将显示(var_dump是执行的第一个和最后一个代码,没有任何影响它):

array (size=37)
  'USER' => string 'www-data' (length=8)
  'HOME' => string '/var/www' (length=8)
  'FCGI_ROLE' => string 'RESPONDER' (length=9)
  'QUERY_STRING' => string '' (length=0)
  'REQUEST_METHOD' => string 'GET' (length=3)
  'CONTENT_TYPE' => string 'application/x-www-form-urlencoded' (length=33)
  [....]

即使是$ _POST超全球也是空的。任何不包含魔术字的URL都会正确报告。我已经使用Postman和Google Chrome验证了上述内容。

供参考,这是我的nginx配置文件:

server {
    ## Basic configuration
    listen 80;
    root /var/projects/bla;
    index index.php;
    server_name dev.bla.com;

    ## Restrict all directory listings
    autoindex off;

    ## Set the error page to index.php. As index.php applies routing
    ## (based on REQUEST_URI), our own error page will show up.
    error_page 404 = /index.php;

    ## Rewrite everything to index.php, but maintain query string
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    ## Block folders (PHP source code etc)
    location ~ /(code|controllers|models|vendor|views) {
        deny all;
        return 404;
    }

    ## Block file extensions (configuration, composer, READMEs, etc)
    location ~ (\.xml|sql|phar|json|lock|conf|cfg|gitignore|md) {
        deny all;
        return 404;
    }

    ## Proxy requests to php-fpm listening on a Unix socket
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

我的PHP版本是5.5.14。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

这条规则:

location ~ (\.xml|sql|phar|json|lock|conf|cfg|gitignore|md) {
    deny all;
    return 404;
}

blocklock匹配。然后转到你为404错误设置的index.php。

成功:

location ~ \.(xml|sql|phar|json|lock|conf|cfg|gitignore|md)$ {
    deny all;
    return 404;
}

另一条规则也有可能出现问题。