linux 端口转发问题

在linux上的tomcat,如果要以80端口运行,必须为root权限,奇葩吧!

所以,要进行端口转发,将 80 转到 8080

方法:

修改iptables:

*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
##-A PREROUTING -p tcp -m tcp --dport 8012 -j DNAT --to-destination 192.168.1.253:8012 
##-A POSTROUTING -p tcp -m tcp --dport 8012 -j MASQUERADE
-A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
-A OUTPUT -d localhost -p tcp --dport 80 -j REDIRECT --to-ports 8080

COMMIT


###############################################################################################################


*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited



COMMIT

其中 :PREROUTING是用于入站规则,OUTPUT用于出站,所以写了两条,否则服务器上,将无法访问localhost (加上-d http://localhost 是为了只针对localhost)

发表评论