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](https://www.361sale.com/wp-content/uploads/2024/07/2024070209583068.png)
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](https://www.361sale.com/wp-content/uploads/2024/07/2024070210060175.png)
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:
- Obtaining the IP address of a website: Translates a domain name into an IP address via DNS.
- Open an IP socket to connect to that IP address.
- Writes HTTP streams over sockets.
- 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
- 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. - 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](https://www.361sale.com/wp-content/uploads/2024/07/2024070210083722.jpg)
Specific solutions
Client issues
- Check for ISP problems: If the 502 error occurs on all sites, it may be that your ISP equipment is malfunctioning or overloaded.
- 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
- 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. - 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_nameserver_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_headerproxy_set_header Host $host; proxy_set_header X-Real-IP $host.proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IPproxy_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.confpm.max_children = 512pm.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.inimax_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_nameserver_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_headerproxy_set_header Host $host; proxy_set_header X-Real-IP $host.proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IPproxy_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 = 512pm.max_requests = 600pm.max_children = 512 pm.max_requests = 600pm.max_children = 512 pm.max_requests = 600
PHP execution timeout
modifications php.ini
Documentation to increase execution time:
max_execution_time = 300max_execution_time = 300max_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 -lnetstat -anpo | grep "php-cgi" | wc -lnetstat -anpo | grep "php-cgi" | wc -l
probe request_terminate_timeout
Setting:
; php-fpm.confrequest_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](https://www.361sale.com/wp-content/uploads/2024/07/2024070210092182.png)
Link to this article:https://www.361sale.com/en/12386
The article is copyrighted and must be reproduced with attribution.
No comments