nginx conf/w多个映射到同一变量 [英] nginx conf /w multiple map(s) to same variable

查看:80
本文介绍了nginx conf/w多个映射到同一变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个多站点设置,需要将域和域/子文件夹映射到一个变量.这样,编程人员便知道要加载哪个版本.

we have a multisite set-up and need to map domains and domains/subfolders to a variable. This way the programming knows which version to load.

我们拥有的商店具有不同的域,可以由$http_host捕获,也可以由domain.com/-string-locale-here-捕获,并由$http_host$uri和匹配命令捕获

We have stores that have separate domains and that can be captured by $http_host but also domain.com/-string-locale-here- and are captured by $http_host$uri and a match command

不知何故,以下内容不起作用.可能是因为有两个映射命令,两个命令都映射到相同的变量$storecode

Somehow the below is not working. Can this be because there are two map commands, both mapping towards the same variable $storecode

还是可能出了什么问题?

Or what might be going wrong?

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;
}

推荐答案

在地图块中未指定default时,默认结果值为空字符串.因此,在您的情况下,无论在第一个映射块中设置了什么值$storecode,它都会在第二个映射块中替换为空字符串.

When default is not specified in a map block, the default resulting value will be an empty string. So, in your case, whatever value $storecode is set with in the first map block, it is replaced with an empty string in the second one.

由于使用映射变量时会对其求值,因此不能在第二个映射块中将$storecode设置为默认值,因为这将导致无限循环.

Since map variables are evaluated when they are used, you cannot set $storecode as the default value in the second map block, because that will cause an infinite loop.

因此解决方案是在第一个映射块中引入一个临时变量,然后在第二个块中将其用作默认值:

So the solution is to introduce a temporary variable in the first map block and then use it as the default value in the second block:

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;
}

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

Alternatively, you can merge these two map blocks into one:

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;
}

这篇关于nginx conf/w多个映射到同一变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