PHP-FPM始终返回200状态码,无论NGINX的状态码如何

4
我有一个基于PHP的错误页面配置,使用NGINX和PHP-FPM。然而,当我请求例如example.com/nothing(不存在的页面)时,PHP-FPM返回200状态码,而不是NGINX返回的正确的404状态码。这也发生在其他错误的情况下(例如example.com/assets在NGINX状态为403时,用PHP-FPM返回200)。本质上,我想让PHP-FPM镜像NGINX显示的状态码(覆盖200状态码),以便我的错误页面显示正确的信息。我知道您可以通过在使用http_response_code();时指定它来更改状态代码,但我宁愿让服务器这样做,而不是我硬编码适当的状态代码。
错误页面:<?echo http_response_code();?> NGINX错误页面配置:
set $errorDocs "/var/www/GLOBAL_RESOURCES/error";
recursive_error_pages on;
location ^~ $errorDocs {
  internal;
  alias $errorDocs;
}
#Resolve error asset location 404s
location /errorAssets {
  root $errorDocs;
}
error_page 404 /404.php;
location = /404.php {
  root $errorDocs;
  include /etc/nginx/xenon-conf/headers/fpm-params.conf;
}

PHP-FPM设置:

include /etc/nginx/fastcgi_params;
include /etc/nginx/fastcgi.conf;
fastcgi_intercept_errors on;
proxy_intercept_errors on;
try_files $uri =404; 
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass  unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Fast-CGI配置:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param  REDIRECT_STATUS    200; 

网站配置:

server {
  listen 80;
  server_name example.com www.example.com;
  access_log /var/log/nginx/example.com.access.log;
  include /etc/nginx/xenon-conf/headers/php-fpm-enable.conf;
  include /etc/nginx/xenon-conf/headers/master-failover.conf;

  set $webRoot "/var/www/example.com";
  root $webRoot;
}

NGINX配置:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
  worker_connections 1024;
}

