Nginx 环境 PHP -FPM 无法解析 PHP 文件 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
orangutan92
V2EX    问与答

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

  •  < href="Javascript:" Onclick="downVoteTopic(461291);" class="vote">
  •   orangutan92 2018-06-07 18:37:25 +08:00 3696 次点击
    这是一个创建于 2752 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    45 条回复    2018-06-13 14:05:25 +08:00
    des
        1
    des  
       2018-06-07 19:07:37 +08:00 via Android
    这排版。。。没法看
    xiri
        2
    xiri  
       2018-06-07 19:09:17 +08:00 via Android
    php-fpm 的配置文件改了吗
    orangutan92
        3
    orangutan92  
    OP
       2018-06-07 19:11:03 +08:00
    @xiri 照着网上的教程改了 n 次都不行。。
    orangutan92
        4
    orangutan92  
    OP
       2018-06-07 19:11:33 +08:00
    @des 我也不知道这编辑器为啥会这样,不支持 markdown 差评
    to2false
        5
    to2false  
       2018-06-07 19:12:22 +08:00 via Android
    这没眼看,太乱的排版了
    orangutan92
        6
    orangutan92  
    OP
       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
        7
    xiri  
       2018-06-07 19:14:21 +08:00 via Android
    php-fpm 的配置文件里 8、10 行

    user = nginx
    group = nginx

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

    把 `location ~ [^/]\.php(/|$)` 改成 `location ~ \.php$`
    gclove
        10
    gclove  
       2018-06-07 19:20:02 +08:00
    还有你作为一个初学者,怎么看这么 "复杂的" 配置教程 ...
    明明对你来说 7 行的配置就够了
    orangutan92
        11
    orangutan92  
    OP
       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
        12
    orangutan92  
    OP
       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
        13
    xiri  
       2018-06-07 19:37:40 +08:00 via Android
    @orangutan92 前面没看清,抱歉了。你的 nginx 貌似也是以 www 用户运行的?那这里可能不需要改动

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

    我看了确实没有问题啊,你重启 nginx 了吗 ? 除非你那几个 lua 文件有问题.
    orangutan92
        15
    orangutan92  
    OP
       2018-06-07 19:40:15 +08:00
    @xiri 我用的是 oneinstack 一键安装的,基本的配置和报错我都解决了,现在就卡在这里。。。
    gclove
        16
    gclove  
       2018-06-07 19:41:07 +08:00
    还有一种可能是,你访问的域名并没有访问到这个 `server {}`, 而是到了 `vhost/*.conf` 里面的配置
    xiri
        17
    xiri  
       2018-06-07 19:43:04 +08:00 via Android
    @orangutan92 oneinstack 不是有添加虚拟主机的脚本吗?为啥还要自己改
    orangutan92
        18
    orangutan92  
    OP
       2018-06-07 19:44:04 +08:00
    @gclove 重启了的,域名访问 html 和 txt 图片什么的都是正常的,就 php 不行。。。
    xiri
        19
    xiri  
       2018-06-07 19:44:39 +08:00 via Android
    @gclove 对,oneinstack 是为每一个站点添加一个 xxx.conf 的单独的配置文件
    gclove
        20
    gclove  
       2018-06-07 19:44:48 +08:00
    看我签名的回复,照我说的检查 @orangutan92
    orangutan92
        21
    orangutan92  
    OP
       2018-06-07 19:47:03 +08:00
    @gclove 改成 `location ~ \.php$`还是不行
    Track3
        22
    Track3  
       2018-06-07 19:56:50 +08:00 via Android
    貌似你的 php-fpm 用的是 unix socket,不是 tcp。把 nginx 配置文件中`fastcgi_pass 127.0.0.1:9000; `这一行注释,换成下面的`fastcgi_pass unix:/dev/shm/php-cgi.sock; `试试
    orangutan92
        23
    orangutan92  
    OP
       2018-06-07 19:57:46 +08:00
    @gclove vhost 里面的配置很简单呐,就是域名和路径
    orangutan92
        24
    orangutan92  
    OP
       2018-06-07 20:00:03 +08:00
    @Track3 之前用的就是那个.sock 文件,还是有问题。这个文件在我重启了 fpm 后变成空的了
    Track3
        25
    Track3  
       2018-06-07 20:04:47 +08:00 via Android
    那个文件有权限吗?把所有者改成 www 试试,如果还是不行,干脆把 php-fpm 配置改成 listen = 127.0.0.1:9000,反正这个地址跟 nginx 配置是必须对应的
    dototototo
        26
    dototototo  
       2018-06-07 20:24:20 +08:00 via Android
    你用别的浏览器试试是否还存在这个问题?
    include mime.types;
    default_type application/octet-stream;
    可以试着从这两行里面找找原因。
    gclove
        27
    gclove  
       2018-06-07 20:29:06 +08:00
    @orangutan92 你描述太简单了,你访问的哪个域名,在 vhost 里有这个域名吗 ?

    不过怎么样,nginx 配置了 fastcgi_pass 是不会把源文件发过去的. cgi 问题
    gclove
        28
    gclove  
       2018-06-07 20:32:15 +08:00
    不管怎么样,nginx 配置了 fastcgi_pass 是不会把源文件发过去的. 能发过去的只有 cgi。
    上面打错了
    orangutan92
        29
    orangutan92  
    OP
       2018-06-07 20:43:31 +08:00 via Android
    @Track3 listen 试过了不行呢
    orangutan92
        30
    orangutan92  
    OP
       2018-06-07 20:43:49 +08:00 via Android
    @dototototo 好,我明天试试
    1762628386
        31
    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
        32
    orangutan92  
    OP
       2018-06-08 09:33:41 +08:00
    @1762628386 这个配置有什么问题呢?
    orangutan92
        33
    orangutan92  
    OP
       2018-06-08 09:35:09 +08:00
    @dototototo 刚刚试过了,火狐也不行
    jasonhzy
        34
    jasonhzy  
       2018-06-08 09:45:27 +08:00
    @orangutan92 注意这个设置 default_type application/octet-stream, 可以改成 default_type text/html
    wqsfree
        35
    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
        36
    wqsfree  
       2018-06-08 10:05:29 +08:00
    最好的方法是看看你的错误日志,报的是什么错误,这个很好解决的。你这个配置文件看的我头疼
    yesono
        37
    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
        38
    orangutan92  
    OP
       2018-06-08 15:20:09 +08:00
    @wqsfree user 为啥是 nobody 啊,Nginx 没有错误日志看不了吧,下载不算 error 的。
    orangutan92
        39
    orangutan92  
    OP
       2018-06-08 15:20:46 +08:00
    @yesono 最开始的时候用的就是这个 sock,要是有用我就不改了。。。
    wqsfree
        40
    wqsfree  
       2018-06-08 15:34:27 +08:00
    @orangutan92 自定义一个用户跑 nginx 服务,这个用户你自己随便设置,要不你给 root 权限给我,我帮你弄吧,我看你挺费劲的
    orangutan92
        41
    orangutan92  
    OP
       2018-06-08 16:26:46 +08:00
    @wqsfree 好,留个你的 QQ 吧~
    wqsfree
        42
    wqsfree  
       2018-06-08 16:37:49 +08:00   1
    @orangutan92 771009669
    orangutan92
        43
    orangutan92  
    OP
       2018-06-11 16:59:42 +08:00
    @wqsfree 感谢这位兄弟帮我解决了问题,我可以把你修改的配置贴出来吗?方便以后遇到同样问题的人~
    wqsfree
        44
    wqsfree  
       2018-06-12 17:10:03 +08:00
    @orangutan92 可以,但是我想把我留下的 QQ 号码删除了,怎么才能联系到站长把我留下的 QQ 号码删了呢?
    orangutan92
        45
    orangutan92  
    OP
       2018-06-13 14:05:25 +08:00
    其实就是把 vhost 里面的虚拟主机配置文件重写一下,把 nginx.conf 的 server 配置粘贴过来,原来的参数还是加上。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2726 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 44ms UTC 02:36 PVG 10:36 LAX 18:36 JFK 21:36
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86