Sign inSign up

tw199501/haproxy

By tw199501

•Updated 11 months ago

Image
0

1.1K

tw199501/haproxy repository overview

⁠Elf-Nginx Containerized Deployment Solution

ā šŸ“– Project Overview

Elf-Nginx is an enterprise-grade containerized deployment solution based on Nginx 1.29.3. It integrates high availability, security protection, geolocation recognition, and automated operations.

ā šŸš€ Key Features
  • High Performance: Custom-compiled from source with multiple performance-oriented modules.
  • High Availability: Keepalived-based active/standby failover.
  • Security Protection: Multiple layers including ModSecurity WAF, GeoIP, and IP filtering.
  • Automation: Scheduled updates of GeoIP databases and Cloudflare configuration.
  • Modular Design: Dynamic module loading with flexible configuration management.

ā šŸ—ļø Technical Architecture

⁠Core Components
⁠Web Server
  • Nginx Version: 1.29.3 (custom compiled)
  • Base Image: Debian Bookworm Slim
  • Build Options: Full feature set including SSL, HTTP/2, and HTTP/3.
⁠Integrated Third-Party Modules
ModuleDescriptionVersion
ngx_http_geoip2_moduleGeoIP2 geolocationlatest
ngx_brotliGoogle Brotli compressionlatest
headers-more-nginx-moduleCustom HTTP headerslatest
ngx_cache_purgeCache purgelatest
njsJavaScript supportlatest
ModSecurity-nginxWAF securityv1.0.4
⁠Dependency Versions
  • OpenSSL: 3.5.4
  • PCRE2: 10.47
  • zlib: 1.3.1
  • libmaxminddb: 1.12.2

ā šŸ“ Project Structure

nginx1.29.3-docker/
ā”œā”€ā”€ Dockerfile                     # Container build configuration
ā”œā”€ā”€ docker-compose.yml             # Runtime compose (elf-nginx + haproxy)
ā”œā”€ā”€ docker-compose.build.yml       # Build-time compose (build & test images)
ā”œā”€ā”€ build-nginx.sh                 # Nginx build script
ā”œā”€ā”€ 30-keepalived-install.sh       # Keepalived install script for bare metal / VMs
ā”œā”€ā”€ keepalived-install.sh          # Lightweight Keepalived install script
ā”œā”€ā”€ docker-entrypoint.sh           # Nginx container entrypoint
ā”œā”€ā”€ nginx/                         # Nginx config & data mount root
│   ā”œā”€ā”€ etc/                       # Nginx configuration
│   ā”œā”€ā”€ modules/                   # Dynamic modules
│   ā”œā”€ā”€ logs/                      # Runtime logs
│   ā”œā”€ā”€ cache/                     # Cache files
│   ā”œā”€ā”€ geoip/                     # GeoIP databases
│   └── keepalived/                # Keepalived config (mount only)
ā”œā”€ā”€ haproxy/
│   └── haproxy.cfg                # HAProxy frontend config
ā”œā”€ā”€ README.md                      # Chinese documentation
ā”œā”€ā”€ README.en.md                   # English documentation (this file)
└── todos.md                       # Development task list

ā šŸ”§ Configuration Details

⁠Docker Compose Configuration (runtime example)
version: "3.9"

services:
  elf-nginx:
    container_name: elf-nginx
    build:
      context: .
      dockerfile: Dockerfile        # Build from the Dockerfile in this directory
    image: elf-nginx:latest
    restart: unless-stopped
    volumes:
      - ./nginx/etc:/etc/nginx                      # Persist configuration
      - ./nginx/modules:/usr/lib/nginx/modules      # Dynamic modules
      - ./nginx/logs:/var/log/nginx                 # Persist logs
      - ./nginx/cache:/var/cache/nginx              # Persist cache
      - ./nginx/geoip:/usr/share/GeoIP              # GeoIP databases
      - ./nginx/keepalived:/etc/keepalived          # Keepalived config (mount only)

  haproxy:
    container_name: haproxy
    image: haproxy:2.9
    restart: unless-stopped
    depends_on:
      - elf-nginx
    ports:
      - "80:80"    # HTTP traffic
      - "443:443"  # HTTPS traffic
    volumes:
      - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro

