Configure Nginx for WWebJS API
mainTo use Nginx as a reverse proxy, configure a location block that handles both standard HTTP requests and WebSocket upgrades.
Key Nginx Settings:
- Path Forwarding: Use
proxy_pass http://upstream_name/;(note the trailing slash) to strip the prefix. - Headers: Forward
Host,X-Real-IP,X-Forwarded-For,X-Forwarded-Proto, andX-Forwarded-Host. - WebSocket Support: Set
proxy_http_version 1.1and useUpgradeandConnectionheaders. - Timeouts: Set
proxy_connect_timeout,proxy_send_timeout, andproxy_read_timeout(e.g., to60s) to prevent premature connection closure during long operations.
upstream wwebjs_backend {
server wwebjs-api:3000;
}
server {
listen 80;
server_name api.yourdomain.com;
location /api/v1/whatsapp/ {
proxy_pass http://wwebjs_backend/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Timeouts for long-running operations
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}