简体   繁体   English

为什么REQUEST_METHOD被错误地报告为“ GET”?

[英]Why is REQUEST_METHOD misreported as “GET”?

I'm using nginx + php5-fpm for my local development site. 我在本地开发站点上使用nginx + php5-fpm。 While building a form I ran into an extremely strange issue. 在构建表单时,我遇到了一个非常奇怪的问题。

It seems that $_SERVER['REQUEST_METHOD'] is sometimes misreported as "GET" when I am POSTing. 似乎在我发布时,$ _ SERVER ['REQUEST_METHOD']有时会错误地报告为“ GET”。 It gets stranger though: this only seems to happen if the URL contains the magic word "block". 但是,它变得很奇怪:仅当URL包含魔术字“ block”时,这才似乎发生。

For example, if I run the following request: 例如,如果我运行以下请求:

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

The server incorrectly reports this as a GET request, as a var_dump on $_SERVER will show you (the var_dump is the first and last piece of code executed, there is nothing influencing it): 服务器错误地将其报告为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)
  [....]

Even the $_POST superglobal is empty. 甚至$ _POST超全局变量都是空的。 Any URL that does not contain the magic word is reported correctly. 正确报告任何不包含魔术字的URL。 I have verified the above with Postman and Google Chrome. 我已经使用Postman和Google Chrome验证了上述内容。

For reference, here is my nginx configuration file: 供参考,这是我的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;
    }
}

My PHP Version is 5.5.14. 我的PHP版本是5.5.14。

How can I fix this? 我怎样才能解决这个问题?

This rule: 这条规则:

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

block is matched by lock . blocklock匹配。 Then it goes to index.php you set for 404 error. 然后转到您为404错误设置的index.php。

Make it: 做了:

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

The other rule is also potential for trouble. 另一个规则也有潜在的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM