B
B
balyko2016-12-21 16:43:22
Django
balyko, 2016-12-21 16:43:22

How to replace subdomain in http header in nginx?

You need to replace the proxied header for gunicor that nginx passes to it. Let's say I request the name dev.example.com and gunicorn is given just example.com. I just managed to remove the subdomain as follows:

location / {
                        proxy_pass http://unix:/var/run/gunicorn/gunicorn.sock;
                        proxy_redirect off;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Forwarded-Proto $scheme;
                        proxy_set_header Host example.com;
                }

However, I need the subdomains to be passed as well, for example hello.dev.example.com is given to gunicorn as hello.example.com. Please tell me how to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nurlan, 2016-12-22
@balyko

Try like this:

server{
  ...
  server_name ~^(?<pre_host>.+)\.dev\.example\.com$;
}

location / {
  ...
  proxy_set_header Host $pre_host.example.com;
}

Finalochka:7e9e4613ed.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question