• Nginx配置文件中,如何配置启用SignalR


    以下内容包含为 SignalR 启用 WebSocket、ServerSentEvents 和 LongPolling 所需的最低设置:

    核心代码:

    1. http {
    2. map $http_connection $connection_upgrade {
    3. "~*Upgrade" $http_connection;
    4. default keep-alive;
    5. }
    6. server {
    7. listen 80;
    8. server_name xx.xxx.com;
    9. location / {
    10. proxy_pass http://backend;
    11. proxy_set_header Host $host;
    12. root html;
    13. index index.html index.htm;
    14. }
    15. # SignalR 单独配置
    16. location /chatHub {
    17. proxy_pass http://backend;
    18. proxy_set_header Upgrade $http_upgrade;
    19. proxy_set_header Connection $connection_upgrade;
    20. proxy_http_version 1.1;
    21. }
    22. }
    23. }

    使用多个后端服务器时,必须添加粘滞会话(sticky sessions),以防止 SignalR 连接在连接时切换服务器。 可通过多种方法在 Nginx 中添加粘滞会话(sticky sessions)。 下面是其中一种

    除了前面的配置外,还添加了以下内容。 在下面的示例中,backend 是服务器组的名称。

    对于 Nginx 开放源代码,使用 ip_hash 基于客户端的 IP 地址将连接路由到服务器:(必须是ip_hash )

    1. upstream backend {
    2. server 192.168.0.245:8000;
    3. server 192.168.0.245:8001;
    4. server 192.168.0.245:8002;
    5. ip_hash; # 根据客户端IP地址Hash值将请求分配给固定的一个服务器处理
    6. }

     全部代码:

    1. worker_processes 1;
    2. events {
    3. worker_connections 1024;
    4. }
    5. http {
    6. include mime.types;
    7. default_type application/octet-stream;
    8. map $http_connection $connection_upgrade {
    9. "~*Upgrade" $http_connection;
    10. default keep-alive;
    11. }
    12. sendfile on;
    13. keepalive_timeout 65;
    14. server {
    15. listen 80 default_server;
    16. server_name _;
    17. return 444 ; #过滤其他域名的请求,返回444状态码
    18. }
    19. upstream backend {
    20. server 192.168.0.245:8000;
    21. server 192.168.0.245:8001;
    22. server 192.168.0.245:8002;
    23. ip_hash; # 根据客户端IP地址Hash值将请求分配给固定的一个服务器处理
    24. }
    25. server {
    26. listen 80;
    27. server_name xx.xxx.com;
    28. location / {
    29. proxy_pass http://backend;
    30. proxy_set_header Host $host;
    31. root html;
    32. index index.html index.htm;
    33. }
    34. # SignalR 单独配置
    35. location /chatHub {
    36. proxy_pass http://backend;
    37. proxy_set_header Upgrade $http_upgrade;
    38. proxy_set_header Connection $connection_upgrade;
    39. proxy_http_version 1.1;
    40. }
    41. error_page 500 502 503 504 /50x.html;
    42. location = /50x.html {
    43. root html;
    44. }
    45. }
    46. }
  • 相关阅读:
    java计算机毕业设计农田节水灌溉监测系统源码+系统+数据库+lw文档+mybatis+运行部署
    C++基础语法
    借助Spire.Doc for Java控件,将 ODT 转换为 PDF。
    Linux的文件权限管理
    【电路笔记】-欧姆定律
    MSQL系列(四) Mysql实战-索引分析Explain命令详解
    如何在 pyqt 中实现桌面歌词
    file-storage-sdk项目开发中的踩坑记录
    Kubernetes——裸机搭建集群环境
    以矩阵的形式,对点或线段或多边形绕固定点旋转方法
  • 原文地址:https://blog.csdn.net/mss359681091/article/details/134079367