文件明明存在nginx却提示404

时间:2025-03-04 10:05:15

近日服务器突然出现问题,浏览器访问时返回“拒绝访问”错误。首先检查了端口情况,

netstat -ntulp |grep 80 

发现80端口没有打开,于是便重启nginx服务

service nginx restart

此时再次查看发现80端口是开启了的,然而访问时提示404错误

经确认服务器的nginx的配置文件中设置的根目录和索引文件均没有错误,  网站根目录下的确存在请求的文件。

此时发现当请求html静态文件时可以正常响应,请求PHP文件时返回404,怀疑是php-fpm出了问题。

因为nginx在处理PHP请求时会根据配置文件中定义的匹配规则将PHP请求转发给相应的fast-cgi(在此为php-fpm),然后再由fast-cgi进行后续处理并返回结果给nginx

#
#some config....
location ~ \.php$ {#This expression can match the php request.
        #Include config file "fastcgi_params"
        include /etc/nginx/fastcgi_params;
        #Enable fastcgi interception error log
        fastcgi_intercept_errors on;
        #Dispatch the php request to fastcgi
        fastcgi_pass 127.0.0.1:9000;
    }
#come config....

所以如果fast_cgi程序无法打开的话,nginx也会返回404错误。

根据配置文件检查9000端口

netstat -ntulp |grep 9000

发现9000端口没有开启,重启php-fpm

service php-fpm restart

问题解决。