Search code examples
http-redirectsubdomain

Redirecting subdomain to ip:port


You know how in computer programming, perfection is not even close to be perfect?

Well, I have my server running on port 8080. I did the redirections needed with iptables to redirect 80 to 8080. So when i go to www.domain.com, it works well

I have a second server running on port 8081. When i go to www.domain.com:8081, it works well.

Everything works! But hey, remember my first rule...

Now, I want to to access my second site by using www.sub.domain.com (or www.domain.com/sub, i don't care) Basically, I want www.sub.domain.com (or www.domain.com/sub) to redirect to www.domain.com:8081

From now, what I understand is that if I had a second IP on my server, it would be very easy. But it's not a good solution, because I could want to have like 100 subdomains, and I don't want to pay for this.

Any thoughts?


Solution

  • I would suggest you to install a reverse proxy to your webserver. Apache and Nginx are supporting this;

    For instance, your webserver listens on port 80, and depending on the requested domain (in a serverblock for nginx, vhost for apache) you can ask him to internally redirect to a specific url:port.

    for apache: (see "Using Virtual_host and mod_proxy together") https://httpd.apache.org/docs/2.4/vhosts/examples.html

    for nginx: https://www.nginx.com/resources/admin-guide/reverse-proxy/