http {
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile            on;
  tcp_nopush          on;
  tcp_nodelay         on;
  keepalive_timeout   65;
  types_hash_max_size 4096;

  include             /etc/nginx/mime.types;
  default_type        application/octet-stream;

  # Load modular configuration files from the /etc/nginx/conf.d directory.
  # See http://nginx.org/en/docs/ngx_core_module.html#include
  # for more information.
  include /etc/nginx/conf.d/*.conf;

  ### CUSTOM HTTP SERVER MASS IMPORTS ###
  include /etc/nginx/xenon-conf/websites/*.web;
  include /etc/nginx/xenon-conf/mapping/*.map;
}

### CUSTOM GENERIC STREAM MASS IMPORTS ###
include /etc/nginx/xenon-conf/stream/*.conf;

提前感谢您!

2个回答

5
如果nginx检测到来自FastCGI的输出,即使upstream(在本例中为php-fpm)触发错误,它也会将其视为有效响应。
禁用PHP-FPM池中的"display_errors"可以解决此问题。
"php_admin_value [display_errors] = Off"可以防止PHP脚本向屏幕显示错误输出,从而导致nginx正确地抛出HTTP 500内部服务器错误。
$  curl -i localhost:8080/test.php?time=`date +%s` HTTP/1.1 500
Internal Server Error Server: nginx ...

没有输出显示,响应为空。您仍然可以使用error_log指令将所有错误记录到文件中。

php_admin_value[error_log] = /var/log/php-fpm/error.log
php_admin_flag[log_errors] = on

-- 来源

为了将HTTP状态码从nginx传递到PHP-FPM,您还需要在PHP处理位置中添加以下内容:

fastcgi_intercept_errors on;

根据手册,此指令:
确定是否将响应代码大于或等于300的FastCGI服务器传递给客户端,还是拦截并重定向到nginx以使用error_page指令进行处理。

您所描述的情况已经在我的配置中运作良好。我遇到的问题出现在40x错误的错误页面上,这些错误是客户端错误。PHP-FPM在此实例中运行时没有错误,因此它返回200,但我希望它能够反映NGINX显示的状态码。 - Spotlightsrule
我的配置已经设置了“fastcgi_intercept_errors on;”。我提供的PHP-FPM设置被导入到每个PHP文件的错误页面配置中。即使添加了类似的“proxy_intercept_errors”选项,它仍然无法正常工作。 - Spotlightsrule
在这种情况下,请发布您的整个配置文件,以便我可以尝试重现错误。 - Mike
原始问题已更新,包含缺失的配置。 - Spotlightsrule

0

这里的主要问题是,默认情况下,当异常发生并且启用了display_errors时,php-fpm不会向nginx返回状态码。

简而言之

使用一些全局错误处理程序来设置错误状态码,如下所示:

http_response_code(500);

这将从php-fpm传递给nginx并附加到nginx的响应中。

如何测试这个行为

创建index.php文件:

<?php
// report all errors
error_reporting(E_ALL);

// do not display errors
ini_set("display_errors", 0);

// set http response code
// http_response_code(500);

// throw exception
throw new \Exception('TEST UNHANDLED EXCEPTION STATUS CODE');

nginx 创建 default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /var/www/html;
        index  index.php index.html index.htm;
    }

    # pass the PHP scripts to FastCGI server listening on php:9000
    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

创建 docker-compose.yml 文件

version: "3.5"

services:
  php:
    image: php:fpm
    volumes:
      - ./index.php:/var/www/html/index.php

  nginx:
    depends_on:
      - php
    image: nginx:latest
    volumes:
      - ./index.php:/var/www/html/index.php
      - ./default.conf:/etc/nginx/conf.d/default.conf

运行项目

docker-compose up -d

php 容器内安装额外的工具以测试 php-fpm

为了测试从 php-fpm 直接返回的响应,我们需要安装 cgi-fcgi 二进制文件。

docker-compose exec php bash -c 'apt update && apt install -y libfcgi0ldbl'

禁用display_errors测试响应

  1. php 容器内,直接向 php-fpm 发送请求:
    SCRIPT_NAME=/var/www/html/index.php SCRIPT_FILENAME=/var/www/html/index.php REQUEST_METHOD=GET cgi-fcgi -bind -connect localhost:9000
    
  2. 响应包含状态码 500,由 nginx 正确处理:
    Status: 500 Internal Server Error
    X-Powered-By: PHP/8.0.2
    Content-type: text/html; charset=UTF-8
    
    
  3. 测试从 nginx 返回的响应:
    docker-compose exec nginx curl -i localhost
    
    响应:
    HTTP/1.1 500 Internal Server Error
    Server: nginx/1.19.6
    Date: Mon, 22 Feb 2021 11:45:56 GMT
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    X-Powered-By: PHP/8.0.2
    
    
    

php-fpm 响应包含状态码 500,且没有正文。 Nginx 在响应中使用此代码。

启用 display_errors 的测试响应

  1. 让我们在index.php中启用display_errors
    // 不显示错误
    ini_set("display_errors", 1);
    
  2. php容器内,直接请求php-fpm
    SCRIPT_NAME=/var/www/html/index.php SCRIPT_FILENAME=/var/www/html/index.php REQUEST_METHOD=GET cgi-fcgi -bind -connect localhost:9000
    
    响应:
    X-Powered-By: PHP/8.0.2
    Content-type: text/html; charset=UTF-8
    
    <br />
    <b>致命错误</b>:在/var/www/html/index.php的第12行抛出未处理的异常状态代码
    堆栈跟踪:
    #0 {main}
      thrown in <b>/var/www/html/index.php</b> on line <b>12</b><br />
    
  3. 测试从nginx返回的响应
    docker-compose exec nginx curl -i localhost
    
    响应:
    HTTP/1.1 200 OK
    Server: nginx/1.19.6
    Date: Mon, 22 Feb 2021 11:47:29 GMT
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    X-Powered-By: PHP/8.0.2
    
    <br />
    <b>致命错误</b>:在/var/www/html/index.php的第12行抛出未处理的异常状态代码
    堆栈跟踪:
    #0 {main}
      thrown in <b>/var/www/html/index.php</b> on line <b>12</b><br />
    
    

php-fpm 响应缺少状态码 500,但具有正文。 Nginx 将其视为带有状态 200 的正常响应。

启用 display_errors 并将标头明确设置为 500 的测试响应

  1. 让我们在打开 display_errors 的情况下明确设置响应状态码。

    // 设置 HTTP 响应代码
    http_response_code(500);
    
  2. 测试 php-fpm 响应:

    SCRIPT_NAME=/var/www/html/index.php SCRIPT_FILENAME=/var/www/html/index.php REQUEST_METHOD=GET cgi-fcgi -bind -connect localhost:9000
    

    响应:

    Status: 500 Internal Server Error
    X-Powered-By: PHP/8.0.2
    Content-type: text/html; charset=UTF-8
    
    <br />
    <b>Fatal error</b>:  Uncaught Exception: TEST UNHANDLED EXCEPTION STATUS CODE in /var/www/html/index.php:12
    Stack trace:
    #0 {main}
      thrown in <b>/var/www/html/index.php</b> on line <b>12</b><br />
    
  3. 测试 Nginx 响应:

    docker-compose exec nginx curl -i localhost
    

    响应:

    HTTP/1.1 500 Internal Server Error
    Server: nginx/1.19.6
    Date: Mon, 22 Feb 2021 11:52:38 GMT
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    X-Powered-By: PHP/8.0.2
    
    <br />
    <b>Fatal error</b>:  Uncaught Exception: TEST UNHANDLED EXCEPTION STATUS CODE in /var/www/html/index.php:12
    Stack trace:
    #0 {main}
      thrown in <b>/var/www/html/index.php</b> on line <b>12</b><br />
    

php-fpm 响应状态码为 500 并带有主体。 Nginx 使用来自 php-fpm 的主体和状态码。

如何修复

我能想到的一个可能的解决方法是全局错误处理程序,它可以捕获每个未处理的错误并显式地设置正确的错误状态码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接