V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
hobbyliu
V2EX  ›  问与答

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

  •  
  •   hobbyliu · 2015-05-27 14:39:07 +08:00 · 13126 次点击
    这是一个创建于 3469 天前的主题,其中的信息可能已经有所发展或是发生改变。

    打开网页,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;
    }

    14 条回复    2015-05-27 17:11:02 +08:00
    hobbyliu
        1
    hobbyliu  
    OP
       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
        2
    endoffight  
       2015-05-27 14:44:20 +08:00
    加一条root 指向根目录试试
    hobbyliu
        3
    hobbyliu  
    OP
       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
        4
    xjx0524  
       2015-05-27 14:50:20 +08:00
    把$document_root改成/home/hobby/service试试
    shiny
        5
    shiny  
       2015-05-27 14:52:56 +08:00
    nginx 配置没走到这个 location 吧。走到这个 location 如果有问题就是 50x 错误。 建议贴全 nginx 配置
    hobbyliu
        6
    hobbyliu  
    OP
       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
        7
    shiny  
       2015-05-27 15:02:40 +08:00
    @hobbyliu 没看到你 php 配置在哪了
    endoffight
        8
    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
        9
    fangjinmin  
       2015-05-27 15:31:08 +08:00
    很明显是php文件没有当作PHP解析。
    按照下面的步骤看一下,
    1,php-fpm的配置文件,看一下php-fpm的运行的user和group是否设置正确。
    2,重启php-fpm和nginx
    userlogin
        10
    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
        11
    userlogin  
       2015-05-27 15:40:52 +08:00
    ps. 如上条,如nginx根目录非/usr/share/nginx/html,请根据实际情况更换为nginx的根目录路径。
    Havee
        12
    Havee  
       2015-05-27 16:13:18 +08:00
    php-fpm 配置中的权限是否跟你 /home/hobby/service 的权限一致
    huigeer
        13
    huigeer  
       2015-05-27 16:24:32 +08:00
    看看nginx的error_log
    hzqim
        14
    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;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2354 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:04 · PVG 00:04 · LAX 08:04 · JFK 11:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.