Skip to main content

Overview

This guide covers production deployment best practices for Codex-LB, including:
  • Reverse proxy setup (Nginx, Caddy, Traefik)
  • SSL/TLS termination
  • Database selection and tuning
  • Security hardening
  • Monitoring and logging
  • Backup strategies

Architecture

A typical production setup:

Prerequisites

  • Linux server (Ubuntu 22.04+ recommended)
  • Docker and Docker Compose installed
  • Domain name with DNS configured
  • SSL certificate (Let’s Encrypt recommended)

Database Selection

SQLite (Default)

When to use:
  • Single-instance deployments
  • Low to medium concurrency (< 100 req/s)
  • Simple setup requirements
Pros:
  • Zero configuration
  • No separate database server
  • Built-in automatic backups before migrations
Cons:
  • Not suitable for multi-instance deployments
  • Limited concurrent write performance
Configuration:
When to use:
  • High concurrency requirements
  • Multi-instance deployments (horizontal scaling)
  • Managed database infrastructure
Pros:
  • Better concurrent write performance
  • Native replication and backup tools
  • Suitable for load balancing across multiple instances
Cons:
  • Requires separate database service
  • More complex setup
Configuration:

Reverse Proxy Setup

Nginx

Create /etc/nginx/sites-available/codex-lb:
Enable and reload:
OAuth Redirect Configuration: Update your environment variables to use the public OAuth domain:

Caddy

Create Caddyfile:
Start Caddy:
Caddy automatically obtains and renews SSL certificates from Let’s Encrypt.

Traefik

Create docker-compose.yml with Traefik:

Security Hardening

Environment Variables

Never commit secrets to version control. Use environment files with restricted permissions:
Critical settings:

Dashboard Authentication

Configure strong authentication in the dashboard:
  1. Navigate to Settings → Security
  2. Set a strong password (16+ characters)
  3. Enable TOTP (Time-based One-Time Password) 2FA
  4. Save recovery codes securely

API Key Authentication

Enable API key authentication to restrict proxy access:
  1. Navigate to Settings → API Key Auth
  2. Toggle Enable API Key Authentication
  3. Create API keys in API Keys section
  4. Set rate limits and model restrictions per key

Firewall Rules

IP Allowlist/Blocklist: Use the built-in firewall to restrict access by IP:
  1. Navigate to Settings → Firewall
  2. Add allowed IP ranges (CIDR notation)
  3. Block malicious IPs as needed
When behind a reverse proxy:
Only enable CODEX_LB_FIREWALL_TRUST_PROXY_HEADERS if your reverse proxy is properly configured to set X-Forwarded-For. Otherwise, clients can spoof IP addresses.

Container Security

The Docker image runs as a non-root user:
Ensure volume permissions match:

Database Configuration

PostgreSQL Setup

Create database and user:
Connection pooling:
Tuning: For PostgreSQL, optimize based on workload:

SQLite Tuning

For SQLite production deployments:

Backup Strategies

Automated Backups

SQLite

Using cron:
Add to crontab:

PostgreSQL

Using pg_dump:
Continuous archiving: For point-in-time recovery, enable PostgreSQL WAL archiving:

Disaster Recovery

Test restores regularly:

Monitoring and Logging

Health Checks

Monitoring endpoint:
Uptime monitoring: Use services like: Configure alerts for:
  • /health endpoint returning non-200
  • Response time > 5 seconds
  • Certificate expiration

Application Logs

View logs:
Centralized logging: Integrate with logging systems: Using Loki:
Using syslog:

Metrics

Prometheus monitoring (future): Codex-LB doesn’t expose Prometheus metrics yet, but you can monitor:
  • Container metrics (CPU, memory, network)
  • PostgreSQL metrics (using postgres_exporter)
  • Nginx/Caddy metrics

Performance Optimization

Connection Pooling

PostgreSQL:

Caching

Codex-LB caches:
  • Settings: Invalidated on change
  • Rate limit headers: Short TTL for API responses
  • Model list: Refreshed periodically from upstream

Horizontal Scaling

With PostgreSQL, you can run multiple Codex-LB instances behind a load balancer:
Load balancer config:

Troubleshooting

High Memory Usage

Check container stats:
Limit memory:

Slow Database Queries

Enable query logging:
PostgreSQL slow query log:

SSL Certificate Issues

Check certificate expiration:
Renew Let’s Encrypt:

Maintenance

Updating Codex-LB

Database Maintenance

PostgreSQL VACUUM:
SQLite integrity check:

Next Steps