502 Bad Gateway Error Details and Solutions

1. What is a 502 Bad Gateway error?

502 Bad Gateway is an HTTP status code that indicates that the server, when acting as a gateway or proxy, attempted to access the next server, but that server returned an illegal response. Simply put, 502 is an error type code that indicates "Wrong gateway".

Picture [1]-502 Bad Gateway error details and solutions - Photon Flux | Professional WordPress repair service, global, fast response

2. Causes of 502 errors

502 errors are usually caused by several reasons:

First reason: DNS caching

If you do not turn on the VPN In the case of some restricted websites (e.g. Facebook), it can leave a local cache and cause the access to fail. In this case you can try to set the DOS window(of a computer) run ipconfig /flushdns command to flush the DNS cache.

Second reason: browser proxy settings

Check to see if the browser has proxy settings turned on, and confirm that the proxy is turned off.

Third reason: DNS hijacking

Even if you use a foreign DNS, it can be hijacked. Some devices can be accessed with VPN on, others cannot. In this case you can switch to another DNS server (e.g. Google DNS or OpenDNS).

Picture [2]-502 Bad Gateway error details and solutions - Photon Flux | Professional WordPress repair services, worldwide, rapid response

3. 502 Wrong HTTP Loop

The 502 error occurs during communication between a client (such as a Web browser) and a Web server as follows:

  1. Obtaining the IP address of a website: Translates a domain name into an IP address via DNS.
  2. Open an IP socket to connect to that IP address.
  3. Writes HTTP streams over sockets.
  4. Receives an HTTP response stream from the web server containing status codes.

When the client receives status code 502 in the last step, it displays "502 Bad Gateway" errorThe

4. Method of fixing 502 errors

Generic solutions

  1. Refresh the page: Press CTRL+F5 Force a page refresh, which will refetch the data from the server instead of reading it from the local cache.
  2. Clear Browser Cache: Clear your browser's cache file and revisit the page.
Picture [3]-502 Bad Gateway error details and solutions - Photon Flux | Professional WordPress repair service, worldwide, rapid response

Specific solutions

Client issues

  1. Check for ISP problems: If the 502 error occurs on all sites, it may be that your ISP equipment is malfunctioning or overloaded.
  2. Checking for internal network problems: If the 502 error occurs only on some sites, it may be due to equipment failure or overload on those sites, contact the webmaster to resolve the issue.

Server issues

  1. Viewing Nginx Logs: Check the Nginx log file (/var/log/nginx) for error messages such as upstream sent too big header while reading response header from upstream. It is possible that the buffer is set too small.
  2. Increase buffer size::
http {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k.
}
http {
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k.
}
http { fastcgi_buffers 8 16k; fastcgi_buffer_size 32k. }

3.Proxy Buffer Settings::

