自定义端口,将端口80的TCP传递给8081

修改server.xml中Shutdown port,Connector port,ajp port和Redirect port的端口

从8080端口改变端口
<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
               
               
将端口80的TCP连接传递给端口8080
在Linux下检查iptables特性是否处于enabled状态
[root@wls-132 bin]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@wls-132 bin]# 
如果看到相似的输出,一般就是可用状态。

通过下列两条命令,你可以让80端口的所有TCP连接按机器配置寻址到所有网络目的地址
[root@wls-132 bin]# iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
[root@wls-132 bin]# iptables -t nat -I OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8080
[root@wls-132 bin]# 

(实验时访问不到应用)如果只想把机器配置的一个IP地址的连接传递给8080端口,那么可以再追加目的地址ip是选择性的使用--dst切换开关,如下所示:
[root@wls-132 bin]# iptables -t nat -I PREROUTING -p tcp --dst 192.168.1.106 --dport 80 -j REDIRECT --to-ports 8081
[root@wls-132 bin]# iptables -t nat -I OUTPUT -p tcp --dst 192.168.1.106 --dport 80 -j REDIRECT --to-ports 8081
[root@wls-132 bin]# 

查看过滤规则
[root@wls-132 bin]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
REDIRECT   tcp  --  anywhere             192.168.1.106       tcp dpt:http redir ports 8081 
REDIRECT   tcp  --  anywhere             anywhere            tcp dpt:http redir ports 8081 
REDIRECT   tcp  --  anywhere             anywhere            tcp dpt:http redir ports 8080 

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
REDIRECT   tcp  --  anywhere             192.168.1.106       tcp dpt:http redir ports 8081 
REDIRECT   tcp  --  anywhere             anywhere            tcp dpt:http redir ports 8081 
REDIRECT   tcp  --  anywhere             anywhere            tcp dpt:http redir ports 8080 

删除过滤规则
[root@wls-132 bin]# iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
[root@wls-132 bin]# iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8081
[root@wls-132 bin]# iptables -t nat -D OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8080
[root@wls-132 bin]# iptables -t nat -D OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8081

重定向方法的缺点在于:为了显示真实的端口,Tomcat要重写URL。
假定访问的www.example.com。如果一个用户在其浏览器地址位置输入
http://www.example.com/,那么依Web应用程序的内容而定,Tomcat会对重写该地址,
且该用户会在自己的浏览器位置看到http://www.example.com:8080/index.html的地址。

Tomcat会认定请求来源与8080端口,因为他在8081端口上打开了web服务连接器,因此无论何时发送重定向,
它都会追加上端口号8080,除非按如下方式在server.xml连接器配置文件中增加了proxyPort="80"的信息:
    <Connector port="8081" protocol="HTTP/1.1" proxyPort="80"
               connectionTimeout="20000"
               redirectPort="8444" />
               
如果Tomcat充当了主页功能,可以设置proxyName="www.example.com"。

iptables参考:http://caisangzi.blog.51cto.com/6387416/1286432;
http://hi.baidu.com/maimixiande/item/7a54d4c2caac0b25ee4665c0

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值