|
| 1 | +# Reverse Proxy / Kong Setup Guide |
| 2 | + |
| 3 | +This document provides configuration examples for deploying WWebJS API behind reverse proxies like Kong, Nginx, or other load balancers. |
| 4 | + |
| 5 | +## Environment Variables |
| 6 | + |
| 7 | +The following environment variables have been added to support reverse proxy deployments: |
| 8 | + |
| 9 | +```bash |
| 10 | +# Base path for mounting all routes (optional) |
| 11 | +BASE_PATH=/api/v1/whatsapp |
| 12 | + |
| 13 | +# Enable trust proxy for proper IP forwarding (required for reverse proxy) |
| 14 | +TRUST_PROXY=true |
| 15 | +``` |
| 16 | + |
| 17 | +## Kong Configuration |
| 18 | + |
| 19 | +### Basic Kong Route Setup |
| 20 | + |
| 21 | +```yaml |
| 22 | +# Kong route configuration |
| 23 | +routes: |
| 24 | + - name: wwebjs-api |
| 25 | + paths: ["/api/v1/whatsapp"] |
| 26 | + strip_path: true # Important: removes the prefix before forwarding |
| 27 | + preserve_host: false |
| 28 | + protocols: ["http", "https"] |
| 29 | + service: wwebjs-service |
| 30 | + |
| 31 | +services: |
| 32 | + - name: wwebjs-service |
| 33 | + url: http://wwebjs-api:3000 |
| 34 | + connect_timeout: 60000 |
| 35 | + write_timeout: 60000 |
| 36 | + read_timeout: 60000 |
| 37 | +``` |
| 38 | +
|
| 39 | +### Kong with WebSocket Support |
| 40 | +
|
| 41 | +```yaml |
| 42 | +# Kong route for WebSocket connections |
| 43 | +routes: |
| 44 | + - name: wwebjs-websocket |
| 45 | + paths: ["/api/v1/whatsapp/ws"] |
| 46 | + strip_path: true |
| 47 | + protocols: ["ws", "wss"] |
| 48 | + service: wwebjs-websocket-service |
| 49 | + |
| 50 | +services: |
| 51 | + - name: wwebjs-websocket-service |
| 52 | + url: http://wwebjs-api:3000 |
| 53 | +``` |
| 54 | +
|
| 55 | +## Nginx Configuration |
| 56 | +
|
| 57 | +```nginx |
| 58 | +upstream wwebjs_backend { |
| 59 | + server wwebjs-api:3000; |
| 60 | +} |
| 61 | + |
| 62 | +server { |
| 63 | + listen 80; |
| 64 | + server_name api.yourdomain.com; |
| 65 | + |
| 66 | + location /api/v1/whatsapp/ { |
| 67 | + proxy_pass http://wwebjs_backend/; |
| 68 | + proxy_set_header Host $host; |
| 69 | + proxy_set_header X-Real-IP $remote_addr; |
| 70 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 71 | + proxy_set_header X-Forwarded-Proto $scheme; |
| 72 | + proxy_set_header X-Forwarded-Host $host; |
| 73 | + |
| 74 | + # WebSocket support |
| 75 | + proxy_http_version 1.1; |
| 76 | + proxy_set_header Upgrade $http_upgrade; |
| 77 | + proxy_set_header Connection "upgrade"; |
| 78 | + |
| 79 | + # Timeouts for long-running operations |
| 80 | + proxy_connect_timeout 60s; |
| 81 | + proxy_send_timeout 60s; |
| 82 | + proxy_read_timeout 60s; |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Docker Compose with Reverse Proxy |
| 88 | + |
| 89 | +```yaml |
| 90 | +version: '3.8' |
| 91 | + |
| 92 | +services: |
| 93 | + wwebjs-api: |
| 94 | + image: avoylenko/wwebjs-api:latest |
| 95 | + container_name: wwebjs-api |
| 96 | + restart: always |
| 97 | + environment: |
| 98 | + # Reverse proxy configuration |
| 99 | + - BASE_PATH=/api/v1/whatsapp |
| 100 | + - TRUST_PROXY=true |
| 101 | + |
| 102 | + # Other configurations |
| 103 | + - BASE_WEBHOOK_URL=https://api.yourdomain.com/api/v1/whatsapp/localCallbackExample |
| 104 | + - API_KEY=your_secure_api_key |
| 105 | + - ENABLE_LOCAL_CALLBACK_EXAMPLE=false |
| 106 | + - ENABLE_SWAGGER_ENDPOINT=true |
| 107 | + volumes: |
| 108 | + - ./sessions:/usr/src/app/sessions |
| 109 | + networks: |
| 110 | + - api-network |
| 111 | + |
| 112 | + nginx: |
| 113 | + image: nginx:alpine |
| 114 | + container_name: nginx-proxy |
| 115 | + ports: |
| 116 | + - "80:80" |
| 117 | + - "443:443" |
| 118 | + volumes: |
| 119 | + - ./nginx.conf:/etc/nginx/nginx.conf |
| 120 | + depends_on: |
| 121 | + - wwebjs-api |
| 122 | + networks: |
| 123 | + - api-network |
| 124 | + |
| 125 | +networks: |
| 126 | + api-network: |
| 127 | + driver: bridge |
| 128 | +``` |
| 129 | +
|
| 130 | +## API Endpoint Examples |
| 131 | +
|
| 132 | +With `BASE_PATH=/api/v1/whatsapp` configured: |
| 133 | + |
| 134 | +### Original endpoints: |
| 135 | +- `GET /session/start/ABCD` |
| 136 | +- `GET /client/getContacts/ABCD` |
| 137 | +- `WebSocket: ws://localhost:3000/ws/ABCD` |
| 138 | + |
| 139 | +### Behind reverse proxy: |
| 140 | +- `GET https://api.yourdomain.com/api/v1/whatsapp/session/start/ABCD` |
| 141 | +- `GET https://api.yourdomain.com/api/v1/whatsapp/client/getContacts/ABCD` |
| 142 | +- `WebSocket: wss://api.yourdomain.com/api/v1/whatsapp/ws/ABCD` |
| 143 | + |
| 144 | +## Important Notes |
| 145 | + |
| 146 | +1. **Strip Path**: Always configure your reverse proxy to strip the base path before forwarding to the application |
| 147 | +2. **Trust Proxy**: Set `TRUST_PROXY=true` to ensure proper IP detection for rate limiting |
| 148 | +3. **WebSocket Headers**: Ensure `X-Forwarded-Host` header is properly forwarded for WebSocket connections |
| 149 | +4. **Timeouts**: Configure appropriate timeouts for WhatsApp operations which can take time |
| 150 | +5. **HTTPS**: Use HTTPS in production and update `BASE_WEBHOOK_URL` accordingly |
| 151 | + |
| 152 | +## Troubleshooting |
| 153 | + |
| 154 | +### Common Issues: |
| 155 | + |
| 156 | +1. **404 Errors**: Check if `strip_path` is enabled in your reverse proxy |
| 157 | +2. **WebSocket Connection Failed**: Ensure WebSocket upgrade headers are properly forwarded |
| 158 | +3. **Rate Limiting Issues**: Verify `TRUST_PROXY=true` is set and `X-Forwarded-For` header is forwarded |
| 159 | +4. **Webhook Callbacks**: Update `BASE_WEBHOOK_URL` to use the external domain with base path |
| 160 | + |
| 161 | +### Testing: |
| 162 | + |
| 163 | +```bash |
| 164 | +# Test API endpoint |
| 165 | +curl https://api.yourdomain.com/api/v1/whatsapp/ping |
| 166 | +
|
| 167 | +# Test WebSocket connection |
| 168 | +wscat -c wss://api.yourdomain.com/api/v1/whatsapp/ws/test |
| 169 | +``` |
0 commit comments