.NET CORE使用SignalR、Nginx、CORS跨域问题
Nginx命令
# 检查配置语法 nginx -t # 重新加载配置 nginx -s reload # 监控错误日志 tail -f /var/log/nginx/error.log
首先使用SingalR,需要在Nginx增加相关配置。修改后检查语法(nginx -t),并重新加载配置(nginx -s reload)
# WebSocket连接升级映射 map $http_upgrade $connection_upgrade { default upgrade; '' close; }
# 修改后的WebSocket代理配置 location /api/chatHub { proxy_pass http://10.202.12.77; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $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; proxy_read_timeout 3600s; proxy_send_timeout 3600s; proxy_connect_timeout 30s; proxy_buffering off; proxy_cache off; } # SignalR协商请求专用配置 location /api/chatHub/negotiate { proxy_pass http://10.202.12.77; 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_read_timeout 60s; proxy_connect_timeout 30s; proxy_send_timeout 60s; }
完整的nginx.conf示例
worker_processes 1; events { worker_connections 1024; } stream { server { listen 3306; proxy_pass 10.202.33.30:3306; } server { listen 5673; proxy_pass 10.202.8.104:5672; } } http { # 根据Upgrade头动态设置Connection头,这是WebSocket代理的关键 map $http_upgrade $connection_upgrade { default upgrade; '' close; } include mime.types; default_type application/octet-stream; sendfile on; client_header_timeout 600s; # 客户端请求头超时设置为 10 分钟 client_body_timeout 600s; # 客户端请求体超时设置为 10 分钟 send_timeout 600s; # 发送响应超时设置为 10 分钟 keepalive_timeout 600s; # 保持连接超时设置为 10 分钟 client_max_body_size 100M; server { listen 80; server_name 10.202.12.140; # WebSocket 代理配置 location /api/chatHub { proxy_pass http://10.202.12.77; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; # 使用变量。作用:根据map规则智能设置Connection头 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_read_timeout 3600s; proxy_send_timeout 3600s; # 确保禁用缓冲。防止Nginx缓冲导致SignalR消息延迟或中断 proxy_buffering off; proxy_cache off; # 连接超时设置。确保WebSocket连接建立不会超时 proxy_connect_timeout 30s; } # 针对SignalR协商请求的配置。SignalR首先会发送HTTP协商请求,需要正确处理 location /api/chatHub/negotiate { proxy_pass http://10.202.12.77; 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_read_timeout 60s; proxy_connect_timeout 30s; proxy_send_timeout 60s; } location / { proxy_pass http://10.202.12.77; 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_read_timeout 600s; # 等待后端响应超时设置为 10 分钟 proxy_connect_timeout 600s; # 连接后端超时设置为 10 分钟 proxy_send_timeout 600s; # 向后端发送请求超时设置为 10 分钟 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8080; server_name 10.202.12.140; location / { proxy_pass http://10.202.12.77:8888; 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_read_timeout 600s; # 等待后端响应超时设置为 10 分钟 proxy_connect_timeout 600s; # 连接后端超时设置为 10 分钟 proxy_send_timeout 600s; # 向后端发送请求超时设置为 10 分钟 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8081; server_name lms.envision-aesc.cn; location / { proxy_pass http://10.202.12.142:3000; 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_read_timeout 600s; # 等待后端响应超时设置为 10 分钟 proxy_connect_timeout 600s; # 连接后端超时设置为 10 分钟 proxy_send_timeout 600s; # 向后端发送请求超时设置为 10 分钟 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8083; server_name lms.envision-aesc.cn; location / { proxy_pass http://10.202.12.144:3000; 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_read_timeout 600s; # 等待后端响应超时设置为 10 分钟 proxy_connect_timeout 600s; # 连接后端超时设置为 10 分钟 proxy_send_timeout 600s; # 向后端发送请求超时设置为 10 分钟 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8084; server_name lms.envision-aesc.cn; location / { proxy_pass http://10.202.12.141:9000; 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_read_timeout 600s; # 等待后端响应超时设置为 10 分钟 proxy_connect_timeout 600s; # 连接后端超时设置为 10 分钟 proxy_send_timeout 600s; # 向后端发送请求超时设置为 10 分钟 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
检查服务端的CORS配置
c.AddPolicy(Appsettings.app(new string[] { "Startup", "Cors", "PolicyName" }), policy => { policy .WithOrigins(Appsettings.app(new string[] { "Startup", "Cors", "IPs" }).Split(',')) .AllowAnyHeader()//Ensures that the policy allows any header. .AllowAnyMethod() .AllowCredentials();// 允许凭据。必须!如果没有这行会导致使用域名时出现跨域问题 });
收余恨、免娇嗔、且自信、改性情,休恋逝水,苦海回身,早悟兰因。

浙公网安备 33010602011771号