Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx map directive: why is it allowed only on http level?

Tags:

http

nginx

server

There is a very useful directive in Nginx map.

But it is possible to use it only on the http level (see docs here http://nginx.org/en/docs/http/ngx_http_map_module.html#map).

For example, I have a server defined and I would like to use some conditional redirects in this server using $url variable. It would be very handy to use this directive inside server but it is not possible. Why?

Yes, I can do it also on the http level but there may be different servers defined on the http level and I would like to keep these conditions inside server section they are defined for.

like image 830
chestozo Avatar asked Dec 08 '14 13:12

chestozo


People also ask

What is map directive in nginx?

Directives. map. map_hash_bucket_size. map_hash_max_size. The ngx_http_map_module module creates variables whose values depend on values of other variables.

Where do I put Nginx directive?

NGINX location directive syntax The NGINX location block can be placed inside a server block or inside another location block with some restrictions. The syntax for constructing a location block is: location [modifier] [URI] { ... ... } The modifier in the location block is optional.

What is http block in nginx?

What is the Http Block? The http block includes directives for web traffic handling, which are generally known as universal . That's because they get passed on to each website configuration served by NGINX. File: /etc/nginx/nginx.conf.

What is upstream server in nginx?

The servers that Nginx proxies requests to are known as upstream servers. Nginx can proxy requests to servers that communicate using the http(s), FastCGI, SCGI, and uwsgi, or memcached protocols through separate sets of directives for each type of proxy.


1 Answers

Pretty old post but I realy want to bring some light into the darkness. The answer itself is quite simple.

DR;TL Variables in NGINX are always global and once defined accessable from anywhere in the configration. Therfore it would not make any sense to define a map in a server or location block.

map creates a new variable whose value depends on values of one or more of the source variables specified in the first parameter.

example configuration:

map $host $myvar { example.com "test"; foo.com     "for";  } 

As variables in NGINX are ALWAYS global and once defined available anywhere else in the configuration. So it wouldn't make any sense to move the map into a location or server block. The interesting fact with our map directive is when the variable myvar will receive its value or when it will be assigned?

map assigns the value to the variable once the variable will be used in your configuration

That means you can define the map in the http context but the value will be assigned at the point you are accessing $myvar in your nginx configuration.

Back to your question: As NGINX variables are always global having a map per server block would make sense as they would be global anyway.

like image 96
Timo Stark Avatar answered Sep 25 '22 07:09

Timo Stark