简体   繁体   English

让Nginx调用PHP文件来处理php-fpm下的404错误

[英]Get Nginx to call PHP file to handle 404 errors under php-fpm

I'm trying to configure Nginx to send ALL 404s to a php file for futher processing. 我正在尝试配置Nginx将所有404发送到php文件以进行进一步处理。 I have not got it working. 我没有工作。 With try_files I get a default 404 and without try_files I get no input file specified. 使用try_files,我得到一个默认的404,没有try_files我没有指定输入文件。 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;
    }
}

The answer was to add the line: fastcgi_intercept_errors on; 答案是添加以下行: fastcgi_intercept_errors on;

Then, in order to handle the 404 and possibly return a different status code from the PHP script: error_page 404 /404.php; 然后,为了处理404并可能从PHP脚本返回不同的状态代码: error_page 404 /404.php; Had to simply be changed to: error_page 404 = /404.php; 不得不简单地改为: error_page 404 = /404.php;

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 IRC频道中的善良人士,他们花了一些时间向我展示正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM