M
M
mirolin122015-09-26 20:37:54
Nginx
mirolin12, 2015-09-26 20:37:54

Need help in nginx. How to convert if to map?

Good day. I have the following nginx configuration:

http {
...
map_hash_bucket_size 256;
map $server_name $sites {
     default 0;
     include /etc/nginx/conf/doc/site_list_st.conf;
}
server {
  server_name 111.111.111.111;
  location / {
                set $asc $sites;
                      if ($asc = 10) { директива on;}
                      if ($asc != 10) { директива off;}
}
}

}

However, a problem arises.
The required directive works only in location (it refuses to work in if). The only solution that I could find on the Internet is to replace if with map. In turn, map only works in the http block, which is why I don't understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Zhivotnev, 2015-09-27
@mirolin12

Before the server{} section:

map $http_host $somevar {
hostnames; 

host1.com "on";
host2.com "off";
default "off":
}

server {
 .... 
set $your_param $somevar;
}

If you can immediately specify your variable in the second part of the map (not everything will work out), then set inside server {} is no longer needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question