欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

504 gateway time-out nginx nginx apache How to Fix 504 Gateway Timeout using Nginx (502 bad gateway 好像也这样处理) 自己亲自做的 有大用 有大大用 有大大大用

关于502 bad gateway下面的方法 好像对,又好像不对

502 bad gateway

 nginx 错误日志 

upstream prematurely closed connection while reading response header from upstream 也是下面的处理方法(下面的处理方法再改 keepalive_timeout  60;keepalive_timeout  120;好像又不能改,改了后好像整个nginx服务器会有问题,打不开网站 )


已亲自做的 有大用

经搜索 是要添加 

proxy_connect_timeout 600s;

proxy_send_timeout   600;

proxy_read_timeout   600;

send_timeout   600;

到 nginx.conf 又发觉到 是添到虚拟主机的各个nginx.conf

image.png

到 /www/wdlinux/nginx-1.8.1/conf/naproxy.conf 看看 果然有关于上面 四项的(其实我只看到三项,那就改三项吧) 之后 nginx 要重启一下吧


image.png



image.png

It is very common to see a 504 Gateway Timeout error using Nginx webserver. This timeout error is generated often by a number of reasons on the backend connection that is serving content. To fix 504 Gateway Time-out, you will have to figure out what configuration are you using.

How you might see the 504 Gateway Timeout error

Different websites may customize the 504 gateway timeout error message. Here are the most common 504 error messages:

  • “504 Gateway Timeout”

  • “504 Gateway Time-Out”

  • “504 Gateway Timeout NGINX”

  • “Nginx 504 Gateway Timeout”

  • “HTTP 504 Gateway Timeout”

  • “HTTP 504 Error”

  • “HTTP 504”

  • “Gateway Timeout (504)”

504 Gateway Timeout error on Nginx + FastCGI (php-fpm)

For Nginx + FastCGI (php-fpm), you should try to tweak nginx configuration in this way:

Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):

max_execution_time = 300

But, you should also change set request_terminate_timeout parameter (commented by default) at www.conffile from PHP-FPM:

pico -w /etc/php-fpm.d/www.conf

Then set the variable to the same value as max_execution_time:

request_terminate_timeout = 300

Now let’s add fastcgi_read_timeout variable inside our Nginx virtual host configuration:

location ~ .php$ { root /var/www/sites/nginxtips.com; try_files $uri =404; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_read_timeout 300; }

Then restart nginx:

service nginx reload

504 Gateway Timeout error using Nginx as Proxy

For Nginx as Proxy for Apache web server, this is what you have to try to fix the 504 Gateway Timeout error:

Add these variables to nginx.conf file:

  proxy_connect_timeout       600;   proxy_send_timeout          600;   proxy_read_timeout          600;   send_timeout                600;

Then restart nginx:

service nginx reload

Additional Resources

Popular search terms:

  • 504 gateway time-out nginx

  • 504 gateway timeout nginx

  • https://www scalescale com/tips/nginx/504-gateway-time-out-using-nginx/

  • nginx gateway timeout



来自  https://www.scalescale.com/tips/nginx/504-gateway-time-out-using-nginx/#



[SOLUTIONS] 504 GATEWAY TIMEOUT NGINX

 admin 

 April 25, 2016

 

 NginxPHPTutorial

 

 1 Comment

504 Gateway Timeout error Nginx is generated often by a number of reasons on the backend connection that is serving content. This is pretty common error, are generated most probably by the PHP max execution time limit or by the FastCGI read timeout settings. Based on Wikipedia504 Gateway Timeout is the server was acting as a gateway or proxy and did not receive a timely response from the upstream server.. In previous post, I’ve write How to Fix 502 Bad Gateway Error on Nginx.

Here are the most common 504 error messages:

  • “504 Gateway Timeout”

  • “504 Gateway Time-Out”

  • “504 Gateway Timeout NGINX”

  • “Nginx 504 Gateway Timeout”

  • “HTTP 504 Gateway Timeout”

  • “HTTP 504 Error”

  • “HTTP 504”

  • “Gateway Timeout (504)”

504 gateway error nginx

