基于url或主机名的nginx映射变量查找+;uri(不是主机名)

基于url或主机名的nginx映射变量查找+;uri(不是主机名),nginx,Nginx,我们运行Magento,直到日期取决于主机名,设置了一个变量来决定显示哪个存储。下面的例子 map $http_host $storecode { domain.com store1_en; otherdomain.com store2_en; } 现在,新的商店是使用(2个字符)语言代码建立的,比如“en”、“de”或“fr”,url变成domain.com/en或otherdomain.com/fr 为了使上述工作正常,应该更改映射查找,不再使用服务器名的主机,而是使用URL的第一部分

我们运行Magento,直到日期取决于主机名,设置了一个变量来决定显示哪个存储。下面的例子

map $http_host $storecode {
 domain.com store1_en;
 otherdomain.com store2_en;
}
现在,新的商店是使用(2个字符)语言代码建立的,比如“en”、“de”或“fr”,url变成domain.com/en或otherdomain.com/fr

为了使上述工作正常,应该更改映射查找,不再使用服务器名的主机,而是使用URL的第一部分。。。。而且顺序正确

问题:如何实现这一点?或者以前有人这样做过吗


因此,下面的映射是我们正在寻找的

  • domain.com/en=>store1\u fr
  • domain.com=>store1\u en
  • otherdomain.com=>store2\u en
作为解决方案的一些想法:

map $uri $storecode {
 ^domain.com/en.* store1_fr;
 ^domain.com.* store1_en;
 ^otherdomain.com.* store2_en;
}

map $http_host$uri $storecode {
 ^domain.com/en.* store1_fr;
 ^domain.com.* store1_en;
 ^otherdomain.com.* store2_en;
}
或者我在想一张地图中的一张地图

或者从$uri中捕获前两个字母(仅当它确实有两个字母以排除其他子目录请求时…)


感谢您的帮助

您的最后一个示例正是您所需要的。只要在^之前加上~就行了。像这样?map$host$uri$storecode{-^domain.com/en.*store1\u fr;-^domain.com.*store1\u en;如下所示:
map$http\u host$uri$storecode{-^domain.com/en.*store1\u fr;}
并且可以直接在彼此之后使用这两个map命令吗?看起来不是这样的: