Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Linux Tomcat 綁定 80 埠 Permission Denied 解決:非 Root 權限與 iptables 轉發

編輯:關於伺服器

錯誤現象:Root 啟動 Tomcat,報 java.net.BindException: Permission denied:80,但 netstat -lnp|grep 80 顯示無佔用。

根本原因

Linux 系統保留 1024 以下埠為特權埠,僅 Root 進程可綁定。雖用 Root 啟動,但 Tomcat 內部可能切換至非特權用戶(如 tomcat 用戶)導致綁定失敗。

標準解決方案:Tomcat 綁 8080 + iptables DNAT

  1. 修改 server.xml:<Connector port="8080" ... />
  2. 配置 iptables 轉發:
    iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
    # 若需 HTTPS
    iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443
  3. 持久化規則(CentOS/RHEL):
    service iptables save
    # 或
    iptables-save > /etc/sysconfig/iptables

替代方案對比

方案優點缺點
iptables 轉發(推薦)標準、無需修改程式、效能損耗極低需 Root 權限、重啟後需持久化
authbind允許非 Root 綁定特權埠需額外安裝配置、Java 支援有限
setcap CAP_NET_BIND_SERVICE賦予 Java 二進制特權安全風險、升級失效、不推薦生產
Nginx 反向代理功能豐富、SSL 卸載、靜態分離增加一層、配置較複雜

驗證步驟

curl -I https://localhost
# 應返回 200 OK,Server 標頭顯示 Tomcat 版本
Copyright © Windows教程網 All Rights Reserved