There are several ways to fix it:

  • For Nginx as Proxy (php-fpm disabled)To apply settings globally, increase the following timeout values by adding the file/etc/nginx/conf.d/timeout.conf and restarting ‘nginx’ service:# cat /etc/nginx/conf.d/timeout.conf

    proxy_connect_timeout       600;
    proxy_send_timeout          600;
    proxy_read_timeout          600;
    send_timeout                600;

    If you only are able to increase timeout settings per domain, it can be done in this way:

    Plesk > Subscriptions > my.domain.com > Websites & Domains > Web Server Settings – add the lines to Additional Nginx directives

  • For Nginx + FastCGI (php-fpm enabled)Increase max_execution_time setting:Plesk > Subscriptions > test.com > Websites & Domains > test.com > PHP Settings – Setmax_execution_time = 300Change request_terminate_timeout parameter (commented by default) in /etc/php-fpm.d/www.conf(for Debian /etc/php5/fpm/pool.d/www.conf) file:

    request_terminate_timeout = 300

    Add fastcgi_read_timeout variable inside the ‘nginx’ virtual host configuration:

    Plesk > Subscriptions > my.domain.com > Websites & Domains > Web Server Settings > Additional Nginx directives

    fastcgi_read_timeout 300;

    Add/increase the following values in the ‘http’ section of the /etc/nginx/nginx.conf file:

    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 256k;

    Restart both ‘apache’ and ‘nginx’.

==========================================

If above steps doesn’t work, try this one..

  1. Open your nginx.conf file located in /etc/nginx directory.

  2. Add this below piece of code under http { section:

    client_header_timeout 3000;client_body_timeout 3000;fastcgi_read_timeout 3000;client_max_body_size 32m;fastcgi_buffers 8 128k;fastcgi_buffer_size 128k;

    Note: If its already present , change the values according.

  3. Reload Nginx and php5-fpm.

    $ service nginx reload
    $ service php5-fpm reload

    If the error persists, consider increasing the values.

=========================================

Changes in php.ini

Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):

Changes in PHP-FPM

Try raising request_terminate_timeout setting in php.ini file (CentOS path is /etc/php-fpm.d):

Changes in Nginx Config

Finally, add fastcgi_read_timeout variable inside our Nginx virtual host configuration:

Reload PHP-FPM and Nginx

 

Read also:

[SOLUTIONS] 500 INTERNAL SERVER ERROR PHP

[SOLUTIONS] 502 Bad Gateway Error on Nginx

Source:

nginx 504 error: Connection timed out

Prevent nginx 504 Gateway timeout using PHP set_time_limit()

How To Fix 504 Gateway Time-out on Nginx

来自 https://asdqwe.net/blog/solutions-504-gateway-timeout-nginx/


A website is not accessible: Nginx 504 Gateway Time-out

Symptoms

  • A website fails to load with the following error in a browser:

    504 Gateway Time-out

  • The following errors can be found in the logfile /var/www/vhosts/system/example.com/logs/proxy_error_log:

    # grep "Connection timed out" /var/www/vhosts/system/example.com/logs/proxy_error_log

    [error] 1096#0: *25 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 203.0.113.2, server: example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:///var/www/vhosts/system/example.com/php-fpm.sock", host: "example.com"

  • There are a lot of similar error messages across all domains:

    # grep -R "upstream timed out" /var/www/vhosts/*/logs/ | wc -l
    1017

Cause

Insufficient timeout limits for nginx.

Resolution

If the issue occurs with one domain, increase timeout limits on a domain level:

  1. In Plesk go to Domains > example.com > Apache & nginx Settings and add the following directives to the Additional nginx directives field:

    proxy_connect_timeout 1200s;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    fastcgi_send_timeout 1200s;
    fastcgi_read_timeout 1200s;

  2. Apply the changes.

