Nginx map - use variable multiple times or use multiple variables

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Nginx map - use variable multiple times or use multiple variables

satay
Hello,

We use Nginx map module to sent traffic to different upstreams based on the
HTTP header:

map $http_flow $flow_upstream {
  default "http://flow-dev";
  prod    "http://flow-prod";
  test    "http://flow-test";
  dev     "http://flow-dev";
}

  location / {
    proxy_read_timeout 5s;
    proxy_pass $flow_upstream;
  }

Now, we want to define a different timeouts to different flows:

map $http_flow $read_timeout {
  default 15s;
  prod    5s;
  test    10s;
  dev     15s;
}

  location / {
    proxy_read_timeout $read_timeout;
    proxy_pass $flow_upstream;
  }

Bunt Nginx config test show the error here:
nginx -t
nginx: [emerg] "proxy_send_timeout" directive invalid value in
/etc/nginx/conf.d/flow.conf:19
nginx: configuration file /etc/nginx/nginx.conf test failed

Can we use map in such a way?

Or maybe something like this:
map $http_flow $flow_upstream $read_timeout {
  default "http://flow-dev"  "15s";
  prod    "http://flow-prod" "5s";
  test    "http://flow-test" "10s";
  dev     "http://flow-dev"  "15s";
}

Thank you!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286421,286421#msg-286421

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx map - use variable multiple times or use multiple variables

Maxim Dounin
Hello!

On Thu, Dec 05, 2019 at 01:12:07PM -0500, stmx38 wrote:

> Hello,
>
> We use Nginx map module to sent traffic to different upstreams based on the
> HTTP header:
>
> map $http_flow $flow_upstream {
>   default "http://flow-dev";
>   prod    "http://flow-prod";
>   test    "http://flow-test";
>   dev     "http://flow-dev";
> }
>
>   location / {
>     proxy_read_timeout 5s;
>     proxy_pass $flow_upstream;
>   }
>
> Now, we want to define a different timeouts to different flows:
>
> map $http_flow $read_timeout {
>   default 15s;
>   prod    5s;
>   test    10s;
>   dev     15s;
> }
>
>   location / {
>     proxy_read_timeout $read_timeout;
>     proxy_pass $flow_upstream;
>   }
>
> Bunt Nginx config test show the error here:
> nginx -t
> nginx: [emerg] "proxy_send_timeout" directive invalid value in
> /etc/nginx/conf.d/flow.conf:19
> nginx: configuration file /etc/nginx/nginx.conf test failed

The proxy_send_timeout does not support variables.  Note that if a
particular directive supports variables, this is explicitly said
in the documentation.

Instead, consider handling requests in different locations if you
want to use different configurations for them.  For example:

     map $http_flow $flow {
         default    dev;
         test       test;
     }

     location / {
         rewrite (.*) /$flow/$1 last;
     }

     location /dev/ {
         proxy_pass http://flow-dev/;
         proxy_send_timeout 15s;
     }

     location /test/ {
         proxy_pass http://flow-test/;
         proxy_send_timeout 10s;
     }

(Untested, and might require additional configuration to work
properly, such as switching off proxy_redirect.)

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx map - use variable multiple times or use multiple variables

satay
Maxim,

Different locations solved the issue.

Thank you for the help!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286421,286423#msg-286423

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx