added examples file
Node-js open wen socket refresh issues in client apps can be caused by Nginx proxy mis-configs. See optimization examples here.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
# /etc/nginx/sites-available/myapp
|
||||
# Required for WebSocket upgrade handling
|
||||
map $http_upgrade $connection_upgrade {
|
||||
default upgrade;
|
||||
'' close;
|
||||
}
|
||||
upstream node_backend {
|
||||
server 127.0.0.1:3000;
|
||||
# Keep connections to Node alive
|
||||
keepalive 32;
|
||||
}
|
||||
server {
|
||||
listen 443 ssl;
|
||||
listen [::]:443 ssl;
|
||||
server_name example.com;
|
||||
# Your existing SSL configuration
|
||||
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
|
||||
# -------------------------------------------------------
|
||||
# Dynamic Node.js application
|
||||
# -------------------------------------------------------
|
||||
location / {
|
||||
proxy_pass http://node_backend;
|
||||
# HTTP/1.1 is important for keep-alive/WebSocket/SSE
|
||||
proxy_http_version 1.1;
|
||||
# Preserve original request information
|
||||
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 support
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
# ---------------------------------------------------
|
||||
# IMPORTANT FOR DYNAMIC/STREAMING APPLICATIONS
|
||||
# ---------------------------------------------------
|
||||
# Don't allow Nginx to hold dynamic responses in buffers
|
||||
proxy_buffering off;
|
||||
# Don't cache application responses
|
||||
proxy_cache off;
|
||||
# Don't buffer requests either
|
||||
proxy_request_buffering off;
|
||||
# Long-running Node connections
|
||||
proxy_connect_timeout 10s;
|
||||
proxy_send_timeout 3600s;
|
||||
proxy_read_timeout 3600s;
|
||||
# Keep connection alive
|
||||
proxy_socket_keepalive on;
|
||||
# Don't rewrite redirects unexpectedly
|
||||
proxy_redirect off;
|
||||
}
|
||||
# -------------------------------------------------------
|
||||
# Optional explicit WebSocket endpoint
|
||||
# Change /socket.io/ to whatever your application uses
|
||||
# -------------------------------------------------------
|
||||
location /socket.io/ {
|
||||
proxy_pass http://node_backend;
|
||||
proxy_http_version 1.1;
|
||||
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 Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
proxy_buffering off;
|
||||
proxy_cache off;
|
||||
proxy_connect_timeout 10s;
|
||||
proxy_send_timeout 3600s;
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_socket_keepalive on;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
==================================
|
||||
I would also explicitly prevent browser caching
|
||||
For dynamic API responses, I’d consider:I would also explicitly prevent browser caching
|
||||
For dynamic API responses, I’d consider:
|
||||
==================================
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://node_backend;
|
||||
proxy_http_version 1.1;
|
||||
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_buffering off;
|
||||
proxy_cache off;
|
||||
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always;
|
||||
add_header Pragma "no-cache" always;
|
||||
add_header Expires "0" always;
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_send_timeout 3600s;
|
||||
}
|
||||
|
||||
|
||||
===========================
|
||||
Check for :
|
||||
===========================
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_buffering off;
|
||||
proxy_cache off;
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_send_timeout 3600s;
|
||||
proxy_socket_keepalive on;
|
||||
|
||||
=======================
|
||||
Reference in New Issue
Block a user