【问题标题】:502 Bad Gateway Nginx after server restore服务器恢复后 502 Bad Gateway Nginx
【发布时间】:2018-01-20 07:12:15
【问题描述】:

所以我有一个 Ubuntu 服务器,我正在尝试从备份中恢复。当一个虚拟的 PHP 版本页面时,我得到 502 Bad Gateway。我试图解决什么问题?很多事情,包括这个相关问题的答案 - nginx 502 bad gateway

这是我在 Ubuntu 上的默认配置

server {
listen 80;
#   listen [::]:81 ipv6only=on default_server;

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

server_name localhost;

location ~* \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
        expires max;
}


location / {
    try_files $uri $uri.php $uri.php/$args /index.php?q=$uri&$args $uri/ =404;
    index index.php index.html index.htm;
    rewrite ^(.*)$ /$1.php;
}

location /phpmyadmin {
  index index.php;
    try_files $uri $uri/ =404;
    auth_basic "Admin Login";
    auth_basic_user_file /etc/nginx/pma_pass;
}

error_page 404 /404.html;

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

location ~ \.php$ {
    try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}


location ~ /\.ht {
    deny all;
}
}

编辑: 这是我认为日志的相关部分

2017/08/11 13:18:13 [error] 27759#0: *270 connect() failed (111: Connection refused) while connecting to upstream, client: 66.61.18.112, server: domain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8080/favicon.ico", host: "[IP ADDRESS]", referrer: "http://[IP ADDRESS]/phpmyadmin/index.php"

编辑 2:

这是我运行ps aux | grep php-fpm时的结果:

mre      21685  0.0  0.0  11756   932 pts/0    S+   10:42   0:00 grep --color=auto php-fpm
root     32721  0.0  1.0 269300 11168 ?        Ss   Aug11   0:30 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    
www-data 32724  0.0  0.4 269300  4540 ?        S    Aug11   0:00 php-fpm: pool www                                                       
www-data 32725  0.0  0.4 269300  4540 ?        S    Aug11   0:00 php-fpm: pool www 

编辑 3:

这是我的 php-fpm.conf 中未注释的内容

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /var/run/php5-fpm.pid

; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
error_log = /var/log/php5-fpm.log


;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ; 
;;;;;;;;;;;;;;;;;;;;

; To configure the pools it is recommended to have one .conf file per
; pool in the following directory:
include=/etc/php5/fpm/pool.d/*.conf

编辑 4:

这是我的 www.conf 中未注释的内容:

listen = 127.0.0.1:9000
listen.owner = www-data  
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

编辑 5:

我尝试转到 index.php 后的 nginx 日志:

[error] 29393#0: *23 connect() failed (111: Connection refused) while connecting to upstream, client: [Computer IP Address], server: [Subdomain.domain.com], request: "GET /favicon.ico HTTP/1.1", upstream:"http://127.0.0.1:8080/favicon.ico", host: [Server IP], referrer: "http://[Server IP]/index.php"

【问题讨论】:

  • 你在尝试什么 URL 路径? fastcgi_params 是否包含任何可能覆盖您上面设置的指令? (尝试将 include fastcgi_params; 放在其他 fastcgi 指令上方并重新启动 nginx)
  • 同样的问题,当我移动线路并重新启动 ngxinx 时出现 502
  • 您应该会在您的 nginx 错误日志中看到该 502 的条目。奇怪的是你不是。还要检查您的 PHP 日志。对不起,我不能提供更多帮助:(
  • 是否有目录/etc/php5/fpm/pool.d/,如果有,它包含哪些文件?似乎您没有默认池设置,或者该目录中的默认池配置错误。
  • 有一个目录包含www.conf

标签: php ubuntu nginx fastcgi


【解决方案1】:

所以我检查了我的 nginx 配置并发现了这些行:

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

然后我去了/etc/nginx/conf.d/*.conf,在servers.conf 文件中看到了一个我计划使用但不再使用的子域的配置,所以我将其删除。这是问题的很大一部分,因为 nginx 首先查看此配置,这就是错误包含此子域的原因。

至于 php 的问题,我将我的配置切换到 fastcgi_pass unix:/run/php/php7.0-fpm.sock;,这似乎已经解决了问题。

【讨论】:

  • 我只是想告诉你,我在看到这个答案之前发表了我的最后一条评论!我是如此接近!我有 3 台 Ngnix 生产服务器,我自己也面临许多这些问题。很高兴你解决了这个问题。
【解决方案2】:

尝试替换

fastcgi_pass unix:/var/run/php5-fpm.sock;

在你的 nginx 配置中

fastcgi_pass 127.0.0.1:9000;

因为如果您的 PHP-FPM 配置使用 TCP 而不是套接字(没关系)。

【讨论】:

    【解决方案3】:

    你必须改变这一行

    来自:

    listen = 127.0.0.1:9000
    

    到:

    listen = /var/run/php5-fpm.sock
    

    在您的 www.conf 文件中

    别忘了重启php。

    说明:

    如果你看一下你的 nginx 虚拟主机 fastcgi_pass 参数

    location ~ \.php$ {
        try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
    

    您会看到您将 php 文件的处理指向 php5-fpm 套接字,但在您的 www.conf 文件中,php 正在侦听端口 9000,这就是您应该将其更改为 /var/run/php5-fpm.sock 的原因

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 2021-09-11
      • 2017-05-10
      • 2022-07-14
      • 2014-06-28
      • 2015-07-25
      • 2017-01-06
      • 2023-03-20
      相关资源
      最近更新 更多