Note: The actual production runtime now uses docker-compose.yml, docker-compose.build.yml, and nginx-ui-compose.yml together. See the Deployment Guide section for the latest recommended flow.

⁠Nginx Configuration Layout
/etc/nginx/
ā”œā”€ā”€ nginx.conf                    # Main configuration
ā”œā”€ā”€ modules.conf                  # Dynamic module loading
ā”œā”€ā”€ conf.d/                       # Generic config snippets
│   ā”œā”€ā”€ ssl.conf                  # SSL/TLS config
│   ā”œā”€ā”€ cloudflare.conf           # Cloudflare integration
│   └── waf.conf                  # WAF rules
ā”œā”€ā”€ sites-available/              # Available site configs
│   └── default.conf              # Default site
ā”œā”€ā”€ sites-enabled/                # Enabled site configs
│   └── default.conf -> ../sites-available/default.conf
ā”œā”€ā”€ geoip/                        # Geolocation/IP settings
│   ā”œā”€ā”€ cloudflare_v4_realip.conf
│   ā”œā”€ā”€ cloudflare_v6_realip.conf
│   ā”œā”€ā”€ ip_whitelist.conf        # IP whitelist
│   └── ip_blacklist.conf        # IP blacklist
└── scripts/                      # Management scripts
    ā”œā”€ā”€ update_geoip.sh          # GeoIP update script
    └── manage_ip.sh             # IP management tool
⁠SSL/TLS Security Configuration
# SSL settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

ā šŸ›”ļø Security Features

⁠1. ModSecurity WAF
  • Integrated OWASP Core Rule Set (CRS).
  • Dynamic rule loading mechanism.
  • JSON-format audit logs.
  • Support for custom exception rules.
⁠2. GeoIP-based Filtering
  • Real-time geolocation detection.
  • Country/city-level resolution.
  • Scheduled database updates.
  • Cloudflare IP range integration.
⁠3. Access Control
  • IP whitelist – allow-only trusted IPs/subnets.
  • IP blacklist – block suspicious IPs/subnets.
  • Dynamic rule management – adjust at runtime.

ā šŸš€ High Availability Configuration

⁠Keepalived Setup
⁠Example Configuration
global_defs {
    enable_script_security
    script_user root
}

vrrp_script chk_nginx {
    script "/usr/local/sbin/check_nginx.sh"
    interval 2
    fall 3
    rise 2
}

vrrp_instance VI_51 {
    state MASTER                    # MASTER or BACKUP
    interface eth0                  # Network interface
    virtual_router_id 51            # VRRP group ID
    priority 200                    # Priority (MASTER: 200, BACKUP: 100)
    advert_int 1                    # Advertisement interval (seconds)

    # Unicast configuration
    unicast_src_ip 192.168.25.10    # Local IP
    unicast_peer {
        192.168.25.11               # Peer IP
    }

    authentication {
        auth_type PASS
        auth_pass 23887711          # VRRP authentication password
    }

    track_script {
        chk_nginx                   # Health check script
    }

    virtual_ipaddress {
        192.168.25.250/24 dev eth0  # Virtual IP
    }
}
⁠Health Check Mechanism
  • Process check – monitor the main Nginx process.
  • Service response – optional HTTP health checks.
  • Automatic failover – switch to backup on failure.

ā šŸ“Š Performance Optimization

⁠Compression
  • Gzip – standard HTTP compression.
  • Brotli – efficient compression from Google.
  • Static file optimization – support precompressed assets.
⁠Caching
  • Proxy cache – cache upstream responses.
  • Client cache – browser-side cache control.
  • FastCGI cache – cache dynamic content.
