Ubuntu如何配置Tomcat访问80端口

说明:在Ubuntu上非root用户只能使用1024端口以上端口,即0-1023对非root用户为禁用状态。需要使用iptables的转发功能,把80端口(外部访问端口)消息转发给8080端口(目标端口)。
本文所述操作预期达成效果:Web访问地址不需要加端口号即可正常访问相关应用程序。

1)查看系统是否安装iptables防火墙。
#whereis iptables
iptables:/sbin/iptables/etc/iptables.rules/usr/share/iptables/usr/share/man/man8/iptables.8.gz

上述返回结果为已安装状态,按3)进行后续操作,否则按2)所述进行安装。

2)安装iptables防火墙。
#apt-get install iptables
3) 查看防火墙配置信息。
#iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
4) 配置防火墙访问控制规则。

创建iptables.rules文件:

#vim /etc/iptables.rules

添加下列内容:

*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:syn-flood - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8443 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable
COMMIT

22端口为ssh默认端口
80端口为HTTP默认端口
443端口为HTTPS默认端口
8080端口为Tomcat默认的HTTP端口
8443端口为Tomcat默认的HTTPS端口

5) 使防火墙规则生效
#iptables-restore < /etc/iptables.rules
6) 配置防火墙规则开机启动。

创建iptables文件:

#vim /etc/network/if-pre-up.d/iptables

添加下列内容:

!/bin/bash
iptables-restore < /etc/iptables.rules
7) 为iptables文件添加执行权限。
#chmod +x /etc/network/if-pre-up.d/iptables
8) 查看规则是否生效。
#iptables -L -n
Chain INPUT (policy ACCEPT)
target   prot opt source    destination     
ACCEPT   all -- 0.0.0.0/0  0.0.0.0/0  
ACCEPT   all -- 0.0.0.0/0  0.0.0.0/0  state RELATED,ESTABLISHED
ACCEPT   tcp -- 0.0.0.0/0  0.0.0.0/0  state NEW tcp dpt:22
ACCEPT   tcp -- 0.0.0.0/0  0.0.0.0/0  state NEW tcp dpt:80
ACCEPT   tcp -- 0.0.0.0/0  0.0.0.0/0  state NEW tcp dpt:443
ACCEPT   tcp -- 0.0.0.0/0  0.0.0.0/0  state NEW tcp dpt:8080
ACCEPT   tcp -- 0.0.0.0/0  0.0.0.0/0  state NEW tcp dpt:8443
ACCEPT   tcp -- 0.0.0.0/0  0.0.0.0/0  state NEW tcp dpt:8012
ACCEPT   tcp -- 0.0.0.0/0  0.0.0.0/0  state NEW tcp dpt:1521
ACCEPT   icmp -- 0.0.0.0/0  0.0.0.0/0  limit: avg 100/sec burst 100
ACCEPT   icmp -- 0.0.0.0/0  0.0.0.0/0  limit: avg 1/sec burst 10
syn-flood tcp -- 0.0.0.0/0  0.0.0.0/0  tcp flags:0x17/0x02
REJECT   all -- 0.0.0.0/0  0.0.0.0/0  reject-with icmp-host-prohibited
9) 增加转发规则。

将80端口转发到8080端口:

#iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
10) 查看80端口相关的转发规则。
#iptables -t nat -L -n  | grep 80
REDIRECT   tcp  --  0.0.0.0/0    0.0.0.0/0       tcp dpt:80 redir ports 8080
11) 配置Tomcat服务端口。

设置HTTP端口为8080:
略。8080端口是Tomcat默认Web服务端口。

12) 删除转发规则。

将转发规则以数字序号排列:

#iptables -t nat -L -n --line-numbers
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source         destination         
1    REDIRECT   tcp  --  0.0.0.0/0      0.0.0.0/0      tcp dpt:80 redir ports 8080

删除num为1的转发规则:

#iptables -t nat -D PREROUTING 1

上述配置完成后,即可使用ip地址(或域名)+应用程序页面访问路径。如需直接使用ip地址(或域名)访问应用程序默认页面,应另外配置。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值