0

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

1 Answer 1

4

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 echos 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;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.