Nginx和php-fpm为php脚本处理404错误(不记录)

时间:2013-11-10 21:54:40

标签: nginx php

目前我正在使用:

处理php脚本
location ~ \.php$ {
    fastcgi_pass 127.0.0.1:7777;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

它适用于现有脚本,但是当我在浏览器中打开http://domain.com/somenotexistingscript.php时,我的错误日志显示:

2013/11/10 21:20:56 [error] 32576#0: *195970 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: domain.com, request: "GET /somenotexistingscript.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:7777", host: "domain.com"

浏览器显示“找不到文件”。我试图在给定的块中添加try_files $uri =404并再次打开页面。出现了默认的nginx错误页面但是日志显示:

2013/11/10 21:26:55 [error] 1284#0: *18 open() "/www/domain.com127.0.0.1:7777" failed (2: No such file or directory), client: x.x.x.x, server: domain.com, request: "GET /somenotexistingscript.php HTTP/1.1", host: "domain.com"

使用this question我设法显示自定义php错误页面,但不幸的是日志仍显示:

2013/11/10 21:20:56 [error] 32576#0: *195970 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: domain.com, request: "GET /somenotexistingscript.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:7777", host: "domain.com"

是否可以处理不存在的脚本而不记录任何错误信息?

1 个答案:

答案 0 :(得分:2)

解决方案是将try_files $uri =404;用于:

fastcgi_split_path_info ^(.+\.php)(/.+)$;

所以配置如下:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:7777;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}