【问题标题】:Setting Up PHP behind Nginx with FastCGI使用 FastCGI 在 Nginx 后面设置 PHP
【发布时间】:2014-06-24 00:07:08
【问题描述】:

我正在尝试在运行 nginx 作为反向代理的服务器上设置 PHP。我在配置文件中有以下指令:

location ~ \.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
}

在浏览器上打开任何 php 脚本时都会显示 500 Internal server error。如果我完全注释掉这个指令,浏览器会抛出一个对话框,提供下载 php 脚本的选项。可以访问与 php 脚本位于同一目录中的所有其他文件,例如图像文件等。哪里有问题?

编辑:配置文件

upstream the_server {
server localhost:8000;
}

server {
    root    /var/www/linux-dash-master;
    listen  80;
    client_max_body_size    4G;
    keepalive_timeout       5;
    log_format timed_combined '$sent_http_x_username - $remote_addr - [$time_local]  '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" - '
    '$request_time $upstream_response_time $pipe';
    access_log  /var/log/nginx/named.access.log timed_combined;
    error_log  /var/log/nginx/named.error.log debug;
    location /linux-dash-master {
            alias /var/www/linux-dash-master;
            index index.html index.php;
    }
    # Pass PHP requests on to PHP-FPM using sockets
    location ~ /linux-dash-master/.*\.php(/|$) {
            alias /var/www/linux-dash-master;
            #fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_intercept_errors on;
            if ($fastcgi_script_name ~ /linux-dash-master(/.*\.php)$) {
                    set $valid_fastcgi_script_name $1;
            }
            fastcgi_param SCRIPT_FILENAME /var/www/linux-dash-master$valid_fastcgi_script_name;
            #fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            try_files $uri $uri/ /index.php?$args;
            #include fastcgi_params;
      }
      location /static/ {
          alias   /path/to/staticfiles/;
      }
      location / {
          proxy_pass          http://the_server;
          proxy_set_header    Host            $host;
          proxy_set_header    X-Real-IP       $remote_addr;
          proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header    X-Remote-User-Name   $remote_user;
      }

}

【问题讨论】:

  • 你的标题说的是fast cgi,但你的设置是fpm,你用的是哪个?
  • 读取 nginx error.log!
  • 8000 端口是干什么用的,什么时候传递给 php 或 8000?
  • @Mohammad AbuShady 8000 是服务器侦听的端口。 gunicorn 进程在此端口上启动如下 'gunicorn wsgi --bind=127.0.0.1:8000 --daemon'
  • 所以如果你最后有.php,你会传递给php,还有什么东西会传递给gunicorn?

标签: nginx fastcgi php


【解决方案1】:

检查/etc/php5/fpm/pool.d/www.conf是否存在?

如果是这样,请检查其中,查找以listen 开头的行,它是指向文件还是端口?

请注意,以; 开头的行不算数

【讨论】:

  • 它是“listen = /var/run/php5-fpm.sock”。我还尝试使用“listen = 127.0.0.1:9000”并适当地更改配置文件。还是不行。
  • 检查 nginx 日志文件,同时确保 php 服务器确实在运行
  • 我在使用 Inspect Element 功能时注意到的另一件事是,所有调用 php 脚本的 GET 请求都返回一个 0kb 大小的 HTML 页面,而不是返回 JSON 响应。
  • 500 内部服务器错误现已消失。 php 服务器现在正在运行。但是在处理 php 脚本时似乎仍然存在问题。事实上,打印“hello world”的一个非常简单的 php 脚本也会显示空白页。有什么想法吗?
  • 你需要检查 nginx 日志,检查 /var/log/nginx/error.log 或任何错误文件名
猜你喜欢
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2012-06-06
  • 2012-12-16
  • 2016-11-08
  • 2018-08-06
相关资源
最近更新 更多