nginx + php-fpm 无法解析 php 文件的问题

2015-05-27 14:39:07 +08:00
 hobbyliu

打开网页,php源码暴露,nginx配置文件如下

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

13026 次点击
所在节点    问与答
14 条回复
hobbyliu
2015-05-27 14:40:54 +08:00
可以确定,PHP-FPM 监听9000 端口正常
[root@localhost ~]# netstat -npa | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2142/php-fpm: maste
endoffight
2015-05-27 14:44:20 +08:00
加一条root 指向根目录试试
hobbyliu
2015-05-27 14:47:07 +08:00
@endoffight 加了 无效
location ~ \.php$ { |~
root /home/hobby/service; |~
fastcgi_pass 127.0.0.1:9000; |~
fastcgi_index index.php; |~
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |~
include fastcgi_params; |~
}
xjx0524
2015-05-27 14:50:20 +08:00
把$document_root改成/home/hobby/service试试
shiny
2015-05-27 14:52:56 +08:00
nginx 配置没走到这个 location 吧。走到这个 location 如果有问题就是 50x 错误。 建议贴全 nginx 配置
hobbyliu
2015-05-27 14:58:56 +08:00
@shiny
```
#user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

pid /run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

index index.html index.htm;


##############################
server {
listen 80;
server_name lumentest;
root /home/hobby/service;
#root /usr/share/nginx/html;
index index.html index.htm;

#charset koi8-r;

#access_log logs/host.access.log main;

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
shiny
2015-05-27 15:02:40 +08:00
@hobbyliu 没看到你 php 配置在哪了
endoffight
2015-05-27 15:09:53 +08:00
location ~ .*\.(php|php5){
45 fastcgi_pass 127.0.0.1:9000;
46 fastcgi_index index.php;
47 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
48 include fastcgi_params;


你改成我这个正则试试
fangjinmin
2015-05-27 15:31:08 +08:00
很明显是php文件没有当作PHP解析。
按照下面的步骤看一下,
1,php-fpm的配置文件,看一下php-fpm的运行的user和group是否设置正确。
2,重启php-fpm和nginx
userlogin
2015-05-27 15:37:38 +08:00
将:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
改成:
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
reload nginx.
userlogin
2015-05-27 15:40:52 +08:00
ps. 如上条,如nginx根目录非/usr/share/nginx/html,请根据实际情况更换为nginx的根目录路径。
Havee
2015-05-27 16:13:18 +08:00
php-fpm 配置中的权限是否跟你 /home/hobby/service 的权限一致
huigeer
2015-05-27 16:24:32 +08:00
看看nginx的error_log
hzqim
2015-05-27 17:11:02 +08:00
空白页吧?

来来来
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}

修改一下include fastcgi.conf;

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/194116

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX