nginx File not found 错误分析与解决方法

使用php-fpm解析PHP,出错提示如下:"No input file specified","File not found",原因是php-fpm进程找不到SCRIPT_FILENAME配置的要执行的.php文件,php-fpm返回给nginx的默认404 错误提示。

比如,doucument_root下没有test.php,访问此文件时通过抓包可以看到返回的内容。

HTTP/1.1 404 Not Found
Date: Fri, 21 Dec 2012 08:15:28 GMT
Content-Type: text/html
Proxy-Connection: close
Server: nginx/1.2.5
X-Powered-By: PHP/5.4.7
Via: 1.1 c3300 (NetCache NetApp/6.0.7)
Content-Length: 16

File not found.

不希望用户看到这个默认的404错误信息,想自定义404错误。

先来了解下这种404错误是如何产生的,以及显示自定义的404错误页的方法。

一、错误的路径被发送到php-fpm进程
出现这类错误,大多是后端fastcgi进程收到错误路径(SCRIPT_FILENAME),而后端fastcgi收到错误路径的原因大都是配置错误。

常见的nginx.conf的配置:

复制代码 代码示例:

server {
    listen   [::]:80;
    server_name  example.com www.example.com;
    access_log  /var/www/logs/example.com.access.log;

location / {
        root   /var/www/example.com;
        index  index.html index.htm index.pl;
    }

location /images {
        autoindex on;
    }

location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
        include fastcgi_params;
    }
}

以上配置的问题分析:
root指令被放到了location /
块。如果root指令被定义在location块中那么该root指令只能对其所在的location生效。其它locaiont中没有root指令,像
location
/images块不会匹配任何请求,需要在每个请求中重复配置root指令来解决这个问题。因此我们需要把root指令放在server块,这样各个
location就会继承父server块定义的$document_root,如果某个location需要定义一个不同
的$document_root,则可以在location单独定义一个root指令。

问题2,fastCGI参数SCRIPT_FILENAME
是写死的。如果修改了root指令的值或者移动文件到别的目录,php-fpm会返回“No input file
specified”错误,因为SCRIPT_FILENAME在配置中是写死的并没有随着$doucument_root变化而变化。

可以修改SCRIPT_FILENAME配置如下:

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

因此,不要忘记在server块中配置root指令,不然$document_root的值为空,只会传$fastcgi_script_name到php-fpm,就会导致“No input file specified”错误。

二、请求的文件物理上不存在

当nginx收到一个不在的.php文件
请求时,因为nginx只会检查$uri是否是.php结尾,不会对文件是否存在进行判断,.php结尾的请求nginx会直接发给php-fpm处理。
php-fpm处理时找不到文件就会返回“No input file specified”带着“404 Not Found”头。

解决办法:
在nginx拦截不存在的文件,请求并返回自定义404错误

使用 try_files 捕捉不存在的urls并返回错误。

复制代码 代码示例:

location ~ .php$ {
 try_files $uri =404;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME ....
 ...................................
 ...................................
}

上面的配置会检查.php文件是否存在,如果不存在,会返回404页面。

就介绍这些吧,有关nginx 404错误的内容,建议大家熟练掌握,结合404自定义错误页,实现友好的错误提示吧。

时间: 2025-01-10 15:36:16

nginx File not found 错误分析与解决方法的相关文章

今天用pro安装nginx+php+mysql出现问题的解决方法

今天用pro安装nginx+php+mysql出现问题的解决方法 by 伍雪颖 dyld: Library not loaded: @@[email protected]@/openssl/1.0.1h/lib/libcrypto.1.0.0.dylib Referenced from: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib Reason: image not found 解决方法:重装openssl Starting MySQL . ERR

Nginx常见错误与问题之解决方法技术指南

  Nginx常见错误与问题之解决方法技术指南. 安装环境: 系统环境:redhat enterprise 6.5 64bit 1.Nginx 常见启动错误 有的时候初次安装nginx的时候会报这样的错误 sbin/nginx -c conf/nginx.conf 报错内容:sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or

今天用pro安装nginx+php+mysql出现故障的解决方法

今天用pro安装nginx+php+mysql出现故障的解决方法 by 伍雪颖 dyld: Library not loaded: @@[email protected]@/openssl/1.0.1h/lib/libcrypto.1.0.0.dylib Referenced from: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib Reason: image not found 解决方法:重装openssl Starting MySQL . ERR

swap file "*.swp" already exists!的解决方法

Linux下编程难免要开启多个vim共同编辑同一个文件,这时再次保存就会出现: swap file "*.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort: 原因: 使用vim编辑文件实际是先copy一份临时文件并映射到内存给你编辑, 编辑的是临时文件, 当执行:w后才保存临时文件到原文件,执行:q后才删除临时文件. 每次启动检索式否有临时文件, 有

error opening trace file: No such file or directory (2)错误解决方法

在练习项目的时候报错    error opening trace file: No such file or directory (2)字面意思是找不到文件,实际上也就是找不到文件,查了其他人的解决方法后还是没解决.突然想到新版本的创建布局文件的时候都有两个一个是文件名命名的xml布局文件另一个是,如上图示,当不与java代码交互的时候布局文件写入fragment_main.xml显示没问题,当涉及到与java代码交互时必须放到activity_main.xml中,否则就会抱错,然后程序意外停

InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法

InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法 140628  8:10:48 [Note] Plugin 'FEDERATED' is disabled.140628  8:10:48 InnoDB: The InnoDB memory heap is disabled140628  8:10:48 InnoDB: Mutexes and rw_locks use Windows interlock

Parse error: syntax error, unexpected end of file in *.php on line * 解决方法

Parse error: syntax error, unexpected end of file in *.php on line * 解决方法 这篇文章主要介绍了PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法,需要的朋友可以参考下 今天在写PHP程序的时候总是出现这样的错误:Parse error: syntax error, unexpected end of file in

织梦系统中出现DedeTag Engine Create File False提示原因及解决方法

今天更新网站时dedecms系统时,遇到一个问题:DedeTag Engine Create File False  出现这样的提示. 其实这也不算是什么错误,我个人觉得最重要的一点就是根目录下没有给写权限.当然,我用的是Linux系统.我直接给了777权限之后就可以了. 之后在网上看到了一些答案,也贴在这里.希望对有需要的朋友有所帮助. 第一种情况:站点.文件夹权限不足造成无法建立文件 这种情况的出现,一方面可能是Apache设置的读写权限较严格,另一方面原因可能是使用者通过服务器或FTP对一

Nginx 499错误的原因及解决方法

今天进行系统维护,发现了大量的499错误, 499错误 ngx_string(ngx_http_error_495_page), /* 495, https certificate error */ngx_string(ngx_http_error_496_page), /* 496, https no certificate */ngx_string(ngx_http_error_497_page), /* 497, http to https */ngx_string(ngx_http_er