获取Nginx来调用PHP文件以处理php-fpm下的404错误 [英] Get Nginx to call PHP file to handle 404 errors under php-fpm
问题描述
我正在尝试将Nginx配置为将所有404发送到php文件以进行进一步处理.我没有工作.使用try_files时,我会得到默认的404,而没有try_files时,我会没有输入文件.这是我到目前为止的内容:
I'm trying to configure Nginx to send ALL 404s to a php file for futher processing. I have not got it working. With try_files I get a default 404 and without try_files I get no input file specified. This is what I have so far:
server {
listen 192.168.100.44:80;
location / {
index index.html;
}
root /var/www/test.example.com;
error_page 404 /404.php;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#try_files $uri = 404;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
推荐答案
答案是添加以下行: fastcgi_intercept_errors;
The answer was to add the line: fastcgi_intercept_errors on;
然后,为了处理404并可能返回与PHP脚本不同的状态代码: error_page 404/404.php; 只需简单地更改为: error_page 404 =/404.php;
Then, in order to handle the 404 and possibly return a different status code from the PHP script: error_page 404 /404.php; Had to simply be changed to: error_page 404 = /404.php;
感谢Nginx IRC频道中的友善人士,他们花了一些时间向我展示正确的方向.
Credit to the kind people in the Nginx IRC channel who took a few moments of their time to show me the right direction.
这篇关于获取Nginx来调用PHP文件以处理php-fpm下的404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!