If the issue occurs with several/all domains, increase timeout limits globally:

  1. Connect to the Plesk server via SSH.

  2. Create a backup of the original file nginx.conf:

    # cp /etc/nginx/nginx.conf nginx.conf_old

  3. Open the file /etc/nginx/nginx.conf in any text editor and add the following directives inside the http {} section:

    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    fastcgi_send_timeout 1200s;
    fastcgi_read_timeout 1200s;

  4. Restart the nginx service:

    # service nginx restart



    504 Gateway Time-Out after publishing on a Nginx-Apache-WordPress platform

    I've set up a blog with WordPress and with Nginx acting as a proxy reverse for Apache. Everything goes really well, but there's a little problem.

    When I write a new post, I can save the draft and everything works fine. But if I clic on the Publish button, the server gives a "504 Gateway time-out" error (Nginx 0.7.65 on the line below).

    The strange things is that the post is indeed published if I visit my blog, there are no mistakes, everythings works right the way it should. So I just come back to my admin page and there it is, the post is listed as published, as if the 504 error was a little warning.

    I think the problem has something to do with PHP (don't have PHP-FPM, just a normal install of php5 -btw, everything is under Ubuntu 10.04 LTS) or maybe with the Apache .htaccess file I've got inside the root of the blog, which is the typical .htaccess:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    # protect wpconfig.php
    <files wp-config.php>
    order allow,deny
    deny from all
    </files>

    Thanks in advance!

    shareimprove this questionasked Feb 16 '11 at 12:01javipas65721435

       Did you try acessing directly the backend and seeing if indeed there is a timeout when posting? – coredumpFeb 16 '11 at 12:37   I don't know exactly what do you mean :( I publish from the backend, and the message appears after publishing the post on the WordPress backend... – javipas Feb 16 '11 at 22:30

    3 Answers 正确答案

    • set proxy_read_timeout and proxy_send_timeout to 60

    • check nginx error log "/var/log/nginx/error.log"(commonly)

    • check Apache error log "/var/log/nginx/error.log"(commonly)



      

    来自 

    https://serverfault.com/questions/236160/504-gateway-time-out-after-publishing-on-a-nginx-apache-wordpress-platform



    How to fix 504 gateway time out Nginx

    The Nginx web server is a full-blown web server that has great potential to deal with huge traffic and especially Reddit type effect. Apart from a web server, Nginx can also be configured as a proxy server. And this configuration is one of the famous and widely used features. For example, when working with php-fpm and other dynamic language modules.

    When Nginx is configured to act as proxy server, in such mode one could face an error 504 gateway time out Nginx. In our today’s article, we will try to understand why it arises and how to deal with it. We will discuss several solutions and reasons.

    This article will cover:

    • How to fix 504 gateway time out Nginx?

    • conclusions

    What does the 504 gateway time out mean?

    As I said, this error occurs when the Nginx server is running in proxy server mode. For example, using Apache as backend server and Nginx as front end proxy server. Literally, it means that the response time from the server has been exceeded. In our case, the response time from php-fpm has been exceeded. Consider several reasons for this behavior:

    • The PHP script or in another language is completely frozen and will not return any response;

    • The script runs for a very long time, but in Nginx the interval is set to reset the connection if the target server did not respond to the request for the allocated rows;

    • The server is overloaded and does not have time to serve all clients, return the answers to all Nginx requests;

    Next, consider what you can do if you encountered a 504 gateway timeout error Nginx.

    How to fix 504 gateway time out Nginx

    The first option – it is when your server, php-fpm or Apache has not enough system resources such as memory or CPU. You can view free memory with the command free:

    free -h

    The load on the processor can be observed by the top command:

    top

    Naturally, if you see that PHP takes up all of the CPU time, it means a problem in the server resources. You can investigate the issue and optimize certain settings to reduce the response time.

    The second option is if it was planned for the script to work for a long time. In this case, you need to configure Nginx to wait for a response from Apache or php-fpm. To solve the problem in the case of php-fpm, you just need to add two lines to the fastgci configuration block:

    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

    Here 300 means 300 seconds, for most scripts, this is quite enough, but you can further increase the value if necessary. Also, error 504 can occur when Nginx is used as a proxy for Apache or any other web server, then you still need to configure the timeout for the proxy. Add these lines to the server section:

    proxy_connect_timeout 600; 
    proxy_send_timeout 600; 
    Proxy_read_timeout 600; 
    Send_timeout 600;

    Here we already set a timeout of 600 seconds for various kinds of actions – connecting, sending data, reading data and so on. After the Nginx setup is complete, restart it:

    sudo systemctl restart nginx

    The last option that we will consider is the script freezes. If you execute the script yourself, you will immediately see that it has performance issues due to some ill or bad coding or may be non-compatibility with php version. If such error occurs it is a more serious problem. You need to immediately read the error log and trace out the problem. Grep is very helpful in such situations,

    grep -i "504" /var/log/nginx/access.log

    More information can sometimes be seen in the error.log:

    grep -i "504" /var/log/nginx/error.log

    Further, if the problem is in php-fpm, you can track which scripts are executed slowly using the built-in function slow-log. To activate it, add the following lines to the configuration of your pool:

    sudo vi /etc/php-fpm.d/www.conf
    slowlog = /var/log/php-fpm/www-slow.log
    request_slowlog_timeout = 5s

    Here 5 seconds, means that scripts will be added to the log file, which runs for more than five seconds. You can change this value at your discretion. In the log you can see not only the scripts themselves but could also trace teh root cause that led to such problems.

    Then you just have to figure out what to do with it, for example, optimize scripts or disable extra plug-ins.

    conclusions

    In this article, we examined how to fix 504 gateway time out issue faced in Nginx, and also why this error may occur.

    Imran Yousaf


    I am Imran Yousaf, a computer geek, founder of the site Smashinglab.com. I am a die hard fond of open-source software and Linux operating system. In addition to Linux, I am interested in everything related to information technology and modern science.


    YOU MAY ALSO LIKE...

    来自  https://www.smashinglab.com/fix-504-gateway-time-nginx/






    普通分类: