關於設置tomcat端口為80的事


今天有人要求tomcat服務器的訪問地址不能帶端口訪問, 也就是說只能用80端口訪問網站.

 

那么問題來了, Ubuntu系統禁止root用戶以外的用戶訪問1024以下的商品, 因此tomcat

 

默認為8080也是有原因的.

 

因此,多才多藝的網友提供了普遍的兩種方案:

 

1, 修改conf/server.xml中connector的port為80, 使用root用戶運行tomcat.完美解決問題(ubuntu系統)

windows亦是修改conf/server.xml中的connector的port為80, 直接運行bin/startup.bat就行了.

 

2. ubuntu有個iptables NAT

即可控制訪問ubuntu系統的請求通過iptables來轉發,比如你訪問80端口的請求,可以重定向到8080端口上去,

這樣tomcat監聽的8080端口就能收到80端口的訪問請求了.

 

看一下歪國人的配置:

Iptables redirect port 80 to port 8080

The problem:

  • You have a linux server
  • Install tomcat or any other application server
  • You don't want the application server to run as root, therefore it cannot listen to any of the ports 80 (http) or 443 (https)
  • From outside, though, the application server must be accessible on ports 80 / 443

 

The most popular approach:

  • Create an ordinary user specificaly for the application server (ex: tomcat)
  • Configure it to listen to a port bigger than 1024 - actually Tomcat comes by default configured to 8080 instead of 80 and 8443 instead of 443
  • Redirect the incoming connections from port 80 to 8080

The redirection is done by using the following iptables commands issued in sequence. The first one will configure the machine to accept incoming connections to port 80, the second does the same for port 8080 and the third one will do the actual rerouting. Please proceed in a similar manner for ports 443 forwarded to 8443

iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

 

然而事情你總以為算結束了!!!

 

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:443 redir ports 8443
2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 8080

Chain INPUT (policy ACCEPT)
num target prot opt source destination

Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
1 REDIRECT tcp -- 0.0.0.0/0 127.0.0.1 tcp dpt:80 redir ports 8080

Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination

 

但是:

$service iptables save

iptables: unrecognized service

WHY!??

http://m.blog.csdn.net/blog/FENGQIYUNRAN/21830541

於是准備着手解決,解決思路很是明了,就是首先確定Linux是否安裝了 iptables 。

service iptables status

但是仍然提示:iptables:unrecognized service。准備安裝,根據不同的Linux內核選擇不同的方法如下:

yum install iptables   #CentOS系統
apt-get install iptables    #Debian系統

但是提示已經安裝,那為什么狀態顯示是未識別的服務呢?繼續找原因。繼續研究發現可能是由於沒有安裝iptables-ipv6,於是采用

sudo apt-get install iptables-ipv6進行安裝,但提示Unable to locate package錯誤得錯誤。

考慮到軟件間的不兼容,無奈先進行更新:sudo apt-get update,更新后重新安裝仍然無法解決定位的問題。

於是采用apt-get install iptables*進行所有可能性查找和安裝。經過一輪安裝后iptables:unrecognized service的問題仍然沒有解決。

繼續研讀相關資料,最終發現問題所在:

在ubuntu中由於不存在 /etc/init.d/iptales文件,所以無法使用service等命令來啟動iptables,需要用modprobe命令。
啟動iptables
modprobe ip_tables
關閉iptables(關閉命令要比啟動復雜)
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
modprobe -r ip_tables
依次執行以上命令即可關閉iptables,否則在執行modproble -r ip_tables時將會提示
FATAL: Module ip_tables is in use.
 
 不過, 最后這個NAT始終沒有起到作用~~~~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM