为什么我不能在CentOS上使用PHP-FPM与nginx一起工作?

我觉得我一定会错过在我面前的东西,但这是情况。 每当我尝试从浏览器访问.php文件(例如, subdomain1.example.com/test.phpsubdomain2.example.com/test2.php等)时,我的nginx服务器将继续返回502 Bad Gateway错误。 。

注意:我已经尝试从这个答案中描述的不同location块中删除嵌套的root指令,然后重新加载/重新启动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; } } } 

随意请求任何可能有助于解决这个问题的其他信息。

这可能是相当明显的,但你的“fastcgi_pass fpm_backend”可能是问题。

我发现Unix套接字可以更加繁琐的工作,像权限是很重要的。

尝试一个http套接字而不是unix。 以下是我如何在我的服务器上执行此操作。

 upstream php56-fpm { server 127.0.0.1:9000; } 

在PHP位置内

 fastcgi_pass php56-fpm; 

你可以在这里下载我的完整configuration文件。