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"; }

3148 次点击
所在节点    问与答
45 条回复
des
2018-06-07 19:07:37 +08:00
这排版。。。没法看
xiri
2018-06-07 19:09:17 +08:00
php-fpm 的配置文件改了吗
orangutan92
2018-06-07 19:11:03 +08:00
@xiri 照着网上的教程改了 n 次都不行。。
orangutan92
2018-06-07 19:11:33 +08:00
@des 我也不知道这编辑器为啥会这样,不支持 markdown 差评
to2false
2018-06-07 19:12:22 +08:00
这没眼看,太乱的排版了
orangutan92
2018-06-07 19:12:43 +08:00
```
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 127.0.0.1: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";
}
```
试试 markdown,代码如上
xiri
2018-06-07 19:14:21 +08:00
php-fpm 的配置文件里 8、10 行

user = nginx
group = nginx

366-370 行,启用系统环境变量(把前面的;去掉)
xiri
2018-06-07 19:15:13 +08:00
@orangutan92 你改过这个吗
gclove
2018-06-07 19:16:38 +08:00
这种问题 ... 去 sf 上问啊 .

把 `location ~ [^/]\.php(/|$)` 改成 `location ~ \.php$`
gclove
2018-06-07 19:20:02 +08:00
还有你作为一个初学者,怎么看这么 "复杂的" 配置教程 ...
明明对你来说 7 行的配置就够了
orangutan92
2018-06-07 19:29:05 +08:00
@xiri 没有改过,php-fpm 的配置文件用的是默认的
```
[www]
listen = /dev/shm/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
```
orangutan92
2018-06-07 19:31:20 +08:00
@gclove 和 location 没关系吧,这个我没有动过的。核心的配置是:
```
location ~ [^/]\.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;
}
```
xiri
2018-06-07 19:37:40 +08:00
@orangutan92 前面没看清,抱歉了。你的 nginx 貌似也是以 www 用户运行的?那这里可能不需要改动

另外同上,你的配置好复杂,一般 nginx 开一个虚拟主机几行配置就弄好了
gclove
2018-06-07 19:39:55 +08:00
我意思是让你把这里改成我这个.

我看了确实没有问题啊,你重启 nginx 了吗 ? 除非你那几个 lua 文件有问题.
orangutan92
2018-06-07 19:40:15 +08:00
@xiri 我用的是 oneinstack 一键安装的,基本的配置和报错我都解决了,现在就卡在这里。。。
gclove
2018-06-07 19:41:07 +08:00
还有一种可能是,你访问的域名并没有访问到这个 `server {}`, 而是到了 `vhost/*.conf` 里面的配置
xiri
2018-06-07 19:43:04 +08:00
@orangutan92 oneinstack 不是有添加虚拟主机的脚本吗?为啥还要自己改
orangutan92
2018-06-07 19:44:04 +08:00
@gclove 重启了的,域名访问 html 和 txt 图片什么的都是正常的,就 php 不行。。。
xiri
2018-06-07 19:44:39 +08:00
@gclove 对,oneinstack 是为每一个站点添加一个 xxx.conf 的单独的配置文件
gclove
2018-06-07 19:44:48 +08:00
看我签名的回复,照我说的检查 @orangutan92

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

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

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

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

© 2021 V2EX