插件窝 干货文章 Fedora 28 server web 无法访问之配置防火墙

Fedora 28 server web 无法访问之配置防火墙

防火墙 sudo bash 服务 349    来源:    2025-05-11

Fedora 28 服务器 Web 访问问题:防火墙配置指南

当 Fedora 28 服务器上的 Web 服务无法访问时,防火墙配置通常是主要原因之一。以下是详细的解决方案:

1. 检查防火墙状态

首先确认防火墙是否正在运行:

sudo systemctl status firewalld

如果防火墙未运行,可以启动它:

sudo systemctl start firewalld
sudo systemctl enable firewalld

2. 检查当前防火墙规则

查看当前防火墙配置:

sudo firewall-cmd --list-all

3. 允许 HTTP/HTTPS 服务

根据您的 Web 服务类型,添加相应规则:

对于 HTTP (端口 80):

sudo firewall-cmd --permanent --add-service=http

对于 HTTPS (端口 443):

sudo firewall-cmd --permanent --add-service=https

如果您使用非标准端口 (如 8080):

sudo firewall-cmd --permanent --add-port=8080/tcp

4. 重新加载防火墙配置

应用更改:

sudo firewall-cmd --reload

5. 验证规则是否生效

再次检查防火墙规则:

sudo firewall-cmd --list-all

您应该能看到 http 和/或 https 服务在服务列表中。

6. 其他可能需要的配置

如果使用 SELinux:

# 允许 HTTP 服务
sudo setsebool -P httpd_can_network_connect on

# 如果您修改了默认端口,需要告诉 SELinux
sudo semanage port -a -t http_port_t -p tcp 8080

检查 Web 服务是否监听正确接口:

sudo netstat -tulnp | grep -E '80|443'

确保您的 Web 服务正在监听 0.0.0.0 (所有接口) 或特定的公共 IP 地址。

7. 测试连接

从另一台机器测试连接:

curl http://your-server-ip

或者使用浏览器访问服务器 IP。

8. 持久化配置

确保所有更改在重启后仍然有效:

sudo firewall-cmd --runtime-to-permanent

常见问题排查

  1. 端口被其他服务占用

    sudo ss -tulnp | grep :80
    
  2. Web 服务未运行

    sudo systemctl status httpd  # 对于 Apache
    sudo systemctl status nginx # 对于 Nginx
    
  3. 网络连接问题

    ping your-server-ip
    traceroute your-server-ip
    

通过以上步骤,您应该能够解决 Fedora 28 服务器因防火墙配置导致的 Web 访问问题。