Short question:
How do you enable port forwarding on only a single host ip address?
Backstory:
My Centos 7 server has 5 ip addresses. Previously I had apache listening on all of them and various domains assigned to those ip addresses which were resolved with virtual hosts.
I changed the Listen
directive in httpd.conf so that now apache only listens to 4 of the ip addresses
Using node.js I created another server instance, but it won't let me listen on the standard port 80 without elevated permissions. I don't want to run it with elevated permissions.
I'd like to port forward port 80 to something like 8080, but only on the one ip address without affecting traffic directed at the other 4 ip addresses. It's important that traffic on other ip addresses are not affected by the rule.
I think the solution will look similar to:
firewall-cmd --zone=public --add-masquerade --permanent
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
The other questions and answers I've found have to do with source ip addresses instead of host ip addresses.
A firewalld zone can be specified either by interface or by source address, but you want to filter by destination address. You'll need a rich rule to handle this particular situation.
Such a rich rule may look like:
See the
firewalld.richlanguage(5)
man page for documentation on rich rules.Once your rich rule is working, remember to make it permanent with
or adding
--permanent
to the previous invocation.Suppose out of your five IP addresses, one is
192.0.2.3
, which you'd like to forward port 80 on. You can useiptables
to forward ports on this IP address like so:This should leave all your other addresses unaffected. Should a program bind to port 80 on all addresses,
192.0.2.3
will still redirect to port 8080 since it is redirected prior to routing.Note: I admit I am proposing a weird and possibly unsupported solution, which may get broken on future
firewalld
versions. Nonetheless, it will certainly work on a CentOS 7 server.I suggest you to directly add such port forwarding rule into the
iptables
/ip6tables
stack. You can achieve that by usingfirewalld
's direct rules. For example:I figured that CentOS's
firewalld
creates aPRE_<zone>_allow
chain innat
table for each active zone, which gets evaluated when packets that matches zone definitions arrive. Soiptables
/ip6tables
rules can be added there.I would configure reverse proxy with different ports for different domains but same IP address. In the httpd config page, I would add Listen directives for each ports (for example: Listen 80, Listen 8081, Listen 8082 etc), include vhosts configuration port in httpd config page and update vhosts configuration page according to the ports.