首页 > 解决方案 > Ngnix + Apache/php-frm 502 网关错误

问题描述

尝试在两个不同的服务器上设置 Nginx 作为反向代理和 Apache 2.4.37 / php-frm 7.4.7。收到 502 bad gateway 错误消息,这是 Nginx 服务器的日志。

2021/08/16 15:40:27 [错误] 6550#0: *18 connect() 在连接到上游时失败(111:连接被拒绝),客户端:192.168.0.100,服务器:aaa.bbb,请求:“GET /wp-admin/admin.php?page=followup-emails HTTP/1.1”,上游:“fastcgi://10.0.0.35:9001”,主机:“aaa.bbb”,推荐人:“https://aaa. bbb/wp-admin/"

Nginx 上的配置:10.0.0.41

server {
listen 443;
client_header_buffer_size 64k;
large_client_header_buffers 4 64k;  
server_name aaa.bbb;



#FASTCGI
 set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $skip_cache 1;
}
if ($query_string != "") {
    set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|/wp-admin/tools.php|index.php|sitemap(_index)?.xml") {
    set $skip_cache 1;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}


location / {
            proxy_cache proxycache;
        proxy_buffering on;

        proxy_cache_bypass  $http_cache_control;
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_no_cache $http_pragma $http_authorization $cookie_nocache $arg_nocache;
        proxy_pass            http://10.0.0.35:80;
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_set_header      Host $host;
        proxy_set_header      X-Real-IP $remote_addr;
        proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header      X-Forwarded-Proto $scheme;
        add_header Cache-Control "public";
        add_header  X-Real-IP  $remote_addr;
        proxy_set_header    HTTP_X_FORWARDED_PROTO $scheme;
}

       location ~ \.php$ {
        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                  
            add_header rt-Fastcgi-Cache $upstream_cache_status;
            include /etc/nginx/fastcgi_params;

            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param SCRIPT_FILENAME /var/www/staging/public_html$fastcgi_script_name;

            # Use the upstream for php5-fpm that we defined in nginx.conf
            fastcgi_pass 10.0.0.35:9001;
            fastcgi_index index.php;
            fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;
            fastcgi_cache phpcache_staging;
            fastcgi_cache_valid  60m;
      }

}



server {
  listen 80;

  server_name aaa.bbb;
  return 301 https://$host$request_uri;
}

在 Apache 上配置,10.0.0.35

<VirtualHost *:80>
   ServerName aaa.bbb
   DocumentRoot /var/www/staging/public_html
   LimitRequestFieldSize 1000000000
   LimitRequestLine 1000000000

<Directory /var/www/staging/public_html>
        AllowOverride All
</Directory>
        <IfModule proxy_fcgi_module>
        <Proxy "fcgi://127.0.0.1:9001/" enablereuse=on max=50>
         ProxySet timeout=1800
        </Proxy>
        <FilesMatch "\.php$">
        <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:fcgi://127.0.0.1:9001"
        </If>
        </FilesMatch>
        </IfModule>

</VirtualHost>

网络统计-an | grep :9001 tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9001 127.0.0.1:38312 已建立

有什么线索吗?

标签: phpapachenginx-reverse-proxy

解决方案


推荐阅读