OpenWRT redirect incoming WAN traffic based on domain name

2

I have a router with OpenWRT firmware and two computers in the local network which host different websites. Also, I have two domain names (domain1 and domain2) both pointing to my router's WAN IP address. I have set up the router to accept incoming requests on port 80 and redirect them to the first computer. Therefore, all traffic is redirected to the single machine. How do I make the router distinguish requests depending on domain name so that whenever I type domain2 it redirects me to the second computer?

user2513149

Posted 2017-11-08T19:02:55.220

Reputation: 121

Answers

4

This is not the purpose of a router, routers w/NAT operate at Layer 3 and Layer 4 (IP and Port) what you need operates at layer 7 (application: http).

TLDR; You need to configure a web proxy to route traffic based on HOST headers, which should be a redundant set of systems like haproxy/keepalived or nginx. Which there are MANY Q/A's here on how to do this.

mod_proxy Fowarding Based on Request Host Header

All that said, if you needed to install nginx on your router you could.

opkg update
opkg install nginx

/etc/nginx/nginx.conf

server {
  server_name site1.example.com;

        location / {
          # app1 reverse proxy
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://192.168.1.3:80;
        }

   access_log      /var/log/nginx/site1.example.com_access.log;
   error_log       /var/log/nginx/site1.example.com_error.log;

}

server {
  server_name site1.mydomain.com;

        location / {
          # app2 reverse proxy
          proxy_pass http://192.168.1.4:80;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

   access_log      /var/log/nginx/site1.mydomain.com_access.log;
   error_log       /var/log/nginx/site1.mydomain.com_error.log;

}

Where 192.168.1.3 and 192.168.1.4 are your backends.

https://wiki.openwrt.org/doc/howto/http.nginx

Jacob Evans

Posted 2017-11-08T19:02:55.220

Reputation: 196

Is it possible to do it using squid? – user2513149 – 2017-11-08T20:03:06.480

Technically yes but squid is a forward proxy by design – Jacob Evans – 2017-11-08T20:20:10.083

And what about the uhttpd server that comes by default with OpenWRT? – user2513149 – 2017-11-09T04:28:17.073

No experience with that, maybe open a new thread specific to uhttpd and openwrt in super user exchange, this is for business systems. – Jacob Evans – 2017-11-09T05:06:29.680