【问题标题】:nginx conf /w multiple map(s) to same variablenginx conf /w 多个映射到同一个变量
【发布时间】:2016-03-01 17:39:25
【问题描述】:

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

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

不知何故,以下不起作用。这可能是因为有两个映射命令,都映射到同一个变量$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;
}

【问题讨论】:

    标签: nginx


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2020-09-09
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多