Nginx 环境 PHP -FPM 无法解析 PHP 文件

2018-06-07 18:37:25 +08:00
 orangutan92

访问 php 文件会提示下载框,而不是执行这个文件,我的 nginx.conf 文件的配置如下: user www www; worker_processes auto;

error_log /data/wwwlogs/error_nginx.log crit; pid /var/run/nginx.pid; worker_rlimit_nofile 51200;

events { use epoll; worker_connections 51200; multi_accept on; }

http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 1024m; client_body_buffer_size 10m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on;

fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_intercept_errors on;

#Gzip Compression gzip on; gzip_buffers 16 8k; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon; gzip_disable "MSIE [1-6].(?!.*SV1)";

#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency. open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;

######################## default ############################ server { listen 80; server_name _; access_log /data/wwwlogs/access_nginx.log combined; root /data/wwwroot/default; index index.html index.htm index.php; #error_page 404 /404.html; #error_page 502 /502.html; location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } location ~ [^/].php(/|$) { #fastcgi_pass remote_php_ip:9000; fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ ..(js|css)?$ { expires 7d; access_log off; } location ~ /.ht { deny all; } } ########################## vhost ############################# include vhost/.conf; ########################## WAF ############################# lua_shared_dict limit 10m; lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua"; init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua"; access_by_lua_file "/usr/local/openresty/nginx/conf/waf/waf.lua"; }

3171 次点击
所在节点    问与答
45 条回复
orangutan92
2018-06-07 19:47:03 +08:00
@gclove 改成 `location ~ \.php$`还是不行
Track3
2018-06-07 19:56:50 +08:00
貌似你的 php-fpm 用的是 unix socket,不是 tcp。把 nginx 配置文件中`fastcgi_pass 127.0.0.1:9000; `这一行注释,换成下面的`fastcgi_pass unix:/dev/shm/php-cgi.sock; `试试
orangutan92
2018-06-07 19:57:46 +08:00
@gclove vhost 里面的配置很简单呐,就是域名和路径
orangutan92
2018-06-07 20:00:03 +08:00
@Track3 之前用的就是那个.sock 文件,还是有问题。这个文件在我重启了 fpm 后变成空的了
Track3
2018-06-07 20:04:47 +08:00
那个文件有权限吗?把所有者改成 www 试试,如果还是不行,干脆把 php-fpm 配置改成 listen = 127.0.0.1:9000,反正这个地址跟 nginx 配置是必须对应的
dototototo
2018-06-07 20:24:20 +08:00
你用别的浏览器试试是否还存在这个问题?
include mime.types;
default_type application/octet-stream;
可以试着从这两行里面找找原因。
gclove
2018-06-07 20:29:06 +08:00
@orangutan92 你描述太简单了,你访问的哪个域名,在 vhost 里有这个域名吗 ?

不过怎么样,nginx 配置了 fastcgi_pass 是不会把源文件发过去的. cgi 问题
gclove
2018-06-07 20:32:15 +08:00
不管怎么样,nginx 配置了 fastcgi_pass 是不会把源文件发过去的. 能发过去的只有 cgi。
上面打错了
orangutan92
2018-06-07 20:43:31 +08:00
@Track3 listen 试过了不行呢
orangutan92
2018-06-07 20:43:49 +08:00
@dototototo 好,我明天试试
1762628386
2018-06-07 20:51:32 +08:00
没走到 php 下面的问题看看吧

#fastcgi_pass remote_php_ip:9000;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
orangutan92
2018-06-08 09:33:41 +08:00
@1762628386 这个配置有什么问题呢?
orangutan92
2018-06-08 09:35:09 +08:00
@dototototo 刚刚试过了,火狐也不行
jasonhzy
2018-06-08 09:45:27 +08:00
@orangutan92 注意这个设置 default_type application/octet-stream, 可以改成 default_type text/html
wqsfree
2018-06-08 10:03:31 +08:00
兄弟下面是我的配置,你自己看看对一下,不行你就自己复制我这个粘贴过,修改下对应的路径
nginx 配置文件

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http

{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;

server

{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}

这下面的是 PHP 配置文件
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
user = php-fpm
group = php-fpm
listen.owner = nobody //和上面的 nginx 的一致
listen.group = nobody // 同上
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
wqsfree
2018-06-08 10:05:29 +08:00
最好的方法是看看你的错误日志,报的是什么错误,这个很好解决的。你这个配置文件看的我头疼
yesono
2018-06-08 13:48:31 +08:00
默认 OneninStack nginx 连接 PHP 是走 /dev/shm/php-cgi.sock 你非要改成走端口,9000 端口没起来....
`
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/dev/shm/php-cgi.sock;
`
orangutan92
2018-06-08 15:20:09 +08:00
@wqsfree user 为啥是 nobody 啊,Nginx 没有错误日志看不了吧,下载不算 error 的。
orangutan92
2018-06-08 15:20:46 +08:00
@yesono 最开始的时候用的就是这个 sock,要是有用我就不改了。。。
wqsfree
2018-06-08 15:34:27 +08:00
@orangutan92 自定义一个用户跑 nginx 服务,这个用户你自己随便设置,要不你给 root 权限给我,我帮你弄吧,我看你挺费劲的

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

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

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

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

© 2021 V2EX