nginx conf / w多个映射到同一个variables

我们有一个多站点设置,需要将域和域/子文件夹映射到一个variables。 这样编程就知道要加载哪个版本。

我们的商店有独立的域名,可以通过$http_host捕获,也可以通过domain.com/-string-locale-here-捕获,并通过$http_host$uri捕获,匹配命令

不知怎的,下面是不行的。 这可能是因为有两个映射命令,都映射到相同的variables$storecode

或者可能会出错?

 map $http_host $storecode { default dom_nl; domain.com dom_nl; domain.de dom_de; store.com str_de; } map $http_host$uri $storecode { ~^store.com/en.* str_en; ~^store.com/fr.* str_fr; } 

在地图块中未指定默认值时,默认结果值将为空字符串。 所以,就你的情况而言,无论在第一个映射块中设置了什么值$storecode ,它都会被替换为第二个映射块中的空字符串。

由于地图变量在使用时被评估,因此不能将$storecode设置$storecode第二个地$storecode中的默认值,因为这会导致无限循环。

所以解决方法是在第一个地图块中引入一个临时变量,然后在第二个块中使用它作为默认值:

 map $host $default_storecode { default dom_nl; domain.com dom_nl; domain.de dom_de; store.com str_de; } map $host$uri $storecode { default $default_storecode; ~^store.com/en.* str_en; ~^store.com/fr.* str_fr; } 

或者,您可以将这两个地图块合并为一个:

 map $host$uri $storecode { default dom_nl; ~^domain.com.* dom_nl; ~^domain.de.* dom_de; ~^store.com/en.* str_en; ~^store.com/fr.* str_fr; ~^store.com.* str_de; }