{"id":2354,"date":"2024-01-10T15:53:20","date_gmt":"2024-01-10T10:23:20","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=2354"},"modified":"2025-04-15T12:31:38","modified_gmt":"2025-04-15T07:01:38","slug":"fix-504-gateway-timeout-nginx","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/fix-504-gateway-timeout-nginx\/","title":{"rendered":"How to Fix 504 Gateway Timeout Error in NGINX"},"content":{"rendered":"\n<p>Have you ever experienced an irritating NGINX server &#8220;504 Gateway Timeout&#8221; error? Most likely, the problem stems from a badly specified timeout setting. NGINX employs a range of timeout mechanisms to regulate server-client communication, guaranteeing effective resource distribution and mitigating incessant queries. But understanding and modifying these might be scary.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large has-custom-border\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/gatway.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"488\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/gatway-1024x488.png\" alt=\"504 Gateway Timeout\" class=\"wp-image-2358\" style=\"border-width:1px\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/gatway-1024x488.png 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/gatway-300x143.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/gatway-768x366.png 768w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/gatway.png 1442w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>This knowledgebase explains details about these timeouts and their solutions along with their purpose, configuration options, and safe disablement techniques. To solve the problem quickly and easily, follow the instructions in the knowledge base:<\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"identify-the-cause\">Identify the Cause<\/h4>\n\n\n<p>First, confirm that the 504 error is due to a timeout issue. Check your NGINX error logs (usually located at <code>\/var\/log\/nginx\/error.log<\/code>) for messages indicating a timeout.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -n 50 \/<span style=\"background-color: initial;color: var(--ast-global-color-3)\">var\/log\/nginx\/error.lo<\/span>g<\/code><\/pre>\n\n\n\n<p>If the log entry resembles this: <code>upstream timed out (110: Connection timed out) while connecting to upstream<\/code>, It means NGINX couldn\u2019t get a response from the upstream server within the specified timeout period and this can be solved by altering the values in NGINX configuration.<\/p>\n\n\n\n<p>To begin with, familiarize yourself with the distinctions among the values that can be altered in the Nginx configuration file: \/etc\/nginx\/sites-available\/default or \/etc\/nginx\/nginx.conf, which is required to fix the timeout problem:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Setting<\/th><th>Purpose<\/th><th>Default Value<\/th><th>Modification Example<\/th><th>Example Scenario<\/th><\/tr><\/thead><tbody><tr><td>keepalive_timeout<\/td><td>Duration a connection remains idle before NGINX closes it.<\/td><td>65s<\/td><td>keepalive_timeout 30;<\/td><td>Useful for websites with frequent client requests, to free up server connections quickly.<\/td><\/tr><tr><td>send_timeout<\/td><td>Maximum time allowed for NGINX to send a response to the client.<\/td><td>60s<\/td><td><br>send_timeout 120;<\/td><td>Beneficial for servers handling large files or media streaming, to accommodate slower client speeds.<\/td><\/tr><tr><td>proxy_read_timeout<\/td><td>Determines how long NGINX will wait for a response from a proxied server.<\/td><td>60s<\/td><td>proxy_read_timeout 180s;<\/td><td>Helpful for backends performing time-consuming operations like report generation.<\/td><\/tr><tr><td>proxy_connect_timeout<\/td><td>Sets the maximum time NGINX will wait while establishing a connection with an upstream server.<\/td><td>60s<\/td><td>proxy_connect_timeout 90s;<\/td><td>Useful when upstream servers are under heavy load and take longer to establish connections.<\/td><\/tr><tr><td>fastcgi_read_timeout<\/td><td>Specifies how long NGINX will wait for a response from a FastCGI process, like PHP-FPM.<\/td><td>60s<\/td><td>fastcgi_read_timeout 300s;<\/td><td>Ideal for hosting complex PHP applications requiring longer processing times.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Now let&#8217;s get to the core of modifying each timeout setting:<\/p>\n\n\n\n<p>Open your NGINX configuration file for editing. This file is typically found at <code>\/etc\/nginx\/nginx.conf<\/code> or in the <code>sites-enabled<\/code> directory within <code>\/etc\/nginx\/<\/code>. If you have a specific configuration for your site, it might be in a file named after your domain in the <code>sites-available<\/code> directory and symlinked to <code>sites-enabled<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/etc\/nginx\/sites-available\/default<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\">OR<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi <span style=\"background-color: initial;color: var(--ast-global-color-3)\">\/etc\/nginx\/nginx.conf<\/span><\/code><\/pre>\n\n\n\n<p>Then, Find the <code>location<\/code> block in your configuration that is handling the requests for the large files. It will look something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>location \/path\/to\/large\/files\/ {<br>\u2026<br>}<\/code><\/code><\/pre>\n\n\n\n<p>Inside this block, add or modify the desired directive.<\/p>\n\n\n\n<p>For example; if you&#8217;re facing the error after serving very large files, you might need to increase proxy_read_timeout to several minutes.<\/p>\n\n\n\n<p>Example of the error:<\/p>\n\n\n\n<p class=\"has-ast-global-color-4-background-color has-background\"><code>2024\/01\/10 15:48:00 [error] 6545#6545: *12345 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.1.100, server: example.com, request: \"GET \/some-resource HTTP\/1.1\", upstream: \"http:\/\/upstream-server:port\/some-resource\", host: \"example.com\"<\/code><\/p>\n\n\n\n<p>Example of edit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>location \/path\/to\/large\/files\/ {<br>     \u2026<br>     proxy_read_timeout 300s; # Sets the timeout to 5 minutes<br>     \u2026<br>}<\/code><\/code><\/pre>\n\n\n\n<p>The other error examples in the error.log file along with the values you can adjust to fix the problem are as follows, for your reference:<\/p>\n\n\n<h5 class=\"wp-block-heading\" id=\"keepalivetimeout\">keepalive_timeout<\/h5>\n\n\n<p><strong>Log Entry Example<\/strong>:<\/p>\n\n\n\n<p class=\"has-ast-global-color-4-background-color has-background\"><code>*1 client stopped connection before send body completed while sending to client, client: 192.168.1.100, server: example.com, request: \"GET \/ HTTP\/1.1\", host: \"example.com\"<\/code><\/p>\n\n\n\n<p><strong>Indicates<\/strong>: The client closed the connection before NGINX was able to complete sending the response. This could be because the <code>keepalive_timeout<\/code> duration was exceeded.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h5 class=\"wp-block-heading\" id=\"fastcgireadtimeout\">fastcgi_read_timeout<\/h5>\n\n\n<p><strong>Log Entry Example<\/strong>:<\/p>\n\n\n\n<p class=\"has-ast-global-color-4-background-color has-background\"><code>upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.1.100, server: example.com, request: \"GET \/php-app.php HTTP\/1.1\", upstream: \"fastcgi:\/\/127.0.0.1:9000\", host: \"example.com\"<\/code><\/p>\n\n\n\n<p><strong>Indicates<\/strong>: NGINX did not receive a response from the FastCGI process (like PHP-FPM) within the specified <code>fastcgi_read_timeout<\/code>. This could suggest the PHP process is taking too long to execute or is stuck.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h5 class=\"wp-block-heading\" id=\"proxyconnecttimeout\">proxy_connect_timeout<\/h5>\n\n\n<p><strong>Log Entry Example<\/strong>:<\/p>\n\n\n\n<p class=\"has-ast-global-color-4-background-color has-background\"><code>connect() timed out (110: Connection timed out) while connecting to upstream, client: 192.168.1.100, server: example.com, request: \"GET \/app HTTP\/1.1\", upstream: \"http:\/\/127.0.0.1:8080\", host: \"example.com\"<\/code><\/p>\n\n\n\n<p><strong>Indicates<\/strong>: NGINX was unable to establish a connection with the upstream server within the <code>proxy_connect_timeout<\/code>. This is often a network-related issue or the upstream server is not accepting connections.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h5 class=\"wp-block-heading\" id=\"sendtimeout\">send_timeout<\/h5>\n\n\n<p><strong>Log Entry Example<\/strong>:<\/p>\n\n\n\n<p class=\"has-ast-global-color-4-background-color has-background\"><code>*1 client timed out (110: Connection timed out) while sending response to client, client: 192.168.1.100, server: example.com, request: \"GET \/large-file.zip HTTP\/1.1\", host: \"example.com\"<\/code><\/p>\n\n\n\n<p><strong>Indicates<\/strong>: NGINX was unable to send data to the client within the <code>send_timeout<\/code> period. This could occur if the client is reading data too slowly, often due to network issues on the client&#8217;s side or the client has stopped receiving data without properly closing the connection.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>These are typical examples of timeout-related errors you might encounter in the NGINX error log. When you see these, you usually start troubleshooting by looking at the respective timeout settings in your NGINX configuration and considering adjustments based on the context of the error. If you are getting a different error: <a href=\"https:\/\/www.veeble.com\/kb\/fix-nginx-503-service-temporarily-unavailable-error\/\">503 Service Temporary Unavailable Error<\/a>, check out the following blog in the link to resolve it: <a href=\"https:\/\/www.veeble.com\/kb\/fix-nginx-503-service-temporarily-unavailable-error\/\">https:\/\/www.veeble.com\/kb\/fix-nginx-503-service-temporarily-unavailable-error\/<\/a><\/p>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-8f0316ad wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Experience the Power of Veeble VPS Hosting<\/h3><p class=\"uagb-cta__desc\">Experience the power of Veeble VPS hosting. Our plans are affordable, reliable, and packed with features that will help you take your business to the next level.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/vps-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Explore VPS Plans<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to resolve the 504 Gateway Timeout error in NGINX easily to enhance your website&#8217;s performance and reduce downtime.<\/p>\n","protected":false},"author":7,"featured_media":8594,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2354","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Fix-504-Gateway-Timeout-Error-in-NGINX.jpg",1366,768,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Fix-504-Gateway-Timeout-Error-in-NGINX-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Fix-504-Gateway-Timeout-Error-in-NGINX-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Fix-504-Gateway-Timeout-Error-in-NGINX-768x432.jpg",768,432,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Fix-504-Gateway-Timeout-Error-in-NGINX-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Fix-504-Gateway-Timeout-Error-in-NGINX.jpg",1366,768,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/How-to-Fix-504-Gateway-Timeout-Error-in-NGINX.jpg",1366,768,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"Learn how to resolve the 504 Gateway Timeout error in NGINX easily to enhance your website's performance and reduce downtime.","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=2354"}],"version-history":[{"count":12,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2354\/revisions"}],"predecessor-version":[{"id":7284,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2354\/revisions\/7284"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/8594"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=2354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=2354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=2354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}