title: Gateway timeout with Spring cloud gateway and Nginx as reverse proxy
body: <p>I created an API gateway for my application and it will act as a front controller for other microservices.
  In my production setup I use Nginx as a reverse proxy for my gateway</p>
  
  <p>The API gateway is running on port 8080</p>
  
  <p>Nginx configured as below:</p>
  
  <p>gateway-api.conf:</p>
  
  <pre><code>server {
      listen 80;
      server_name api.example.com;
      location / {
          proxy_set_header        X-Real-IP       $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_http_version 1.1;
          proxy_set_header Connection "";
          proxy_pass http://localhost:30010/;
          keepalive_timeout 500s;
      }
      keepalive_timeout 500s;
      access_log /var/log/nginx/api.log;  
      error_log /var/log/nginx/api_error.log;
  }
  </code></pre>
  
  <p>Timeout setting in nginx.conf:</p>
  
  <pre><code>proxy_connect_timeout 300;
  proxy_send_timeout 300;
  proxy_read_timeout 300;
  send_timeout 300;
  </code></pre>
  
  <p>Spring cloud gateway gradle file: </p>
  
  <pre><code>compile('org.springframework.cloud:spring-cloud-starter-gateway')
   compile('org.springframework.cloud:spring-cloud-starter-openfeign')
   compile("org.springframework.boot:spring-boot-starter-actuator")
   compile('org.springframework.boot:spring-boot-starter-security')
  
  springBootVersion=2.0.3.RELEASE
  springDMPVersion=1.0.4.RELEASE
  springPlatformBomVersion=Cairo-SR2
  springCloudVersion=Finchley.RELEASE
  </code></pre>
  
  <p>Gateway application:</p>
  
  <pre><code>@SpringBootApplication
  @ComponentScan(basePackages = {"com.example"})
  @EntityScan(basePackages = {"com.example"})
  @EnableFeignClients(basePackages = "com.example")
  public class GatewayApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(GatewayApplication.class, args);
      }
  }
  </code></pre>
  
  <p>Problem statement:</p>
  
  <p>In one of my microservices, one REST API takes more than 3 minutes to complete.
  If I call this API via <code>nginx(api.example.com)</code>, it fails <strong>exactly after 1 minute</strong> and gives HTTP status 504.</p>
  
  <p>curl:</p>
  
  <pre><code>curl --request GET \
    --url http://api.example.com/hellomicroservice/api/take/moretime
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>504 Timeout while reading the response from Server
  </code></pre>
  
  <p>There are no error logs in nginx or the API gateway.</p>
  
  <p>Access log from nginx:</p>
  
  <pre><code>203.129.213.102 - - [01/Apr/2019:08:14:33 +0000] "GET hellomicroservice/api/take/moretime HTTP/1.1" 499 0 "-" "PostmanRuntime/7.3.0"
  </code></pre>
  
  <p>But when I make a call of the same API directly to the gateway (on gateway port 8080), the request is processed successfully.</p>
  
  <p>curl with gateway port:</p>
  
  <pre><code>curl --request GET \
    --url http://api.example.com:8080/hellomicroservice/api/take/moretime
  </code></pre>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>If I apply the Nginx timeout settings as less than 60 Seconds (e.g. 30 seconds), the request gets timed out in a specified time interval. But if I set the Nginx timeout to be more than 60 seconds, let's 300 Seconds, the request gets timed out after 60 seconds.</p>