0

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?

migrated from serverfault.com Nov 9 '17 at 6:47

This question came from our site for system and network administrators.

3

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

  • Is it possible to do it using squid? – user2513149 Nov 8 '17 at 20:03
  • Technically yes but squid is a forward proxy by design – Jacob Evans Nov 8 '17 at 20:20
  • And what about the uhttpd server that comes by default with OpenWRT? – user2513149 Nov 9 '17 at 4:28
  • 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 Nov 9 '17 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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