如何parsing和检索Nginx中的子域名和主机名标签

我正在使用一个基于nginx + php-fpm + mysql堆栈上的几个magento版本进行testing的环境,我把每个发行版放在不同的文件夹中,并放在nginx的vhostconfiguration文件中,我使用下面的语句:

 server_name〜^(?<。>。+)\。magento \ .test $;
 root [root_path] / distro / $ version;

所以,直到在这里,我可以得到任何dynamicurl映射到其文件夹

 ce107.magento.test => [root_path] / distro / ce107 /
 ce108.magento.test => [root_path] / distro / ce108 /
 ce109.magento.test => [root_path] / distro / ce109 /

当我尝试使用“主机名称标签”来切换每个发行版上的网站时,问题就开始了:

  us.ce107.magento.test,ar.ce107.magento.test ... 

因为nginx会查找: [root_path] /distro/us.ce107/[root_path] /distro/ar.ce107/文件夹。

所以我需要parsing和检索值:

  [ws_code] [版] .magento.test 

我不知道该怎么做,也许我可以使用“map $ host $ ws_code {}”来parsing$ ws_code的值,并应用一些正则expression式来删除“ws_code”。 部分从$版本

BTW显然我可以做到“编码”,但我想保持“dynamic”

有人能帮忙吗? 提前致谢!

(?<version>.+)expression式将使用贪婪匹配并将其分配给$versionvariables。 你可以做同样的匹配ws_codeversion

 server_name ~^(?<ws_code>.+)\.(?<version>.+?)\.magento\.test$; root [root_path]/distro/$version/$ws_code; 

我对[ws_code]使用了贪婪匹配,对[version]非贪婪匹配。 另外我会用[a-zA-Z0-9-]+这样的更确定性的东西来排除这个可能匹配的域分隔符点符号(在这种情况下,我们不需要检查它的匹配algorithm是否贪婪) :

 server_name ~^((?<ws_code>[az]+)\.)?(?<version>[a-zA-Z0-9-]+)\.magento\.test$; root [root_path]/distro/$version; 

附加()? 通过ws_code matchig的括号将允许这个configuration匹配两个第三级域只有一个variables可用和第四级域有两个variables可用。 您可能想要为ws_code 指定一个默认variables 。