Simulate random 504 errors in Nginx

267 Views Asked by At

Is there a way that I can add to an Nginx config something that would at random cause 504 Gateway Timeout errors?

My current Nginx development config is:

http {
   
   client_max_body_size 100M;
   client_body_timeout 60s;
   client_header_timeout 60s;
   keepalive_timeout 60s;

   upstream app {
      server app-1:8080;
      server app-2:8080;
   }

   server {
      listen 80;
      location / {

         # want to enter something like
         # if (random() < 10) {
         #   return 504;
         # }

         proxy_pass http://app;
         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_set_header X-Forwarded_Proto $scheme;
         client_max_body_size 100M;
      }
   }
   
}

My problem is that in production I get the occasional 504 Gateway Timeout errors between Nginx and my Kotlin Spring Boot web app and I want to test running code in development to handle these errors when they come up. But in my development mode with Nginx running in a docker container, it's just working and I haven't run into the 504 Gateway errors.

So while I do want to get the 504 fixed, I also am hoping there's a way I could simulate a random Nginx 504 response every now and then.

1

There are 1 best solutions below

0
opeonikute On

A simple explanation of a 504 timeout states:

The 504 Gateway Timeout Error indicates that the server didn't receive a timely response from an upstream server it needed to access in order to complete a request.

You can simply stop the servers running in development and make a request to Nginx. It won't get a timely response and return a 504.

You can also reduce the timeout duration (60 seconds) by default so you don't have to wait long. e.g. 30 seconds

http{
   ...
   proxy_read_timeout 30;
   proxy_connect_timeout 30;
   proxy_send_timeout 30;
   ...
}