# Nginx Configuration for Flask-SocketIO with WebSocket Support This document provides the required Nginx configuration to properly proxy WebSocket connections for the Markov Economics application. ## Required Nginx Configuration ```nginx server { listen 80; server_name markov.elpatron.me; # Optional: Redirect HTTP to HTTPS return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name markov.elpatron.me; # SSL configuration (adjust paths as needed) ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/private.key; # WebSocket proxy settings location /socket.io/ { proxy_pass http://127.0.0.1:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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; # WebSocket specific timeouts proxy_read_timeout 86400; proxy_send_timeout 86400; proxy_connect_timeout 60; # Disable buffering for real-time communication proxy_buffering off; proxy_cache off; } # Regular HTTP traffic location / { proxy_pass http://127.0.0.1:5000; 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; # Standard timeouts proxy_read_timeout 30; proxy_send_timeout 30; proxy_connect_timeout 10; } } ``` ## Critical Configuration Points 1. **WebSocket Upgrade Headers**: The `Upgrade` and `Connection` headers are essential for WebSocket handshake 2. **HTTP Version**: Must use HTTP/1.1 for WebSocket support 3. **Extended Timeouts**: WebSocket connections need longer timeouts than regular HTTP 4. **Disable Buffering**: Real-time communication requires immediate data flow 5. **Separate Location Blocks**: Socket.IO traffic should be handled separately from regular HTTP traffic ## Testing the Configuration After applying the configuration: 1. Restart Nginx: `sudo nginx -t && sudo systemctl reload nginx` 2. Check browser console for connection messages 3. Test the debug endpoint: https://markov.elpatron.me/debug 4. Run the distribution test: Open browser console and type `testDistributionChart()` ## Troubleshooting If WebSocket connections still fail: - Check Nginx error logs: `sudo tail -f /var/log/nginx/error.log` - Verify the application is accessible directly: `curl http://127.0.0.1:5000/health` - Test WebSocket connection manually with tools like `wscat` - Ensure firewall allows WebSocket traffic ## Alternative: Fallback to Polling Only If WebSocket cannot be made to work, the application will automatically fall back to HTTP polling, which should work with any standard HTTP proxy configuration.