在声明'server\u name'时,如何在正则表达式模式中使用nginx变量`

在声明'server\u name'时,如何在正则表达式模式中使用nginx变量`,nginx,Nginx,在我的系统上开发时,我使用server\u name~ ^(?.+)\.localhost$为了捕获子域,但是在生产中,我的反向代理部署在多个域上,域名存储在nginx变量$domain中 如何在执行字符串插值的同时执行正则表达式捕获 例如,代替server\u name~ ^(?.+)\.localhost$我该如何做server\u name~ ^(?.+)\.${domain}$ 实际代码: server { listen 80; server_name ~

在我的系统上开发时,我使用
server\u name~ ^(?.+)\.localhost$
为了捕获子域,但是在生产中,我的反向代理部署在多个域上,域名存储在nginx变量
$domain

如何在执行字符串插值的同时执行正则表达式捕获

例如,代替
server\u name~ ^(?.+)\.localhost$
我该如何做
server\u name~ ^(?.+)\.${domain}$

实际代码:

  server {
    listen       80;
    server_name ~^(?<subdomain>.+)\.localhost$;

    location / {
        proxy_pass https://sarahah.com; # get the joke? ;)
        proxy_set_header Host $subdomain.sarahah.com;
    }
服务器{
听80;
服务器名称~^(?.+)\.localhost$;
地点/{
代理通行证https://sarahah.com明白这个笑话吗?)
代理集头主机$subdomain.sarahah.com;
}

在尝试阅读文档后,我放弃了,创建了一个
nginx.conf.erb
文件,我用Ruby的
erb
编译成
nginx.conf

说真的,如果你是从未来来的,并且为了解决这个问题而绞尽脑汁,只需使用
ERB
文件。这比编写几十个bash
echo
s脚本来将环境变量输入
nginx.conf
要好,这是我这一周做的最好的事情

我的代码现在看起来像:

server {
    listen       80;
    server_name ~^(?<subdomain>.+)\.<%= ENV['DOMAIN'] %>$;

    location / {
        proxy_pass https://sarahah.com; # get the joke? ;)
        proxy_set_header Host $subdomain.sarahah.com;
    }
服务器{
听80;
服务器名称~^(?.+)\.$;
地点/{
代理通行证https://sarahah.com明白这个笑话吗?)
代理集头主机$subdomain.sarahah.com;
}

在尝试阅读文档后,我放弃了,创建了一个
nginx.conf.erb
文件,我用Ruby的
erb
编译成
nginx.conf

说真的,如果你是从未来来的,并且为了解决这个问题而绞尽脑汁,只需使用
ERB
文件。这比编写几十个bash
echo
s脚本来将环境变量输入
nginx.conf
要好,这是我这一周做的最好的事情

我的代码现在看起来像:

server {
    listen       80;
    server_name ~^(?<subdomain>.+)\.<%= ENV['DOMAIN'] %>$;

    location / {
        proxy_pass https://sarahah.com; # get the joke? ;)
        proxy_set_header Host $subdomain.sarahah.com;
    }
服务器{
听80;
服务器名称~^(?.+)\.$;
地点/{
代理通行证https://sarahah.com明白这个笑话吗?)
代理集头主机$subdomain.sarahah.com;
}