map

This module allows you to classify, or map a set of values into a different set of values and store the result in a variable.

Example:

  1. map $http_host $name {
  2. hostnames;
  3. default 0;
  4. example.com 1;
  5. *.example.com 1;
  6. test.com 2;
  7. *.test.com 2;
  8. .site.com 3;
  9. }

One use for this would be to use a mapping in place of writing lots of server/location directives or redirects:

  1. map $uri $new {
  2. default http://www.domain.com/home/;
  3. /aa http://aa.domain.com/;
  4. /bb http://bb.domain.com/;
  5. /john http://my.domain.com/users/john/;
  6. }
  7. server {
  8. server_name www.domain.com;
  9. rewrite ^ $new redirect;
  10. }

指令

map

syntax:*map $var1 $var2 { … }*

default:*none*

context:*http*

map defines the mapping table which will be used to set a variable. There are three special parameters:

  • default — defines the value to be used where no match is found.
  • hostnames — it allows for an easier matching of values like host names, names with a starting dot may match exact host names and host names ending with the value, for example:
    1. *.example.com 1;

Instead of two entries

  1. example.com 1;
  2. *.example.com 1;

we can use only one

  1. .example.com 1;
  • include — include values from a file. Multiple includes may be used.

    map_hash_max_size

syntax:*map_hash_max_size number*

default:*map_hash_max_size 2048*

context:*http*

The directive sets the maximum size of a hash table to hold the variable map. For more details see the descriptions of hash settings Optimization section .

map_hash_bucket_size

syntax:*map_hash_bucket_size n*

default:*map_hash_bucket_size 32/64/128*

context:*http*

The directive sets the maximum size in a hash table to map variables. The default value depends on the size of the cache line processor. More see in the descriptions of hash settings in the Optimization section .

References

Original Documentation

原文: https://wizardforcel.gitbooks.io/nginx-doc/content/Text/3.18_map.html