⁠HTTP/2 & HTTP/3
  • HTTP/2 multiplexing – faster page loads.
  • HTTP/3 QUIC – latest protocol support.

ā šŸ”„ Automated Operations

⁠Scheduled Tasks
⁠GeoIP Database Updates
  • Schedule: Every Wednesday and Saturday at 03:00.
  • Content: GeoLite2 Country/City/ASN databases.
  • Scope: Cloudflare IP ranges.
  • Automation: Auto restart/reload Nginx after updates.
⁠Systemd Timer Example
# systemd timer example
[Timer]
OnCalendar=Wed,Sat 03:00
Persistent=true
RandomizedDelaySec=5min
⁠Log Management
  • Access log – /var/log/nginx/access.log
  • Error log – /var/log/nginx/error.log
  • WAF audit log – /var/log/modsecurity/audit.log
  • Keepalived log – /var/log/keepalived/

ā šŸƒā€ā™‚ļø Deployment Guide

⁠Environment Requirements
  • Docker: 20.10+
  • Docker Compose: 2.0+
  • System Resources: Minimum 2 GB RAM, 4 GB disk.
  • Network: Able to expose ports 80/443.
⁠Quick Start (Local Development)
⁠1. Build Images
# From the project root (the folder that contains nginx1.29.3-docker/)
 docker builder prune -f    # Optional: clean up build cache

 cd nginx1.29.3-docker
 docker compose -f docker-compose.build.yml build
⁠2. Start Local Services (Testing)
# Start elf-nginx + haproxy using the build compose file
 docker compose -f docker-compose.build.yml up -d --build
⁠3. Push Images to Docker Hub
 docker login
 docker push tw199501/nginx:1.29.3
 docker push tw199501/haproxy:trixie

 # Optionally, use the provided helper scripts:
 # Bash (Git Bash / WSL):
 #   bash nginx1.29.3-docker/push-images.sh
 # Windows PowerShell (from project root):
 #   .\nginx1.29.3-docker\push-images.ps1
⁠Cross-Host Deployment (VM / Bare Metal)
⁠1. Prepare Directories on Target Host
sudo mkdir -p /opt/nginx-stack/nginx
sudo mkdir -p /opt/nginx-stack/nginx-ui
⁠2. Pull Images on Target Host
docker pull tw199501/nginx:1.29.3
docker pull tw199501/haproxy:trixie
⁠3. Start Services Using Compose

Copy the release-time docker-compose.yml and nginx-ui-compose.yml to the same directory on the target host.

docker compose -f docker-compose.yml up -d
docker compose -f nginx-ui-compose.yml up -d
⁠4. Manage Configuration via Nginx UI (Plan B)
Traffic path:  Client -> haproxy(80/443) -> elf-nginx:80
Config path:   /opt/nginx-stack/nginx      <-> elf-nginx:/etc/nginx
               /opt/nginx-stack/nginx      <-> nginx-ui:/etc/nginx
               /opt/nginx-stack/nginx-ui   <-> nginx-ui:/etc/nginx-ui
  • Access Nginx UI for the first time at:
    • http://<host>:8080 or
    • https://<host>:8443
⁠High Availability Deployment
⁠1. Primary/Backup Node Variables
# Primary node (MASTER)
export ROLE=MASTER
export IFACE=eth0
export VRID=51
export VIP_CIDR=192.168.25.250/24
export PEER_IP=192.168.25.11
export PRIORITY=200

# Backup node (BACKUP)
export ROLE=BACKUP
export IFACE=eth0
export VRID=51
export VIP_CIDR=192.168.25.250/24
export PEER_IP=192.168.25.10
export PRIORITY=100
⁠2. Run Keepalived Installer
# Run on both nodes
bash 30-keepalived-install.sh
⁠3. Validate High Availability
# Check virtual IP binding
ip -4 addr show dev eth0 | grep 192.168.25.250

# Inspect VRRP status
journalctl -u keepalived -e -n 50

