我有一个使用 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();
,但我宁愿让服务器执行此操作,而不需要我硬编码正确的状态代码。
错误页面:<? 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;
提前致谢!