在声明'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
文件。这比编写几十个bashecho
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
文件。这比编写几十个bashecho
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;
}