雲服務器部署 Web 服務後無法訪問,90% 原因是 80 端口未放行。CentOS 7+ 默認使用 firewalld,CentOS 6 使用 iptables,本文覆蓋兩種模式的完整操作。
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --list-all
firewall-cmd --list-ports
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT # 保留 SSH
/etc/rc.d/init.d/iptables save
service iptables restart
vim /etc/sysconfig/iptables
# 在 COMMIT 前添加
-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
service iptables restart
iptables -L -n -v --line-numbers | grep 80
# 輸出應包含:ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
| 現象 | 原因 | 解決 |
|---|---|---|
| 本地 curl 正常,外部不通 | 雲廠商安全組未放行 | 控制台配置安全組入站規則 80/443 |
| firewalld 與 iptables 衝突 | 兩者同時運行 | CentOS 7 僅用 firewalld,禁用 iptables-services |
| 端口顯示開放但無響應 | Web 服務未啟動或監聽 127.0.0.1 | 確認 nginx/apache 監聽 0.0.0.0:80 |