LNMP中如何配置防火墙规则

1427
2025/2/21 9:23:52
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在LNMP(Linux, Nginx, MySQL, PHP)环境中配置防火墙规则可以通过多种方式实现,具体取决于你的需求和使用的Linux发行版。以下是几种常见的方法:

使用iptables配置防火墙规则

  1. 检查防火墙状态:

    sudo iptables -L
    
  2. 设置默认策略:

    sudo iptables -P INPUT DROP
    sudo iptables -P FORWARD DROP
    sudo iptables -P OUTPUT ACCEPT
    
  3. 配置允许的入站规则:

    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT  # 允许HTTP流量
    sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 允许HTTPS流量
    
  4. 保存规则:

    sudo service iptables save
    
  5. 启用防火墙:

    sudo systemctl enable iptables
    sudo systemctl start iptables
    
  6. 重启防火墙使配置生效:

    sudo service iptables restart
    

使用firewalld配置防火墙规则

  1. 检查防火墙状态:

    sudo systemctl status firewalld
    
  2. 临时关闭防火墙:

    sudo systemctl stop firewalld
    
  3. 永久关闭防火墙:

    sudo systemctl disable firewalld
    
  4. 配置防火墙区域:

    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --permanent --zone=public --add-service=https
    
  5. 重新加载防火墙配置:

    sudo firewall-cmd --reload
    

使用ngx_lua_waf配置防火墙规则

  1. 安装ngx_lua_waf:

    wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip
    unzip ngx_lua_waf.zip
    mv ngx_lua_waf-master /usr/local/nginx/conf/waf
    
  2. 修改nginx配置文件:

    http {
        lua_package_path "/usr/local/nginx/conf/waf/?.lua";
        lua_shared_dict limit 10m;
        init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
        access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;
    
        config.waf.RulePath = "/usr/local/nginx/conf/waf/wafconf/"
        config.waf.attacklog = "off"
        config.waf.logdir = "/usr/local/nginx/logs/hack/"
        config.waf.UrlDeny = "on"
        config.waf.Redirect = "on"
        config.waf.CookieMatch = "on"
        config.waf.postMatch = "on"
        config.waf.whiteModule = "on"
        config.waf.black_fileExt = {"php", "jsp"}
        config.waf.ipWhitelist = {"127.0.0.1"}
        config.waf.ipBlocklist = {"1.0.0.1"}
        config.waf.CCDeny = "on"
        config.waf.CCrate = "100/60"
    }
    
  3. 重启nginx:

    sudo service nginx reload
    

以上是几种在LNMP环境中配置防火墙规则的方法,你可以根据自己的需求选择合适的方法进行配置。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: linux中ioctl() FIONREAD使用报错怎么解决