Skip to content

feat: add reverse proxy support with configurable base path and trust proxy settings#47

Merged
avoylenko merged 1 commit intoavoylenko:mainfrom
sonarta:main
Sep 2, 2025
Merged

feat: add reverse proxy support with configurable base path and trust proxy settings#47
avoylenko merged 1 commit intoavoylenko:mainfrom
sonarta:main

Conversation

@sonarta
Copy link
Copy Markdown
Contributor

@sonarta sonarta commented Sep 1, 2025

Description

This PR adds comprehensive reverse proxy support to enable deployment behind Kong, Nginx, or other load balancers with configurable base paths.

Changes Made

  • Added new environment variables:

    • BASE_PATH: Configurable base path for mounting routes (e.g., /api/v1/whatsapp)
    • TRUST_PROXY: Enable trust proxy for proper IP forwarding behind reverse proxies
  • Enhanced Express app configuration:

    • Routes now mount with configurable base path instead of hardcoded root (/)
    • Trust proxy setting for accurate client IP detection
  • Improved WebSocket handling:

    • WebSocket upgrade handler now supports base path routing
    • Uses X-Forwarded-Host header for proper host detection behind proxies
  • Fixed rate limiting:

    • Rate limiter now uses real client IP instead of proxy IP
    • Proper IP extraction when behind reverse proxy
  • Updated configuration:

    • Added new environment variables to .env.example
    • Comprehensive documentation in REVERSE_PROXY_SETUP.md

Testing

  • Tested with Kong API Gateway configuration
  • Verified WebSocket connections work with base path
  • Confirmed rate limiting uses correct client IPs
  • All existing functionality remains unchanged when new variables are not set

Documentation

  • Created REVERSE_PROXY_SETUP.md with Kong, Nginx, and Docker Compose examples
  • Updated .env.example with new configuration options
  • Includes troubleshooting guide for common reverse proxy issues

Backward Compatibility

✅ Fully backward compatible - existing deployments continue to work without any changes

Related Issues

Fixes issues with:

  • 404 errors when deployed behind reverse proxy with path prefix
  • WebSocket connection failures in proxy environments
  • Incorrect rate limiting when all requests appear from proxy IP

Comment thread src/middleware.js
@avoylenko avoylenko merged commit 62860a6 into avoylenko:main Sep 2, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants