为什么我无法让 PHP-FPM 与 CentOS 上的 nginx 一起工作?

为什么我无法让 PHP-FPM 与 CentOS 上的 nginx 一起工作?

我感觉我肯定错过了眼前的某些东西,但情况就是这样。502 Bad Gateway每次我尝试从浏览器访问.php文件时,我的 nginx 服务器都会继续返回错误(例如,subdomain1.example.com/test.phpsubdomain2.example.com/test2.php等)。

注意:我尝试root从不同的块中删除嵌套的指令location,如下所述这个答案,然后重新加载/重新启动 nginx,但没有帮助。

这是nginx.conf文件:

user nginx nginx;
worker_processes 5;
pid /var/run/nginx.pid;

events {
  worker_connections 2048;
}

http {
  ##
  # Basic Settings
  ##

  sendfile on;

  keepalive_timeout 5;

  types_hash_max_size 2048;

  include /etc/nginx/mime.types;
  include /etc/nginx/fastcgi.conf;
  default_type application/octet-stream;
  #default_type text/html;

  ## Detect when HTTPS is used
  map $scheme $fastcgi_https {
    default off;
    https on;
  }

  ##
  # Logging Settings
  ##
  access_log /var/log/nginx/access.log;

  error_log /var/log/nginx/error.log notice;

  rewrite_log on;

  log_format main '$remote_addr - $remote_user [$time_local] $request "$status" $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';


  ##
  # Gzip Settings
  ##
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 5;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  upstream fpm_backend {
   #server 127.0.0.1:9000; # backend server:port address
   server unix:/var/run/php-fpm.sock;
  }

  ##
  # Virtual Host Configs
  ##

  #
  # Server 1
  #
  server {
    listen     80 default_server;
    server_name  subdomain1.example.com;
    root   /usr/share/nginx/html;

    location / {
      index  index.html index.htm;
    }

    error_page  404        /404.html;
    location = /404.html {
      root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
      try_files $uri =404; # if reference to php executable is invalid return 404
      expires off; # no need to cache php executable files
      fastcgi_read_timeout 600;
      fastcgi_pass fpm_backend; # configured in nginx.conf
      fastcgi_keep_conn on; # use persistent connects
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
  }


  #
  # Server 2
  #
  server {
    listen     80;
    listen     443 ssl;
    server_name subdomain2.example.com;
    root   /sites/subdom2;
    ssl_certificate   ssl/ex-wildcard.crt;
    ssl_certificate_key ssl/ex-wildcard.key;
    ssl_crl       ssl/ex_bundle-g2-g1.crt;

    location / {
      index index.php;
    }

    error_page  404        /404.html;
    location = /404.html {
      root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }

    location /content/ {
      root   /sites/subdom2;
      ## mp4 streaming
      mp4;

      secure_link $arg_a,$arg_b;

      secure_link_md5 ABCDEFGHIJK$uri$arg_b$remote_addr;

      if ($secure_link = "") {
        return 403;
      }

      if ($secure_link = "0") {
        return 403;
      }
    }

    location /videos/ {
      ## mp4 streaming
      mp4;

      secure_link $arg_a,$arg_b;

      secure_link_md5 ABCDEFGHIJK$uri$arg_b$remote_addr;

      if ($secure_link = "") {
        return 403;
      }

      if ($secure_link = "0") {
        return 403;
      }
    }

    location /otherdir/content/ {
      root   /sites/subdom2/;
      ## mp4 streaming
      mp4;

      secure_link $arg_a,$arg_b;

      secure_link_md5 ABCDEFGHIJK$uri$arg_b$remote_addr;

      if ($secure_link = "") {
        return 403;
      }

      if ($secure_link = "0") {
        return 403;
      }
    }

    location ~ \.php$ {
      try_files $uri =404; # if reference to php executable is invalid return 404
      expires off; # no need to cache php executable files
      fastcgi_read_timeout 600;
      fastcgi_pass fpm_backend; # configured in nginx.conf
      fastcgi_keep_conn on; # use persistent connects
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
  }
}

请随时索取任何可能有助于解决此问题的其他信息。

答案1

这可能很明显,但您的“fastcgi_pass fpm_backend”可能是问题所在。

我发现 Unix 套接字的工作起来更加麻烦,权限之类的事情很重要。

尝试使用 http 套接字而不是 unix。以下是我在服务器上执行此操作的方法。

upstream php56-fpm {
  server 127.0.0.1:9000;
}

PHP 位置内部

fastcgi_pass php56-fpm;

您可以下载我的完整配置文件这里

相关内容