Configure Lighttpd 1.4 load balancing with multiple backend servers for high availability

Intermediate 25 min May 22, 2026 31 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up Lighttpd with mod_proxy to distribute traffic across multiple backend servers, implement health checks for automatic failover, and configure SSL termination for a robust high availability web infrastructure.

Prerequisites

  • Multiple backend servers running web applications
  • Root or sudo access
  • Basic understanding of HTTP and load balancing concepts

What this solves

Lighttpd load balancing distributes incoming web traffic across multiple backend servers to prevent single points of failure and handle increased traffic loads. This configuration ensures your web application stays available even if individual backend servers fail, while improving response times through intelligent traffic distribution.

Step-by-step configuration

Install Lighttpd and required modules

Install Lighttpd with the proxy module needed for load balancing functionality.

sudo apt update
sudo apt install -y lighttpd lighttpd-mod-deflate
sudo dnf update -y
sudo dnf install -y lighttpd lighttpd-mod-deflate

Enable proxy module

Enable the mod_proxy module which handles load balancing and backend server communication.

sudo lighty-enable-mod proxy

Configure main Lighttpd settings

Set up the primary configuration with optimized settings for load balancing workloads.

server.modules = (
    "mod_indexfile",
    "mod_access",
    "mod_alias",
    "mod_redirect",
    "mod_proxy",
    "mod_compress",
    "mod_accesslog"
)

server.document-root = "/var/www/html"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
server.port = 80

Performance optimizations for load balancing

server.max-connections = 2048 server.max-fds = 2048 server.event-handler = "linux-sysepoll" server.network-backend = "linux-sendfile"

Index files

index-file.names = ( "index.php", "index.html", "index.htm" )

URL rewriting for compatibility

url.access-deny = ( "~", ".inc" )

MIME type configuration

include "/etc/lighttpd/conf-available/mime.conf"

Set up backend server definitions

Configure multiple backend servers that will handle the actual application requests.

# Backend server pool configuration
proxy.balance = "round-robin"
proxy.server = (
    "" => (
        "backend1" => (
            "host" => "203.0.113.10",
            "port" => 8080,
            "check-local" => "disable"
        ),
        "backend2" => (
            "host" => "203.0.113.11", 
            "port" => 8080,
            "check-local" => "disable"
        ),
        "backend3" => (
            "host" => "203.0.113.12",
            "port" => 8080,
            "check-local" => "disable"
        )
    )
)

Health check configuration

proxy.header = ( "upgrade" => "enable", "connect" => "enable" )

Connection timeout settings

proxy.server-timeout = 30 proxy.connect-timeout = 10

Load balancing method options:

"round-robin" - distributes requests evenly

"least-connection" - sends to server with fewest active connections

"hash" - uses client IP for consistent server assignment

"fair" - weighted distribution based on response times

Enable the proxy configuration

Activate the proxy configuration to make load balancing active.

sudo ln -sf /etc/lighttpd/conf-available/10-proxy.conf /etc/lighttpd/conf-enabled/
sudo lighttpd -t -f /etc/lighttpd/lighttpd.conf

Configure SSL termination (optional but recommended)

Set up SSL/TLS termination at the load balancer for secure connections.

sudo mkdir -p /etc/lighttpd/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout /etc/lighttpd/ssl/server.key \
    -out /etc/lighttpd/ssl/server.crt \
    -subj "/C=US/ST=State/L=City/O=Organization/CN=example.com"
# SSL configuration
$HTTP["scheme"] == "http" {
    url.redirect = (".*" => "https://%0$0")
}

$SERVER["socket"] == ":443" {
    ssl.engine = "enable"
    ssl.pemfile = "/etc/lighttpd/ssl/server.crt"
    ssl.privkey = "/etc/lighttpd/ssl/server.key"
    ssl.cipher-list = "ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20:!aNULL:!SHA1:!AESCCM"
    ssl.honor-cipher-order = "enable"
    ssl.disable-client-renegotiation = "enable"
    ssl.use-sslv2 = "disable"
    ssl.use-sslv3 = "disable"
}
sudo ln -sf /etc/lighttpd/conf-available/10-ssl.conf /etc/lighttpd/conf-enabled/

Configure health checking and failover

Set up advanced health monitoring to automatically remove failed backends from the pool.

# Advanced health checking
proxy.balance = "round-robin"
proxy.server = (
    "" => (
        "backend1" => (
            "host" => "203.0.113.10",
            "port" => 8080,
            "check-local" => "disable",
            "disable-time" => 60,
            "allow-x-sendfile" => "enable"
        ),
        "backend2" => (
            "host" => "203.0.113.11", 
            "port" => 8080,
            "check-local" => "disable",
            "disable-time" => 60,
            "allow-x-sendfile" => "enable"
        ),
        "backend3" => (
            "host" => "203.0.113.12",
            "port" => 8080,
            "check-local" => "disable",
            "disable-time" => 60,
            "allow-x-sendfile" => "enable"
        )
    )
)

Health check endpoint (optional)

$HTTP["url"] =~ "^/health/?$" { proxy.server = () setenv.add-response-header = ( "Content-Type" => "application/json" ) server.document-root = "/var/www/health" }

Create health check endpoint

Set up a simple health check endpoint for monitoring system status.

sudo mkdir -p /var/www/health
{"status":"healthy","timestamp":"$(date -Iseconds)","load_balancer":"lighttpd"}
sudo chown -R www-data:www-data /var/www/health
sudo chmod -R 644 /var/www/health/*

Configure logging for monitoring

Set up comprehensive logging to track backend server performance and failures.

# Enhanced logging for load balancer monitoring
accesslog.filename = "/var/log/lighttpd/access.log"
accesslog.format = "%h %V %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D %{X-Forwarded-For}i"

Separate log for backend errors

$HTTP["status"] =~ "^[45]" { accesslog.filename = "/var/log/lighttpd/backend-errors.log" }

Debug logging (enable only for troubleshooting)

debug.log-request-header = "enable"

debug.log-response-header = "enable"

sudo ln -sf /etc/lighttpd/conf-available/20-logging.conf /etc/lighttpd/conf-enabled/

Set up log rotation

Configure log rotation to prevent disk space issues from large access logs.

/var/log/lighttpd/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 640 www-data adm
    postrotate
        if [ -f /run/lighttpd.pid ]; then
            /bin/kill -USR1 $(cat /run/lighttpd.pid)
        fi
    endscript
}

Start and enable Lighttpd

Start the load balancer and enable it to start automatically on boot.

sudo systemctl enable lighttpd
sudo systemctl start lighttpd
sudo systemctl status lighttpd

Verify your setup

Test the load balancer configuration and confirm traffic distribution.

# Check Lighttpd status
sudo systemctl status lighttpd

Test configuration syntax

sudo lighttpd -t -f /etc/lighttpd/lighttpd.conf

Check if load balancer is responding

curl -I http://localhost/ curl -I https://localhost/

Test health check endpoint

curl http://localhost/health

Monitor backend distribution in logs

sudo tail -f /var/log/lighttpd/access.log

Check for backend connection errors

sudo tail -f /var/log/lighttpd/error.log

Test specific backend responses

for i in {1..6}; do curl -s http://localhost/ | grep -o "Server: .*" || echo "Request $i"; done
Note: You can monitor which backend server handled each request by checking the access logs or implementing custom headers that identify the backend server.

Advanced load balancing configuration

Configure weighted load balancing

Assign different weights to backends based on their capacity or performance.

# Weighted round-robin load balancing
proxy.balance = "round-robin"
proxy.server = (
    "" => (
        "backend1" => (
            "host" => "203.0.113.10",
            "port" => 8080,
            "check-local" => "disable",
            "disable-time" => 60,
            "weight" => 3  # This server gets 3x more requests
        ),
        "backend2" => (
            "host" => "203.0.113.11", 
            "port" => 8080,
            "check-local" => "disable",
            "disable-time" => 60,
            "weight" => 2  # This server gets 2x more requests
        ),
        "backend3" => (
            "host" => "203.0.113.12",
            "port" => 8080,
            "check-local" => "disable",
            "disable-time" => 60,
            "weight" => 1  # Base weight
        )
    )
)

Set up session affinity (sticky sessions)

Configure session persistence to ensure user sessions stay on the same backend server.

# Session affinity based on client IP
proxy.balance = "hash"
proxy.server = (
    "" => (
        "backend1" => (
            "host" => "203.0.113.10",
            "port" => 8080,
            "check-local" => "disable"
        ),
        "backend2" => (
            "host" => "203.0.113.11", 
            "port" => 8080,
            "check-local" => "disable"
        ),
        "backend3" => (
            "host" => "203.0.113.12",
            "port" => 8080,
            "check-local" => "disable"
        )
    )
)

Alternative: Cookie-based session affinity

Requires application support for session cookies

Common issues

Symptom Cause Fix
502 Bad Gateway errors Backend servers unreachable Check backend server status and network connectivity with telnet 203.0.113.10 8080
All traffic goes to one backend Load balancing not configured properly Verify proxy.balance = "round-robin" and restart lighttpd
SSL certificate errors Certificate path or permissions wrong Check certificate files exist and are readable: sudo ls -la /etc/lighttpd/ssl/
Configuration test fails Syntax errors in config files Run sudo lighttpd -t -f /etc/lighttpd/lighttpd.conf for detailed error messages
High memory usage Too many concurrent connections Adjust server.max-connections and monitor with sudo netstat -an | grep :80 | wc -l
Backend health checks failing Backend servers responding slowly Increase proxy.server-timeout and disable-time values

Performance optimization tips

For high-traffic scenarios, consider these additional optimizations that complement the NGINX performance optimization techniques:

  • Connection pooling: Enable keep-alive connections to backends to reduce connection overhead
  • Caching: Implement response caching for static content using mod_cache
  • Compression: Enable gzip compression to reduce bandwidth usage
  • Monitoring: Set up comprehensive monitoring similar to Prometheus and Grafana monitoring approaches
  • Resource limits: Tune file descriptor limits and connection pools based on traffic patterns

Next steps

Running this in production?

Want this handled for you? Setting this up once is straightforward. Keeping it patched, monitored, backed up and performant across environments is the harder part. See how we run infrastructure like this for European teams.

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle managed cloud infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.