![](/img/trans.png)
[英]Nginx working as reverse proxy with backend on php-fpm always returns status code 200
[英]PHP-FPM Always Returns 200 Regardless Of NGINX Status Code
我有一个带有 NGINX 和 PHP-FPM 的基于 PHP 的错误页面配置。 但是,例如,当我请求example.com/nothing
(不存在的页面)时,PHP-FPM 返回 200 状态代码,而不是 NGINX 返回的正确 404 状态代码。 其他错误也会发生这种情况(例如: example.com/assets
在 NGINX 状态为 403 时使用 PHP-FPM 返回 200)。 基本上我希望 PHP-FPM 做的是镜像 NGINX 显示的状态代码(用 NGINX 显示的状态代码覆盖 200 状态代码),所以我的错误页面显示正确的信息。 我知道您可以通过在使用http_response_code();
时指定它来更改状态代码http_response_code();
,但我宁愿让服务器执行此操作,而无需我对正确的状态代码进行硬编码。
错误页面: <? echo 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;
快速 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;
提前致谢!
如果 nginx 正在检测来自 FastCGI 上游的输出,它会将其视为有效响应,即使上游(在本例中为 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 指令进行处理。
这里的主要问题是默认情况下php-fpm
不会在发生异常并启用display_errors
时向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
测试响应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
nginx
正确处理的状态代码 500: Status: 500 Internal Server Error X-Powered-By: PHP/8.0.2 Content-type: text/html; charset=UTF-8
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
测试响应index.php
启用display_errors
: // do not display errors ini_set("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
回复: 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 />
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>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
将此视为状态为 200 的正常响应。
display_errors
并将标头显式设置为 500 的测试响应让我们在display_errors
上显式设置响应状态代码。
// set http response code http_response_code(500);
测试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 />
测试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
正文和状态代码。
我能想到的一个可能的解决方法是全局错误处理程序,它捕获每个未处理的错误并显式设置正确的错误状态代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.