我尝试将 php 文件放入文件夹info.php
中/usr/local/www/nginx
。对于 url http://test.com/info.php
,它给出了错误404 - Not found
。
我可以访问index.html
url 中的文件http://test.com
我的服务器配置nginx.conf
文件如下。
server {
listen 80;
server_name test.com www.test.com;
root /usr/local/www/nginx;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
请指出我遗漏的内容。谢谢。
我的错误日志如下-
2015/09/08 09:13:06 [notice] 8965#0: using the "kqueue" event method
2015/09/08 09:13:06 [notice] 8965#0: nginx/1.7.7
2015/09/08 09:13:06 [notice] 8965#0: built by clang 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
2015/09/08 09:13:06 [notice] 8965#0: OS: Darwin 14.4.0
2015/09/08 09:13:06 [notice] 8965#0: hw.ncpu: 4
2015/09/08 09:13:06 [notice] 8965#0: net.inet.tcp.sendspace: 131072
2015/09/08 09:13:06 [notice] 8965#0: kern.ipc.somaxconn: 128
2015/09/08 09:13:06 [notice] 8965#0: getrlimit(RLIMIT_NOFILE): 2560:9223372036854775807
2015/09/08 09:13:06 [notice] 8966#0: start worker processes
2015/09/08 09:13:06 [notice] 8966#0: start worker process 8967
2015/09/08 09:13:06 [notice] 8966#0: start worker process 8968
2015/09/08 09:13:06 [notice] 8966#0: signal 23 (SIGIO) received
2015/09/08 09:13:11 [crit] 8968#0: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock:", host: "test.com"
2015/09/08 09:13:11 [error] 8968#0: *1 open() "/usr/local/www/nginx-dist/50x.html" failed (2: No such file or directory), client: 127.0.0.1, server: test.com, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock", host: "test.com"
答案1
您发布的错误日志中的最后两行显示 php-fpm 没有创建 sock 文件。
请检查 php-fpm 的池配置文件中配置是否正确,它可能看起来像这样:
listen = /var/run/php5-fpm.sock
php-fpm 的默认监听器是127.0.0.1:9000
。
如果您发现没有/var/run/php5-fpm.sock
,或者不知道如何查找和调整池配置,则可能需要以下 nginx 配置。
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
答案2
这是您的问题:
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
这与您设置的文档根目录不对应。我不确定您在哪里找到此配置,但它有问题。
应为:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
答案3
错误日志中的倒数第二行是导致问题的直接原因:PHP-FPM 未运行,或者正在监听其他套接字。您需要修复fastcgi_pass
配置参数以指向正确的 PHP-FPM 套接字,或者让 PHP-FPM 正确运行并监听配置中指定的套接字。