首页 > 解决方案 > Firewalld - 将 eth1:0 上收到的流量转发到与 eth1 不同的 IP

问题描述

我在firewalld中有以下两个区域:

  zone1 (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth1
  sources:
  services:
  ports: 80/tcp 443/tcp
  protocols:
  masquerade: yes
  forward-ports: port=80:proto=tcp:toport=80:toaddr=192.168.0.1       
        port=443:proto=tcp:toport=443:toaddr=192.168.0.1
  source-ports:
  icmp-blocks:
  rich rules:

zone2 (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth1:0
  sources:
  services:
  ports: 80/tcp 443/tcp
  protocols:
  masquerade: yes
  forward-ports: port=80:proto=tcp:toport=80:toaddr=192.168.0.2
        port=443:proto=tcp:toport=443:toaddr=192.168.0.2
  source-ports:
  icmp-blocks:
  rich rules:

eth1 是 172.16.1.1 的真实网卡 eth1:0 是 172.16.1.2 的虚拟网卡

在一台物理机器上。

它们由

ifconfig eth1 172.16.1.1
ifconfig eth1:0 172.16.1.2

我需要的是当我用网络浏览器点击 172.16.1.1 时能够看到 192.168.0.1 上的网站,当我用网络浏览器点击 172.16.1.2 时能够看到 192.168.0.2 上的网站。

例如。我想通过端口 80 和 443 将流量转发到 eth1 (172.16.1.1) 上的 192.168.0.1 和 eth1:0 (172.16.1.2) 上的 192.168.0.2。

使用上述配置,firewalld / iptables IGNORES eth1:0 - 例如,如果我点击 172.16.1.1,我会在 192.168.0.1 上获得网站。但是如果我点击 172.16.1.2 我仍然会在 192.168.0.1 上获得网站,而不是 192.168.0.2

例如。虚拟网卡 eth1:0 似乎等同于 firewalld,因为 eth1 - 端口 80 上到 172.16.1.1 (eth1) 或 172.16.1.2 (eth1:0) 的 HTTP 流量都被发送到 192.168.0.1,忽略转发规则集在 eth1:0 上。

如何获得由 firewalld 控制的 firewalld / iptables 到

将 172.16.1.1 (eth1) 上 :80 和 :443 接收到的流量转发到 192.168.0.1

将 :80 和 :443 上 172.16.1.2 (eth1:0) 接收到的流量转发到 192.168.0.2

而不仅仅是将 172.16.1.1 和 172.16.1.2 上的所有流量转发到 192.168.0.1?

谢谢!

标签: tcpipportforwardingipv4firewalld

解决方案


好的,这是通过放弃 firewalld 并使用 iptables 解决的:

下面的这些命令允许我做我需要的事情,例如一个 HTTP 请求

172.16.1.1 - 在 eth0 上 172.16.1.2 - 在 eth1

被转发到不同的机器上

192.168.0.1 192.168.0.2

取决于对端口 80 的 HTTP 请求是否命中 172.16.1.1 或 172.16.1.2。

systemctl restart iptables
systemctl restart rsyslog
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X

iptables -t nat -A PREROUTING -d 172.16.1.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.1:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.0.1 --dport 80 -j SNAT --to-source 172.16.1.1

iptables -t nat -A PREROUTING -d 172.16.1.2 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.2:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.0.2 --dport 80 -j SNAT --to-source 172.16.1.2

上面的代码允许我在其 172.16.1.1 或 172.16.1.2 NIC 的 IP 地址上访问我的“代理”服务器,如果命中 172.16.1.1,则将 HTTP 请求转发到 192.168.0.1,如果命中则转发到 192.168.0.2 172.16.1.2 被击中。

这正是票的用途,因此解决了。

问候

斯特凡


推荐阅读