Search code examples
nginxsubdomain

nginx redirect subdomain to seperate server ip


I have a dynamic IP which I manage using ddclient. I use no-ip to maintain the hostnames to point to my IP.

I have www.somename.com, sub.somename.com and app.somename.com. Obviously, these all point to my IP. The first two are a couple of wordpress pages on a server (server1) running NGINX, with separate configs in sites-available for each site. The latter is a separate application server (server2) running GitLab.

My router does not allow me to switch on subdomain, so all port 80 traffic is routed to server1. I'm hoping there is a config I can apply in nginx that will allow me to send all traffic for app.somename.com to a local IP address on my network (192.168.0.nnn), but keep the address of the page as app.subdomain.com.

Right now, I have :-


/etc/nginx/site-available$ ls
somename.com    domain    sub.somename.com   app.somename.com

The relevant ones are linked in sites-enabled. For the app server, I have :-

server {
        server_name app.somename.com;
        location / {
                proxy_pass http://192.168.0.16:80;
        }
}

The problem, is that in the browser address bar, this results in :-

http://192.168.1.16/some/pages

Where I want :-

http://app.somename.com/some/pages

How do I resolve this?


Solution

  • You could try like this!

    server {
            server_name app.somename.com;
            location / {
                    proxy_pass http://192.168.0.16:80;
                    proxy_set_header Host app.somename.com;
            }
    }