HAProxy 安全配置

在HAProxy案例中加入安全配置,可以参考以下步骤和示例配置。这个案例将展示如何在HAProxy中实现基本的安全措施,包括限制访问、启用HTTPS、配置HSTS以及使用ACLs进行细粒度控制。

1. 限制访问

使用ACLs来限制哪些IP地址或网络可以访问HAProxy服务。

plaintext 复制代码
frontend http_front
    bind *:80
    acl allowed_ips src 192.168.1.0/24
    http-request allow if allowed_ips
    http-request deny

2. 启用HTTPS

配置SSL/TLS证书来加密客户端和HAProxy之间的通信。

plaintext 复制代码
frontend https_front
    bind *:443 ssl crt /path/to/your/certificate.pem alpn h2,http/1.1
    default_backend https_back

3. 配置HSTS

启用HSTS头以防止协议降级攻击。

plaintext 复制代码
http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains" if { ssl_fc }

4. 使用ACLs进行细粒度控制

通过ACLs实现更复杂的访问控制逻辑。

plaintext 复制代码
frontend https_front
    bind *:443 ssl crt /path/to/your/certificate.pem alpn h2,http/1.1
    acl is_admin path_beg /admin
    use_backend admin_backend if is_admin
    default_backend user_backend

backend admin_backend
    server admin_server 192.168.1.101:80 check

backend user_backend
    server user_server 192.168.1.102:80 check

5. 防止DDoS攻击

使用限速和连接数限制来减轻DDoS攻击的影响。

plaintext 复制代码
frontend https_front
    bind *:443 ssl crt /path/to/your/certificate.pem alpn h2,http/1.1
    limit_req zone=one burst=5 nodelay
    default_backend https_back

backend https_back
    balance roundrobin
    server web1 192.168.1.101:80 check
    server web2 192.168.1.102:80 check

完整示例配置

plaintext 复制代码
global
    log /dev/log local0
    maxconn 4096
    ssl-default-bind-ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES1:EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH

defaults
    log global
    mode http
    option httplog
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http_front
    bind *:80
    acl allowed_ips src 192.168.1.0/24
    http-request allow if allowed_ips
    http-request deny

frontend https_front
    bind *:443 ssl crt /path/to/your/certificate.pem alpn h2,http/1.1
    acl is_admin path_beg /admin
    use_backend admin_backend if is_admin
    default_backend user_backend
    http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains" if { ssl_fc }
    limit_req zone=one burst=5 nodelay

backend admin_backend
    server admin_server 192.168.1.101:80 check

backend user_backend
    balance roundrobin
    server web1 192.168.1.101:80 check
    server web2 192.168.1.102:80 check

注意事项

  • 证书管理:确保SSL/TLS证书有效且安全。
  • 日志监控:定期检查HAProxy日志以发现异常行为。
  • 定期更新:保持HAProxy及其依赖库的最新版本。

通过这些配置,您可以显著提高HAProxy部署的安全性。务必根据您的具体环境和需求进行调整和优化。

相关推荐
环流_1 天前
网络原理-TCP协议
服务器·网络·tcp/ip
看海的四叔1 天前
【SQL】SQL-常见窗口函数有哪些-上篇
数据库·hive·sql·mysql·数据分析·窗口函数
6190083361 天前
win wsl2 指定目录安装Ubuntu-24.04开启ssh sftp
linux·ubuntu·ssh
pele1 天前
如何处理ORA-01152报错_恢复未完成导致的数据文件仍需介质恢复
jvm·数据库·python
上海云盾-小余1 天前
出海业务安全架构搭建:跨境云主机合规部署与全域抗攻击策略
安全·安全架构
easy_coder1 天前
一次部署阻塞的根因分析:自动提交与手动提交链路混用的代价
运维·云计算
IntMainJhy1 天前
【flutter for open harmony】Flutter SQLite 本地数据库的鸿蒙化适配与实战指南
数据库·flutter·sqlite
一直跑1 天前
同一台服务器上(同局域网)的其他账号访问自己的数据(没有sudo权限和无 ACL和无共同组)
java·linux·服务器
qq_372154231 天前
SQL如何避免隐式类型转换导致的慢查询_参数类型对齐与索引失效
jvm·数据库·python
qq_342295821 天前
MySQL怎样在触发器中引用新旧数据行_NEW与OLD关键字详解
jvm·数据库·python