server {
listen 80; server_name *.example.com; server_name
server_name *.example.com;
server { listen 80; server_name *.example.com; location / {
proxy_buffer_size 64k; proxy_buffers 32 32k; location / {
proxy_buffers 32 32k; proxy_busy_buffers_size 128k;
proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_set_header
proxy_set_header Host $host; proxy_set_header X-Real-IP $host.
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
    listen 80; server_name *.example.com; server_name
    server_name *.example.com;

    server { listen 80; server_name *.example.com; location / {
        proxy_buffer_size 64k; proxy_buffers 32 32k; location / {
        proxy_buffers 32 32k; proxy_busy_buffers_size 128k;
        proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_set_header
        proxy_set_header Host $host; proxy_set_header X-Real-IP $host.
        proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
server { listen 80; server_name *.example.com; server_name server_name *.example.com; server { listen 80; server_name *.example.com; location / { proxy_buffer_size 64k; proxy_buffers 32 32k; location / { proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_set_header proxy_set_header Host $host; proxy_set_header X-Real-IP $host. proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

4.Increase the number of PHP-FPM processes::

; php-fpm.conf
pm.max_children = 512
pm.max_requests = 600
; php-fpm.conf
pm.max_children = 512
pm.max_requests = 600
; php-fpm.conf pm.max_children = 512 pm.max_requests = 600

5.Increase PHP execution time::

; php.ini
max_execution_time = 300
; php.ini
max_execution_time = 300
; php.ini max_execution_time = 300

6.Increase Nginx timeout::

http {
fastcgi_connect_timeout 300;
fastcgi_connect_timeout 300; fastcgi_send_timeout 300;
fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300.
}
http {
    fastcgi_connect_timeout 300;
    fastcgi_connect_timeout 300; fastcgi_send_timeout 300;
    fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300.
}
http { fastcgi_connect_timeout 300; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300. }

5. Nginx 502 Bad Gateway Error Causes and Solutions

Nginx 502 errors can be caused by a variety of reasons, usually due to problems with the back-end server. Here are some common causes and solutions:

FastCGI buffer set too small

Found in the Nginx logs upstream sent too big header error, which can be resolved by increasing the buffer size:

http {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k.
}
http {
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k.
}
http { fastcgi_buffers 8 16k; fastcgi_buffer_size 32k. }

Proxy buffer set too small

When using a reverse proxy, a header that is too large may result in a 502 error:

server {
listen 80; server_name *.example.com; server_name
server_name *.example.com;
server { listen 80; server_name *.example.com; location / {
proxy_buffer_size 64k; proxy_buffers 32 32k; location / {
proxy_buffers 32 32k; proxy_busy_buffers_size 128k;
proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_set_header
proxy_set_header Host $host; proxy_set_header X-Real-IP $host.
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
    listen 80; server_name *.example.com; server_name
    server_name *.example.com;

    server { listen 80; server_name *.example.com; location / {
        proxy_buffer_size 64k; proxy_buffers 32 32k; location / {
        proxy_buffers 32 32k; proxy_busy_buffers_size 128k;
        proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_set_header
        proxy_set_header Host $host; proxy_set_header X-Real-IP $host.
        proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
server { listen 80; server_name *.example.com; server_name server_name *.example.com; server { listen 80; server_name *.example.com; location / { proxy_buffer_size 64k; proxy_buffers 32 32k; location / { proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_set_header proxy_set_header Host $host; proxy_set_header X-Real-IP $host. proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

PHP-FPM process count is too low

modifications php-fpm.conf The document increases the number of processes:

pm.max_children = 512
pm.max_requests = 600
pm.max_children = 512
pm.max_requests = 600
pm.max_children = 512 pm.max_requests = 600

PHP execution timeout

modifications php.ini Documentation to increase execution time:

max_execution_time = 300
max_execution_time = 300
max_execution_time = 300

Nginx wait timeout

Increase the timeout in the Nginx configuration:

http {
fastcgi_connect_timeout 300;
fastcgi_connect_timeout 300; fastcgi_send_timeout 300;
fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300.
}
http {
    fastcgi_connect_timeout 300;
    fastcgi_connect_timeout 300; fastcgi_send_timeout 300;
    fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300.
}
http { fastcgi_connect_timeout 300; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300. }

Other solutions

  • Check if the number of PHP-FPM processes is sufficient:
netstat -anpo | grep "php-cgi" | wc -l
netstat -anpo | grep "php-cgi" | wc -l
netstat -anpo | grep "php-cgi" | wc -l

probe request_terminate_timeout Setting:

; php-fpm.conf
request_terminate_timeout = 0
; php-fpm.conf
request_terminate_timeout = 0
; php-fpm.conf request_terminate_timeout = 0

Most of the 502 Bad Gateway error problems can be solved effectively by the above methods. Hope this article can help you better understand and solve 502 errors, and improve the stability and user experience of your website.

Picture [4]-502 Bad Gateway error details and solutions - Photon Flux | Professional WordPress repair services, worldwide, fast response

Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by Harry
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments