【问题标题】:nginx php-fpm 502 Bad Gatewaynginx php-fpm 502 错误网关
【发布时间】:2021-05-23 19:54:46
【问题描述】:

我使用 Ired 邮件和 2 个网站非常成功地运行 Ubuntu 服务器 20.04,其中一个使用 WordPress。

我想安装 Nextcloud,为此我必须重新安装 php-fpm 以生成 php7.4-fpm.sock。在 Nextcloud 工作后,但我的其他网站停止工作并出现错误“502 Bad Gateway”。

至少可以说,我很困惑!

我按照这篇文章安装 Nextcloud 并按照说明设置启用站点的 .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

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

【问题讨论】:

    标签: php nginx bad-gateway


    【解决方案1】:

    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 套接字。

    listen = 127.0.0.1:9000
    

    接下来在 nginx 中你可以根据 fpm 配置指定fastcgi_pass。如果您使用 Unix Socket,那么您所有的网站,包括 Nextcloud 都必须使用 Unix Socket。

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

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

    fastcgi_pass 127.0.0.1:9000;
    

    【讨论】:

    • 非常感谢,现在一切正常!而且我学到了一些新东西:)
    猜你喜欢
    • 2011-04-06
    • 2012-04-17
    • 2011-03-12
    • 1970-01-01
    • 2019-06-07
    • 2012-02-05
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多