{"id":26306,"date":"2016-08-04T10:02:48","date_gmt":"2016-08-04T08:02:48","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=26306"},"modified":"2016-08-04T10:35:57","modified_gmt":"2016-08-04T08:35:57","slug":"504-gateway-timeout-error-on-nginx-fastcgi-php-fpm","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/08\/04\/504-gateway-timeout-error-on-nginx-fastcgi-php-fpm\/","title":{"rendered":"504 Gateway Timeout error on Nginx + FastCGI (php-fpm)"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p><a href=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/08\/504.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"26307\" data-permalink=\"https:\/\/mamchenkov.net\/wordpress\/2016\/08\/04\/504-gateway-timeout-error-on-nginx-fastcgi-php-fpm\/attachment\/504\/\" data-orig-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/08\/504.jpg?fit=650%2C500&amp;ssl=1\" data-orig-size=\"650,500\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"504\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/08\/504.jpg?fit=650%2C500&amp;ssl=1\" class=\"aligncenter size-medium wp-image-26307\" src=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/08\/504-500x385.jpg?resize=500%2C385&#038;ssl=1\" alt=\"504\" width=\"500\" height=\"385\" srcset=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/08\/504.jpg?resize=500%2C385&amp;ssl=1 500w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/08\/504.jpg?w=650&amp;ssl=1 650w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>&#8220;504 Gateway Timeout&#8221; error is a very common issue when using Nginx with PHP-FPM. \u00a0Usually, that means that it took PHP-FPM longer to generate the response, than Nginx was willing to wait for. \u00a0A few possible reasons for this are:<\/p>\n<ul>\n<li>Nginx timeout configuration uses very small values (expecting the responses to be unrealistically fast).<\/li>\n<li>The web server is overloaded and takes longer than it should to process requests.<\/li>\n<li>The PHP application is slow (maybe due to database behind it being or slow).<\/li>\n<\/ul>\n<p>There is plenty advice online on how to troubleshoot and sort these issues. \u00a0But when it comes down to increasing the timeouts, I found such advice to be scattered, incomplete, and often outdated. <a href=\"https:\/\/www.scalescale.com\/tips\/nginx\/504-gateway-time-out-using-nginx\/\">\u00a0This page<\/a>, however, has a good collection of tweaks. \u00a0They are:<\/p>\n<ol>\n<li>Increase PHP maximum execution time in <em>\/etc\/php.ini<\/em>:\u00a0<strong>max_execution_time = 300<\/strong><\/li>\n<li>Increase PHP-FPM request terminate timeout in the pool configuration (<em>\/etc\/php-fpm.d\/www.conf<\/em>):\u00a0<strong>request_terminate_timeout = 300<\/strong><\/li>\n<li>Increase Nginx FastCGI read timeout (in <em>\/etc\/nginx\/nginx.conf<\/em>):\u00a0<strong>fastcgi_read_timeout 300;<\/strong><\/li>\n<\/ol>\n<p>Also, see <a href=\"http:\/\/stackoverflow.com\/questions\/16002268\/prevent-nginx-504-gateway-timeout-using-php-set-time-limit\">this Stack Overflow thread<\/a> for more suggestions.<\/p>\n<p>P.S.: while you are sorting out your HTTP errors, have a quick look at <a href=\"http:\/\/httpstatusdogs.com\/\">HTTP Status Dogs<\/a>, which I blogged about a while back.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>&#8220;504 Gateway Timeout&#8221; error is a very common issue when using Nginx with PHP-FPM. \u00a0Usually, that means that it took PHP-FPM longer to generate the response, than Nginx was willing to wait for. \u00a0A few possible reasons for this are: Nginx timeout configuration uses very small values (expecting the responses to be unrealistically fast). The &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/08\/04\/504-gateway-timeout-error-on-nginx-fastcgi-php-fpm\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">504 Gateway Timeout error on Nginx + FastCGI (php-fpm)<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"504 Gateway Timeout error on Nginx + FastCGI (php-fpm) #SysAdmin #DevOps #hosting #Nginx #PHP","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,6,133,62,1334],"tags":[3164,38,1330,2289],"keyring_services":[],"class_list":["post-26306","post","type-post","status-publish","format-standard","hentry","category-general","category-linux","category-sysadmin","category-technology","category-web-work","tag-nginx","tag-php","tag-web-development","tag-web-hosting"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":27865,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/08\/21\/using-php-fpm-as-a-simple-built-in-async-queue\/","url_meta":{"origin":26306,"position":0},"title":"Using php-fpm as a simple built-in async queue","author":"Leonid Mamchenkov","date":"August 21, 2017","format":false,"excerpt":"Here's an interesting solution for a poor man's asynchronous queue using PHP-FPM: PHP-FPM already acts as a queue for Nginx\/Apache FastCGI clients. While your web-request is running you can just send another FastCGI request to the same PHP-FPM socket asynchronously and non-blocking. This request is immediately executed in another php-fpm\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":49042,"url":"https:\/\/mamchenkov.net\/wordpress\/2020\/05\/25\/send-additional-http-headers-to-nginxs-fastcgi\/","url_meta":{"origin":26306,"position":1},"title":"Send additional HTTP headers to Nginx&#8217;s FastCGI","author":"Leonid Mamchenkov","date":"May 25, 2020","format":false,"excerpt":"It's not that often that I come across a useful, but undocumented feature in a major software application.\u00a0 It happened recently, so I'll document it here just for the future self. For a particular setup, I had to send additional HTTP headers (let's use X-GEOIP for this example) to the\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24603,"url":"https:\/\/mamchenkov.net\/wordpress\/2015\/08\/19\/custom-single-sign-on-with-nginx-and-auth-request-module\/","url_meta":{"origin":26306,"position":2},"title":"Custom Single Sign-On with Nginx and Auth Request Module","author":"Leonid Mamchenkov","date":"August 19, 2015","format":false,"excerpt":"In a recent project I crashed into a wall. \u00a0At least for a couple of days that is. \u00a0The requirement was to integrate the Request Tracker (aka RT) installation on CentOS 7 server with Nginx to\u00a0a client's company single sign-on solution. \u00a0Which wasn't LDAP. \u00a0Or Active Directory. \u00a0Or anything standard\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29034,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/11\/26\/roadrunner-high-performance-php-application-server-load-balancer-and-process-manager\/","url_meta":{"origin":26306,"position":3},"title":"RoadRunner &#8211; high-performance PHP application server, load-balancer and process manager","author":"Leonid Mamchenkov","date":"November 26, 2018","format":false,"excerpt":"RoadRunner is a high-performance PHP application server, load-balancer and process manager, written in Goland.\u00a0 It sounds like an excellent replacement for the built-in PHP server, and even more than that - it's production-ready.\u00a0 And it works on Windows too. RoadRunner is an open source (MIT licensed) high-performance PHP application server,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":18031,"url":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/16\/apache2-vs-nginx-for-php-application\/","url_meta":{"origin":26306,"position":4},"title":"Apache2 vs Nginx for PHP application","author":"Leonid Mamchenkov","date":"April 16, 2013","format":"link","excerpt":"Apache2 vs Nginx for PHP application The conclusion is that it doesn\u2019t matter which server you are going to chose. The real performance wins are purely on PHP side. Using an accelerator with caching can multiply the number of requests your infrastructure can maintain.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":22700,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/10\/22\/url-rewriting-in-built-in-web-server-of-php\/","url_meta":{"origin":26306,"position":5},"title":"URL rewriting in built-in web server of PHP","author":"Leonid Mamchenkov","date":"October 22, 2014","format":false,"excerpt":"PHP comes with a handy for development built-in web server. \u00a0However, most web projects have friendly URLs these days which are either Apache (mod_rewrite) or Nginx specific configuration. \u00a0This almost renders built-in PHP web server useless. \u00a0Unless you provide a PHP-based routing, like so: Start the PHP web server with\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=26306"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26306\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=26306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=26306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=26306"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=26306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}