使Nginx忽略PHP-FPM中的内容长度标头

时间:2014-02-13 01:57:34

标签: nginx php

我有PHP FPM + Nginx设置。我的一个PHP应用程序设置了无效的内容长度标头,所以我试图使用fastcgi_hide_header忽略它,但它不起作用。它适用于除Content-Length以外的标题,因此我认为特别是存在问题。

这样做的正确方法是什么?我无法修改PHP应用程序来修复问题的根源。

server {
        listen 8000 default_server;

        root /var/www;
        index index.php index.html index.htm;

        rewrite_log on;

        # Make site accessible from http://localhost/
        server_name localhost;

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

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ [^/]\.php(/|$) {
                fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                fastcgi_hide_header X-Fake-Header;
                fastcgi_hide_header Content-Length;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

如果我删除PHP中设置标题的代码(这是所需的输出),则输出:

< HTTP/1.1 200 OK
* Server nginx/1.4.1 (Ubuntu) is not blacklisted
< Server: nginx/1.4.1 (Ubuntu)
< Date: Thu, 13 Feb 2014 01:58:07 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.5.3-1ubuntu2.1

如果我保留代码,但使用上面的nginx配置,我得到了这个:

< HTTP/1.1 200 OK
* Server nginx/1.4.1 (Ubuntu) is not blacklisted
< Server: nginx/1.4.1 (Ubuntu)
< Date: Thu, 13 Feb 2014 01:59:09 GMT
< Content-Type: text/html
< Content-Length: 6
< Connection: keep-alive
< X-Powered-By: PHP/5.5.3-1ubuntu2.1

1 个答案:

答案 0 :(得分:1)

我最终不得不在Nginx中使用HttpHeadersMore module(如果您使用的是Ubuntu,则nginx-extras包含此内容,但不包含nginx-full)。

安装模块后,我刚刚将以下内容添加到我的Nginx配置中:

more_clear_headers Content-Length;

这按预期工作。