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?
1 Answer
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.
-
-
-
1And what about the
uhttpd
server that comes by default with OpenWRT? Commented Nov 9, 2017 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. Commented Nov 9, 2017 at 5:06