Reputation: 9886
When developing on my system, I use server_name ~^(?<subdomain>.+)\.localhost$;
to capture the subdomain, however, in production, my reverse proxy is deployed over multiple domains and the domain name is stored in the nginx variable $domain
.
How do I do a regex capture while doing string interpolation at the same time.
E.g. instead of server_name ~^(?<subdomain>.+)\.localhost$
how do I do server_name ~^(?<subdomain>.+)\.${domain}$;
Actual code:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.localhost$;
location / {
proxy_pass https://sarahah.com; # get the joke? ;)
proxy_set_header Host $subdomain.sarahah.com;
}
Upvotes: 0
Views: 1495
Reputation: 9886
After jumping around and trying to read documents, I kinda gave up and created an nginx.conf.erb
file, that I compile into nginx.conf
with Ruby's ERB
.
Seriously, if you're from the future and banging your head to fix this, just go with the ERB
file. It's better than scripting dozens of bash echo
s to get your environment variables into the nginx.conf
and it's the best thing I did all week.
My code now looks like:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.<%= ENV['DOMAIN'] %>$;
location / {
proxy_pass https://sarahah.com; # get the joke? ;)
proxy_set_header Host $subdomain.sarahah.com;
}
Upvotes: 4