Nginx Newspaper 504 Gateway Timeout error Resolution __nginx

Source: Internet
Author: User
Tags fpm php script vps nginx server
Reprint article Source: http://www.111cn.net/sys/nginx/90669.htm (if invasion delete)

Nginx Newspaper 504 Gateway Timeout error caused, one is the file configuration problem, the other is related to the length of processing, and finally there may be insufficient resources, the following we look at.

Explained as follows:

Recently in the work, the need to do Excel import function, because of the Excel data is more, and our server program needs to check the content of the data, will call a lot of external service interface, so no suspense import Excel interface call more than a minute, and error: 504 Gateway Timeout Here are two ways to solve this problem:

1. Optimize business code

An interface called for more than a minute, there must be a place to optimize, to see whether the database or interface calls are reasonable, whether the request can be merged.

2. Modify the Nginx server configuration

If it is not optimized, you can increase the Nginx timeout time.
See if the time meets the requirements, three parameters in the Nginx.config:

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;

The units above are seconds.

If you use a nginx agent, you can add it in the Block:

Proxy_connect_timeout 300s;
Proxy_send_timeout 300s;
Proxy_read_timeout 300s;

Become:

Location/foo {
Proxy_pass Http://xxx.xxx.xxx.xxx:8080/foo;
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_connect_timeout 300s;
Proxy_send_timeout 300s;
Proxy_read_timeout 300s;
Access_log/var/log/nginx/access.foo.log main;
Error_log/var/log/nginx/error.foo.log;
}

If it doesn't work out, we'll see.

  from the error code can be basically determined to be independent of the nginx itself, mainly submitted to the php-fpm of the request is not the correct feedback resulting, in general, when the dynamic request, Nginx will directly transfer the request to PHP-FPM, and PHP-FPM php-cgi process to deal with the relevant requests, and then return in turn, and finally by nginx the results back to the client browser, but I this VPS is currently running a pure PHP application content, in fact, all users of the request are PHP requests, some time-consuming, The php-cgi process is always full, and PHP-FPM's own configuration file only opens up to 10 groups of php-cgi processes, so that a little more online users can cause the request to fail properly.
  
  Probably analyzed the reason, it is easier to do the following, first of all, to change the PHP-FPM configuration:
  
  Max_ Children from the previous 10 to now 30, so that there is sufficient php-cgi process can be used;
  change request_terminate_timeout from previous 0s to 60s, so php-cgi process The timeout for processing a script is 60 seconds, preventing the process from being suspended and increasing efficiency.
  
  Then change several nginx configuration items to reduce the number of FASTCGI requests and try to maintain buffers unchanged:
  
  Fastcgi_ Buffers from 4 64k to 2 256k;
  Fastcgi_buffer_size changed from 64k to 128K;
  fastcgi_busy_buffers_size from 128K to 256 K;
  Fastcgi_temp_file_write_size changed from 128K to 256K.
  
  OK, reload php-fpm and Nginx configuration, test again, so far two weeks time no longer appear 504 Gateway time-out situation, is achieved effect.  

In addition, the default static processing of PHP-FPM can make the process of php-cgi to occupy memory for a long time and cannot be released, which is also one of the causes of Nginx error, so can change the PHP-FPM processing mode to Apache mode.
<value name= "Style" >apache-like</value>

From the changes completed to the present test shows that the effect of the above method is still very obvious, and did not find a Nginx502 bad gateway or 504 Gateway time-out error. Of course, if your VPS or server performance is good enough to be based on the specific circumstances do not have to make unnecessary changes.


Instance


To my current server for example CPU is Ben 1.5G, Memory 1gb,centos system, the visitor is about 50 people around the same time online.

But the people on the line need to request php-cgi to do a lot of information processing, so I set the nginx.conf to:
Fastcgi_connect_timeout 300s;
Fastcgi_send_timeout 300s;
Fastcgi_read_timeout 300s;
Fastcgi_buffer_size 128k;
Fastcgi_buffers 8 128k; #8 128
Fastcgi_busy_buffers_size 256k;
Fastcgi_temp_file_write_size 256k;
Fastcgi_intercept_errors on;
The main setting here is the first three, which is
Fastcgi_connect_timeout 300s;
Fastcgi_send_timeout 300s;
Fastcgi_read_timeout 300s;
This set the php-cgi connection, send and read time, 300 seconds enough to use, so my server rarely appear 504 Gateway time-out this error. The key is the php-fpm.conf setting, which leads directly to 502 bad Gateway and 504 Gateway time-out.

Now let's take a closer look at some of the key parameters of php-fpm.conf:
Php-fpm.conf has two critical parameters, one is "Max_children" and the other is "Request_terminate_timeout"
The value of my two settings is "40″, one is" 900″, but this value is not universal, but it needs to be calculated by itself.
The method of calculation is as follows:
If your server performance is good enough and your broadband resources are sufficient, PHP scripts do not have loops or bugs, you can set the "request_terminate_timeout" to 0s directly. The meaning of 0s is to allow php-cgi to carry on without any time limit. And if you can't do this, which means that your php-cgi may be a bug, or that your broadband is not enough or that other reasons cause your php-cgi to be suspended, then it's recommended that you assign a value to "Request_terminate_timeout", This value can be set according to the performance of your server. Generally the better performance you can set the higher, 20 minutes-30 minutes can be. Because my server PHP script needs to run for a long time, and some may be more than 10 minutes so I set 900 seconds, so that will not cause php-cgi to die and appear 502 Bad Gateway this error.
And how is the value of "Max_children" calculated? This value in principle is the bigger the better, the php-cgi process will be processed more quickly, the queue of requests will be very little. Set "Max_children" also need to set according to the performance of the server, generally a server under normal circumstances of each php-cgi memory consumed in about 20M, so my "Max_children" I set to 40, 20m*40= 800M means that at peak time all php-cgi are consumed within 800M, less than my valid memory 1Gb. And if my "max_children" set smaller, such as 5-10, then php-cgi will be "very tired", processing speed is also very slow, waiting for a long time. If a long time did not get processing requests will appear 504 Gateway time-out this error, and is processing a very tired of those php-cgi if encountered problems will appear 502 bad Gateway this error.
The fastcgi settings are added to the server {}.
After the change, if you start Nginx prompt:
Nginx: [Emerg] unknown directive "fastcgi_connect_timeout" In/home/chen/workspace/jamy/nginx.conf:2
Errors like this may not be removed from the front full-width spaces.

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.