Nginx 502 Bad Gateway Error Causes and Solutions

Nginx 502 Bad Gateway Error Causes and Solutions

I have encountered the Nginx 502 Bad Gateway error several times. I’ll make a note of it here as a reminder.


There are many situations in which 502 errors may occur. Let's talk about each situation separately.

1. The fastcgi buffer is set too small

When an error occurs, first look for the nginx log file in the directory /var/log/nginx. The following error is found in the log.

2013/01/17 13:33:47 [error] 15421#0: *16 upstream sent too big header while reading response header from upstream

After checking the information, I found that there is a bug in the nginx buffer, and the pages on our website may consume too much buffer.

I searched for a solution online and saw a method to increase the buffer on a foreign website, which completely solved the Nginx 502 Bad Gateway problem. Here’s how:

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

Please increase the above two configuration items according to the situation of the server and website.

2. The proxy buffer is set too small

If you are using nginx reverse proxy, if the header is too large, exceeding the default 1k, it will trigger the upstream sent too big header mentioned above (to put it simply, nginx sends the external request to the backend for processing, and the header returned by the backend is too large, which nginx cannot process, resulting in 502.

server {
    listen 80;
    server_name *.lxy.me;
    location / {
################Add these 3 lines proxy_buffer_size 64k;
       proxy_buffers 32 32k;
       proxy_busy_buffers_size 128k;
###############Add these 3 lines proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
............
}

3. The default number of php-cgi processes is too small

If a 502 error occurs during installation and use, it is usually because the default number of php-cgi processes is 5. 502 may be caused by insufficient phpcgi processes. You need to modify /usr/local/php/etc/php-fpm.conf and increase the max_children value appropriately. It is also possible that the max_requests value is not enough. It should be noted that these two configuration items take up a lot of memory, please set them according to the server configuration. Otherwise it may have the opposite effect.

4. PHP execution timeout

PHP execution timeout, modify /usr/local/php/etc/php.ini and change max_execution_time to 300

5. nginx waiting time timeout

The execution time of some PHP programs exceeds the waiting time of Nginx. You can appropriately increase the timeout time of FastCGI in the nginx.conf configuration file.

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Troubleshooting the cause of 502 bad gateway error on nginx server
  • 4 Common Causes and Solutions for Nginx 502 Bad Gateway Error
  • Deep Dive: How to fix the Nginx 502 Bad Gateway error
  • Causes and solutions for front-end exception 502 bad gateway

<<:  JavaScript single thread and asynchronous details

>>:  The use of anchor points in HTML_PowerNode Java Academy

Recommend

Incredible CSS navigation bar underline following effect

The first cutter in China github.com/chokcoco Fir...

In-depth explanation of various binary object relationships in JavaScript

Table of contents Preface Relationships between v...

Native JS to implement the aircraft war game

This article example shares the specific code of ...

Detailed tutorial on configuring nginx for https encrypted access

environment: 1 CentOS Linux release 7.5.1804 (Cor...

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

Practical notes on installing Jenkins with docker-compose

Create a Directory cd /usr/local/docker/ mkdir je...

JavaScript implementation of magnifying glass details

Table of contents 1. Rendering 2. Implementation ...

Detailed tutorial on installing and using Kong API Gateway with Docker

1 Introduction Kong is not a simple product. The ...

A colorful cat under Linux

Friends who have used the Linux system must have ...

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

Details of using vue activated in child components

Page: base: <template> <div class="...

MySQL common backup commands and shell backup scripts sharing

To back up multiple databases, you can use the fo...