【问题标题】:Nginx with PHP5-FPM -- .php files giving blank screens带有 PHP5-FPM 的 Nginx - .php 文件提供空白屏幕
【发布时间】:2013-01-26 16:23:40
【问题描述】:

我正在努力让 nginx 启动并使用 php5-fpm 运行。我觉得这是我忽略了一个小细节,所以我休息了几天,然后又回来了。今晚又搞砸了几个小时,但无济于事。

无论如何,问题是:我已经启动并运行了 nginx。它似乎可以正确地提供网页。例如,http://www.shidenadvanced.com 的基本网站服务就很好。但是,我拥有的位于http://www.shidenadvanced.com/test.php 的 php 测试返回空白。以前它作为 502 错误网关返回。

通过我的研究,我了解到这意味着它无法通过 php-fpm 正确路由它。不是 100%。

这是我的 /sites-available/config:

server {
    server_name www.shidenadvanced.com shidenadvanced.com;
    access_log /srv/sites/shidenadvanced/logs/access.log;
    error_log /srv/sites/shidenadvanced/logs/error.log;

    root /srv/sites/shidenadvanced/www;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    #location ~ \.php$ {
    #    try_files $uri =404;
    #    include /etc/nginx/fastcgi_params;
    #    fastcgi_pass unix:/var/run/php-fpm5.sock;
    #    fastcgi_index index.php;
    #    fastcgi_param SCRIPT_FILENAME /srv/sites/shidenadvanced/www$fastcgi_script_name;
    #}

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

除此之外,我已经单独保留了大部分设置。不完全确定发生了什么。有人能解释一下吗?

【问题讨论】:

  • 您的错误日志中有任何内容吗? /var/run/ 中是否也存在套接字?
  • 错误日志的输出: 2013/01/26 05:02:52 [crit] 3647#0: *20 connect() to unix:/var/run /php-fpm5.sock 连接到上游时失败(2:没有这样的文件或目录),客户端:74.110.180.71,服务器:www.shidenadvanced.com,请求:“GET /test.php HTTP/1.1”,上游: "fastcgi://unix:/var/run/php-fpm5.sock:", host: "www.shidenadvanced.com" -- 这是最近的一行。但是那个 php-fpm5.sock 确实存在。 /var/run/php5-fpm.sock,即
  • 愚蠢的问题,只是为了确定:你提到你已经启动并运行了 nginx,你是否也启动(和配置)了 php-fpm 守护进程?
  • 我很确定我做到了。正如我所提到的,我有文件 /var/run/php5-fpm.sock 。我还查看了 /etc/php5/fpm/pool.d/www.conf 以确保它正在侦听该套接字。当我输入“service php5-fpm status”时,它返回它正在运行。我试过多次重启 php5-fpm 和 nginx。
  • 你解决了吗?

标签: nginx webserver php


【解决方案1】:

试试这个。我对您处理 fastcgi 的方式进行了一些更改

server {
server_name www.shidenadvanced.com shidenadvanced.com;
access_log /srv/sites/shidenadvanced/logs/access.log;
error_log /srv/sites/shidenadvanced/logs/error.log;

root /srv/sites/shidenadvanced/www;
index index.php index.html index.htm;

location / {
    try_files $uri $uri/ /index.html;
}

# use fastcgi for all php files
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;
}

location ~ /\.ht {
    deny all;
}
}

【讨论】:

  • 在将其更改为此配置后,它会给我一个“502 Bad Gateway”响应。我最初是在这一点上,并认为这意味着它根本找不到 PHP 服务(尽管我可能会弄错)。 shidenadvanced.com/test.php
  • 我花了一天半的时间使用 php5-fpm 出现一个新的空白页面,这个问题突然出现在我面前。将此行添加到我的 php 位置语句中解决了我的问题fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
【解决方案2】:

我个人更喜欢socket方案:

fastcgi_pass unix:/path/tp/myfirst.socket;

而不是

fastcgi_pass 127.0.0.1:9000;

但您也需要主机的 fpm 配置:

[hak-rentrisch_de]
listen = /path/tp/myfirst.socket
listen.owner = hostuser
listen.group = hostgroup
listen.mode = 0666

listen.backlog = -1
listen.allowed_clients = 127.0.0.1

user = hostuser
group = hostgroup

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

php_admin_value[include_path] = .:/var/www/libs 
[...]

亲切的问候

【讨论】:

  • 我已经在我的输出中使用了一个套接字实现。您在这里提到的第二个文件是什么?
  • 第二个文件是池配置。查看 nginx 配置,它包含一些池目录
猜你喜欢
  • 2014-08-17
  • 2015-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 2017-04-07
  • 2015-11-17
相关资源
最近更新 更多