Linux – Route HTTP(S) traffic to specific IP based on requested URL on Linux

httpiptableslinuxroutingvpn

I have an Ubuntu Server in the cloud. Let's call it ServerA.
ServerA has an apache2 installed and is serving a default page on HTTP requests. mydomain.com is pointing to the ServerA's IP and thus the default ServerA's site is available via this domain name as well.

ServerA has an OpenVPN installed and works as a VPN gateway for different clients to connect.

When ServerB connects to ServerA as an OpenVPN client, it is assigned a local IP in the OpenVPN network that ServerA's OpenVPN has created.

I set myotherdoamin.com to point to the same ServerA's IP. I would like ServerA to be configured that way so that if an HTTP request to mydomain.com comes in – it is routed to ServerA's apache2 and the local site is served. In case a request to myotherdomain.com comes in, I would like ServerA to route the request to a connected ServerB's HTTP port, thus returning a VPN client's HTTP response.

My questions are:

  1. Is iptables capable fo routing rules based on HTTP request headers?
  2. Is apache2 capable to routing requests to other IP's based on HTTP request headers?
  3. Which application is best for handling my desired behavior?

Thank you.

Best Answer

Is iptables capable of routing rules based on HTTP request headers?

No, it's not. IPTables works at layer 3/4, and HTTP request headers are layer 7.

Is apache2 capable to routing requests to other IP's based on HTTP request headers?

Yes, using mod_proxy, Apache can proxy those requests to another server.

Which application is best for handling my desired behavior?

As stated above, mod_proxy will work for you, but any other reverse proxy that can operate at layer 7 will do the trick. Along with Apache's mod_proxy, Nginx and HAproxy are probably the most common ones.