6

I have multiple subdomains, all pointing to one machine, and one IP address. On this machine, I want to have nginx acting as a reverse proxy, and depending on which subdomain was used to access the machine, I want it to reverse proxy to a different server. All the examples I've seen of using nginx as a reverse proxy use location, but as I understand that only works for the path, not for different subdomains. How can I achieve what I want?

  • 2
    This might be a bit too broad. Can you post more about what you've tried already? – Will Feb 1 '16 at 5:47
15

Unless I completely misread your question: You simply set up server blocks for each sub-domain and the define the correct reverse proxy for the root of that subdomain i.e. something along the lines of:

 server {
        server_name subdomain1.example.com;
        location / {
            proxy_pass       http://hostname1:port1;
        }
 }
 server {
        server_name subdomain2.example.com;
        location / {
            proxy_pass       http://hostname2:port2;
        }
 }
  • Sorry for late response, but I got it working! – markasoftware Feb 4 '16 at 1:00
3

Pretty much the same way.

location /foo { rewrite ^/foo(.+)$ /$1 break; proxy_pass http://foo; } location /bar { rewrite ^/bar(.+)$ /$1 break; proxy_pass http://bar; }

  • 1
    This wasn't exactly what I was looking for. See the accepted answer – markasoftware Feb 4 '16 at 1:01
  • 1
    Chewing and digesting is the requester burden, not mine. – drookie Feb 4 '16 at 7:21

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.