ivanko_p
ivanko_p

Reputation: 11

How to properly set timeout for one location in nginx and proxy_pass it to upstream server?

I have the following nginx config which consists of an upstream server app and a server listening on localhost:5000.

All requests with url prefixed with /test will be proxied to an @app server (like /test, /test/foo, /testing)

I want to add incresed timeout to a requests with url /test/timeout (sending request to http:///localhost:5000/test/timeout)

So far I came up with the following solution matching url directly with = and proxy passing it to app.

  http {
    include       mime.types;   
    error_log     logs/timeout.log info;

    default_type application/octet-stream;

    
    upstream app {
        server localhost:8080;
    }
    
    server {
        listen 5000;
        server_name localhost;
    
        location @app { 
            proxy_pass http://app;        
        }
    
        location /test {
            try_files @request_uri @app;
        }
        
        location = /test/timeout {
            proxy_read_timeout 125s;
            proxy_connect_timeout 125s;
            proxy_send_timeout 125s;
            proxy_pass http://app;
        }

    }
 }

The aforementioned config is working fine, but I wonder if it's possible to do the same thing without proxy passing from /test/timeout like so (the below config does not work) to avoid duplication of proxy_pass http://app;

  http {
    include       mime.types;   
    error_log     logs/timeout.log info;

    default_type application/octet-stream;

    
    upstream app {
        server localhost:8080;
    }
    
    server {
        listen 5000;
        server_name localhost;
    
        location @app { 
            proxy_pass http://app;        
        }
    
        location /test {
            try_files @request_uri @app;
        }
        
        location /test/timeout/ {
            proxy_read_timeout 125s;
            proxy_connect_timeout 125s;
            proxy_send_timeout 125s;
        }

    }
  }

Upvotes: 0

Views: 61

Answers (0)

Related Questions