Nginx PHP-FPM 502 Bad Gateway

3

我之前在Ubuntu server 20.04上成功运行了Ired mail和两个网站,其中一个是WordPress。

我想安装Nextcloud,为此我必须重新安装php-fpm以生成php7.4-fpm.sock。 这样做后,Nextcloud可以正常工作,但我的其他网站停止工作,并显示“502 Bad Gateway”错误。

可以说,我非常困惑!

我遵循了这篇文章中的说明来安装Nextcloud,并根据说明设置了sites-enabled .conf文件:https://www.linuxbabe.com/ubuntu/install-nextcloud-ubuntu-20-04-nginx-lemp-stack/amp

我认为我理解了.conf文件在127.0.0.1:XXXX上监听并且现在监听php7.4-fpm.sock吗?

这是我在重新安装php-fpm后编写的网站.conf文件:

#
# Note: This file must be loaded before other virtual host config files,
#
# HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name SOMEWEBSITE www.SOMEWEBSITE;

    error_log /var/log/nginx/localhost.error_log info;

    root /var/www/SOMEWEBSITE/html;
    index index.php index.html;

    include /etc/nginx/templates/misc.tmpl;
    include /etc/nginx/templates/ssl.tmpl;
    include /etc/nginx/templates/iredadmin.tmpl;
    include /etc/nginx/templates/roundcube.tmpl;
    include /etc/nginx/templates/sogo.tmpl;
    include /etc/nginx/templates/netdata.tmpl;
    include /etc/nginx/templates/php-catchall.tmpl;
    include /etc/nginx/templates/stub_status.tmpl;
        
    location / {
        try_files $uri $uri/ /index.php?q=$uri$args;
    }
        
    # PHP handling
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    ssl_certificate /etc/letsencrypt/live/SOMEWEBSITE/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/SOMEWEBSITE/privkey.pem; # managed by Certbot

}
# Redirect http to https
server {
    listen 80;
    listen [::]:80;
    server_name SOMEWEBSITE www.SOMEWEBSITE;

    return 301 https://$host$request_uri;
}


我已经检查了php7.4-fpm.sock文件的权限。
ll /var/run/php/ | grep php

-rw-r--r--  1 root     root        3 May 22 21:13 php7.4-fpm.pid
srw-rw----  1 www-data www-data    0 May 22 21:13 php7.4-fpm.sock=
lrwxrwxrwx  1 root     root       30 May 22 21:13 php-fpm.sock -> /etc/alternatives/php-fpm.sock=

我觉得它看起来还不错。

这是日志文件:

2021/05/23 20:32:52 [error] 43596#43596: *305 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xxx.xxx, server: SOMEWEBSITE, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9999", host: "SOMEWEBSITE"
2021/05/23 20:32:53 [info] 43596#43596: *305 client xx.xx.xxx.xxx closed keepalive connection

有什么想法吗?需要更多信息吗?谢谢您的关注。

2个回答

3

PHP-FPM可以使用两种方法来接受FastCGI请求,一种是使用TCP Socket,另一种是使用Unix Socket。

您可以在php-fpm配置中指定它,在Ubuntu中,配置文件位于/etc/php/7.4/fpm/pool.d/www.conf,并检查listen配置。

如果要使用Unix Socket,请使用以下配置。

listen = /run/php/php7.4-fpm.sock

对于TCP Socket。
listen = 127.0.0.1:9000

在nginx中,您可以根据fpm配置指定fastcgi_pass。如果您使用Unix套接字,则所有网站(包括Nextcloud)都必须使用Unix套接字。

fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

如果您使用TCP Socket,您需要更改Nextcloud的nginx配置以从TCP Socket传递。

fastcgi_pass 127.0.0.1:9000;

非常感谢,现在一切工作正常!而且我学到了新的东西 :) - Lee Bolton

0
我也遇到了这个问题。问题是php-fpm没有监听端口9999(在我的情况下,我使用端口9999)。为了使/mail/工作,您需要更改以下配置文件。将ip:port更改为socket。
  • /etc/php/fpm/pool.d/www.conf
监听 = /var/run/php/php7.2-fpm.sock
  • /etc/nginx/templates/fastcgi_php.tmpl
快速通用网关接口(FastCGI)传递 unix:/var/run/php/php7.2-fpm.sock;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接