You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux服务器Nginx配置下Blazor默认WebSocket连接异常求助

问题解决方案

一、解决Nginx启动警告

警告原因是重复设置了相同的proxy_set_header字段,导致代理头部哈希表无法优化。直接移除重复的头部配置即可,若仍有警告,可在http块添加proxy_headers_hash_max_size 2048;。

二、解决Blazor WebSocket连接错误

Blazor默认WebSocket需要Nginx代理时传递升级相关头部,你的HTTPS服务块完全缺失这些配置,且HTTP服务块的跳转逻辑位置错误(导致WebSocket请求无法正确跳转)。修正点:

  • 将HTTP服务的301跳转指令移到location外部,确保所有HTTP请求强制跳转到HTTPS
  • 在HTTPS服务的location /中添加WebSocket代理必需的配置项

修正后的完整Nginx配置

events {
    worker_connections 1024;
}

http {
    map $http_connection $connection_upgrade {
        "~*Upgrade" $http_connection;
        default keep-alive;
    }

    # 可选:若仍有哈希警告,取消下方注释
    # proxy_headers_hash_max_size 2048;

    # HTTP 强制跳转 HTTPS
    server {
        listen 80;
        server_name fanatic360.xyz;

        # 所有HTTP请求直接跳转HTTPS,放在location外确保生效
        return 301 https://$host$request_uri;
    }

    # HTTPS 主服务配置
    server {
        listen 443 ssl;
        server_name fanatic360.xyz;

        ssl_certificate /etc/letsencrypt/live/fanatic360.xyz/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/fanatic360.xyz/privkey.pem;
        ssl_session_timeout       1d;
        ssl_protocols             TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers off;
        ssl_ciphers               ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
        ssl_session_cache         shared:SSL:10m;
        ssl_session_tickets       off;
        ssl_stapling              off;

        add_header X-Frame-Options DENY;
        add_header X-Content-Type-Options nosniff;

        location / {
            proxy_pass http://bspeaks_app:8080;
            # 基础代理头部
            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 必需配置
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_http_version 1.1;
            proxy_buffering off;
            proxy_cache off;
            proxy_read_timeout 100s;
        }
    }
}

内容的提问来源于stack exchange,提问作者Muheeb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:01:06