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 server
s defined on the http
level and I would like to keep these conditions inside server
section they are defined for.
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.
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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With