ā šŸ”§ Management Commands

⁠IP Management Tool
# Add IP to whitelist
bash /etc/nginx/scripts/manage_ip.sh allow 192.168.1.100 /etc/nginx/geoip/ip_whitelist.conf

# Remove IP from whitelist
bash /etc/nginx/scripts/manage_ip.sh deny 192.168.1.100 /etc/nginx/geoip/ip_whitelist.conf
⁠Service Management
# Reload Nginx configuration
nginx -s reload

# Test Nginx configuration
nginx -t

# Restart Nginx service
systemctl restart nginx

# Check Nginx status
systemctl status nginx
⁠Monitoring & Maintenance
# List Nginx processes
ps aux | grep nginx

# Check port usage
netstat -tlnp | grep :80
netstat -tlnp | grep :443

# Tail logs
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

⁠🚨 Troubleshooting

⁠Common Issues
⁠1. Container Fails to Start
# Check container logs
docker-compose logs elf-nginx

# Check port conflicts
netstat -tlnp | grep :80
netstat -tlnp | grep :443
⁠2. Nginx Configuration Errors
# Test configuration syntax
docker exec elf-nginx nginx -t

# Inspect config file
docker exec elf-nginx cat /etc/nginx/nginx.conf
⁠3. Keepalived Failover Problems
# Check VRRP status
journalctl -u keepalived --no-pager

# Validate health check script
bash /usr/local/sbin/check_nginx.sh
⁠Log Analysis
⁠Key Log Paths
  • Nginx error log: /var/log/nginx/error.log
  • WAF audit log: /var/log/modsecurity/audit.log
  • Keepalived logs: journalctl -u keepalived

ā šŸ“ˆ Performance Tuning

⁠System Parameter Tuning
# File descriptor limits
echo "nginx soft nofile 65535" >> /etc/security/limits.conf
echo "nginx hard nofile 65535" >> /etc/security/limits.conf

# Network tuning
echo "net.core.somaxconn = 65535" >> /etc/sysctl.conf
sysctl -p
⁠Nginx Worker Tuning
worker_processes auto;
worker_connections 4096;
worker_rlimit_nofile 65535;

ā šŸ” Security Recommendations

⁠1. Regular Updates
  • Security patches – keep OS and packages updated.
  • SSL certificates – use Let's Encrypt with auto renewal.
  • WAF rules – update rule sets regularly.
⁠2. Access Control
  • Whitelisting – allow only trusted IP ranges.
  • Rate limiting – mitigate DDoS.
  • Strong SSL config – follow modern cipher suites.
⁠3. Monitoring & Alerting
  • Log monitoring – alert on abnormal access.
  • Performance monitoring – track latency and throughput.
  • Security monitoring – detect suspicious patterns.

ā šŸ“¦ Docker / Compose Cheat Sheet

The commands below assume you are in the nginx1.29.3-docker directory.

# Build images using the build compose file (build only)
docker compose -f docker-compose.build.yml build

# Build and start containers (dev use)
docker compose -f docker-compose.build.yml up -d --build

# Start services using the release docker-compose.yml (for consumers of the images)
docker compose up -d

# Stop and remove containers (keep images)
docker compose down

# Show current container status
docker compose ps

# Tail logs for nginx / haproxy
docker compose logs -f elf-nginx
docker compose logs -f haproxy

# Enter nginx container (debugging)
docker exec -it elf-nginx /bin/bash

# Manually trigger GeoIP update script
docker exec elf-nginx /etc/nginx/scripts/update_geoip.sh

ā šŸ“ž Support & Project Info

⁠Project Information
  • Version: 1.29.3
  • Last Updated: 2025-11-28
  • Maintainer: Elf team

This project aims to provide an enterprise-grade containerized Nginx solution. Issues and pull requests are welcome.

Tag summary

Content type

Image

Digest

sha256:8e8a67b37…

Size

41.3 MB

Last updated

11 months ago

docker pull tw199501/haproxy