Nginx配置文件中,如何配置啟用SignalR
以下內(nèi)容包含為 SignalR 啟用 WebSocket、ServerSentEvents 和 LongPolling 所需的最低設(shè)置:
http { map $http_connection $connection_upgrade { "~*Upgrade" $http_connection; default keep-alive; } server { listen 80; server_name example.com *.example.com; # Configure the SignalR Endpoint location /hubroute { # App server url proxy_pass http://localhost:5000; # Configuration for WebSockets proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_cache off; # WebSockets were implemented after http/1.0 proxy_http_version 1.1; # Configuration for ServerSentEvents proxy_buffering off; # Configuration for LongPolling or if your KeepAliveInterval is longer than 60 seconds proxy_read_timeout 100s; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } }
使用多個后端服務(wù)器時,必須添加粘滯會話(sticky sessions),以防止 SignalR 連接在連接時切換服務(wù)器。 可通過多種方法在 Nginx 中添加粘滯會話(sticky sessions)。 下面是其中一種
除了前面的配置外,還添加了以下內(nèi)容。 在下面的示例中,backend 是服務(wù)器組的名稱。
對于 Nginx 開放源代碼,使用 ip_hash 基于客戶端的 IP 地址將連接路由到服務(wù)器:(必須是ip_hash )
http { upstream backend { # App server 1 server localhost:5000; # App server 2 server localhost:5002; ip_hash; } }
最后,將 server 部分中的 proxy_pass http://localhost:5000 更改為 proxy_pass http://backend

浙公網(wǎng)安備 33010602011771號