Nginx 502 Bad Gateway Solution

Source: Internet
Author: User
Tags nginx reverse proxy

1. view the FastCGI process PS-Ef | grep 'php-fpm'. if the process is not started, start

2. Number of system files opened

Ulimit-N 4096
View the number of files opened by all processes
Lsof | WC-l
View the number of files opened by a process
Lsof-p pid | WC-l

I. FastCGI buffer settings are too small 

HTTP {
...
Fastcgi_buffers 8 16 K;
Fastcgi_buffer_size 32 K;
...
}

II. the Proxy Buffer settings are too small
If you are using nginx reverse proxy, if the header is too large, it exceeds the default 1 K, the above upstream sent too big header will be triggered (to put it bluntly, nginx will process external requests to the backend. If the header returned by the backend is too large, nginx will cause 502 if it cannot process it.

Server {
Listen 80;
SERVER_NAME *. lxy. Me;

Location /{

###############
Proxy_buffer_size 64 K;
Proxy_buffers 32 32 K;
Proxy_busy_buffers_size 128 K;
###############
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.
502 problems occur during installation and use. Generally, the default PHP-CGI process is five, which may be caused by insufficient phpcgi processes, you need to modify/usr/local/PHP/etc/php-fpm.conf to add the max_children value as appropriate. It is also possible that the max_requests value is insufficient. It must be noted that this configuration item occupies a large amount of memory. Please set it according to the server configuration. Otherwise, it may have a negative effect.

Iv. PHP Execution timeout
PHP Execution time-out. Modify/usr/local/PHP/etc/PHP. ini and change max_execution_time to 300.

5. nginx wait time timeout
The execution time of some PHP programs exceeds the nginx waiting time. You can add the FastCGI timeout time in the nginx. conf configuration file as appropriate.

HTTP {
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
......
}

Nginx 502 Bad Gateway Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.