大网数据广告活动
服务器运维问答

iptables: Packets about generated locally and heading to local

· 52CE 我爱测

Does packet which is generated locally and heading to local take PREROUTING chain? I added some rules to iptables to forward http request for my public IP to locally binded Nginx. iptables -t nat -A PREROUTING -i enp3s0f0 -p tcp …

问题原文

原帖链接 · J Lee · CC BY-SA 3.0

Does packet which is generated locally and heading to local take PREROUTING chain?

I added some rules to iptables to forward http request for my public IP to locally binded Nginx.

iptables -t nat -A PREROUTING -i enp3s0f0 -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1
iptables -t nat -A POSTROUTING -o lo -p tcp --sport 80 -j SNAT --to-source xxx.yyy.zzz.www

and also set policy of filter tables FORWARD chain to ACCEPT.

As a result, these rules worked and I can access to Nginx in another machine, but I can't access to xxx.yyy.zzz.www in the machine which has xxx.yyy.zzz.www IP. (with Curl and Chrome)

Why can't I access to Nginx in the xxx.yyy.zzz.www machine?

已采纳解答

原帖链接 · Anton Danilov · CC BY-SA 3.0

Nope, the locally originated packets don't pass the PREROUTING chains (see this wiki diagram - the path of local packets starts at the local process block). Also, these packets pass only the INPUT/OUTPUT chains and never the FORWARD.

To redirect the local originated packets you should insert the DNAT/REDIRECT rule into the nat/OUTPUT chain:

iptables -t nat -A OUTPUT -p tcp --dst xxx.yyy.zzz.www --dport 80 \
  -j DNAT --to-destination 127.0.0.1

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。