# BBSEngine Handbook - Apache Reverse Proxy Configuration for Gunicorn
# For use with mod_proxy_http module
#
# This configuration proxies requests to a Gunicorn server running separately
# Benefits: Service isolation, easy restart, auto-recovery, scalability
#
# Installation:
# 1. Enable proxy modules: sudo a2enmod proxy proxy_http
# 2. Copy to /etc/apache2/sites-available/
# 3. Enable: sudo a2ensite handbook-gunicorn.conf
# 4. Start Gunicorn service: sudo systemctl start handbook-gunicorn
# 5. Restart Apache: sudo systemctl restart apache2

<VirtualHost *:80>
    ServerName handbook.bbsengine.org
    ServerAlias docs.bbsengine.org

    # Root for this virtual host
    DocumentRoot /home/opencode/data/work/bbsengine6/handbook

    # Enable proxy modules
    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On

        # Main reverse proxy for /handbook/
        ProxyPass /handbook/ http://127.0.0.1:8000/handbook/
        ProxyPassReverse /handbook/ http://127.0.0.1:8000/handbook/

        # Connection settings
        SetEnv proxy-nokeepalive 1
        SetEnv proxy-initial-not-pooled 1
        SetEnv proxy-sendchunked 1
    </IfModule>

    # Security headers
    <IfModule mod_headers.c>
        Header set X-Content-Type-Options "nosniff"
        Header set X-Frame-Options "SAMEORIGIN"
        Header set X-XSS-Protection "1; mode=block"
        Header set Referrer-Policy "strict-origin-when-cross-origin"
        Header set Content-Security-Policy "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'"
    </IfModule>

    # Caching
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 hour"
    </IfModule>

    # Compression
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/xml application/json text/javascript
    </IfModule>

    # Logging
    ErrorLog ${APACHE_LOG_DIR}/handbook-error.log
    CustomLog ${APACHE_LOG_DIR}/handbook-access.log combined

    # Connection timeout for slow Gunicorn
    ProxyTimeout 300
</VirtualHost>

# Optional: Redirect http to https
# <VirtualHost *:80>
#     ServerName handbook.bbsengine.org
#     Redirect permanent / https://handbook.bbsengine.org/
# </VirtualHost>

# Optional: HTTPS version
# <VirtualHost *:443>
#     ServerName handbook.bbsengine.org
#
#     SSLEngine on
#     SSLCertificateFile /path/to/cert.pem
#     SSLCertificateKeyFile /path/to/key.pem
#
#     # ... same proxy config as above ...
# </VirtualHost>
