【问题标题】:WebSocket Nginx 426 Upgrade requiredWebSocket Nginx 426 需要升级
【发布时间】:2021-11-03 21:18:05
【问题描述】:

我有一个网络聊天 (example.com/chat) 在我的服务器上监听 tcp://0.0.0.0:50500。 我还配置了 nginx 反向代理以将数据从我的 example.com/chat 发送到 0.0.0.0:50500。

我的网站 nginx 配置是这样的:

map $http_upgrade $connection_upgrade {
  default Upgrade;
  ''      close;
}
server {
    server_name example.com www.example.com  ;
    listen 5.4.....0:443 ssl  http2 ;

    listen [2a0......:a4]:443 ssl  http2 ;

    ssl_certificate "/var/www......._51.crt";
    ssl_certificate_key "/var/www/.......51.key";
    add_header Strict-Transport-Security "max-age=31536000" always;

    charset utf-8;
    gzip on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/css text/xml application/javascript text/plain application/json image/svg+xml image/x-icon;
    gzip_comp_level 1;

    set $root_path /var/www/user/data/www/example.com;
    root $root_path;
    disable_symlinks if_not_owner from=$root_path;

    location / {

        proxy_pass http://127.0.0.1:81;
        proxy_redirect http://127.0.0.1:81/ /;
        include /etc/nginx/proxy_params;
    }
    
    location ~ ^/chat {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://0.0.0.0:50500;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 300s;
        proxy_buffering off;
  }


     location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpeg|avi|zip|gz|bz2|rar|swf|ico|7z|doc|docx|map|ogg|otf|pdf|tff|tif|txt|wav|webp|woff|woff2|xls|xlsx|xml)$ {
        try_files $uri $uri/ @fallback;
        expires 30d;

    }

    location @fallback {
        proxy_pass http://127.0.0.1:81;
        proxy_redirect http://127.0.0.1:81/ /;
        include /etc/nginx/proxy_params;
    }

    include "/etc/nginx/fastpanel2-sites/fastuser/example.com.includes";
    include /etc/nginx/fastpanel2-includes/*.conf;
    error_log /var/www/user/data/logs/example.com-frontend.error.log;
    access_log /var/www/user/data/logs/example.com-frontend.access.log;
}


server {
    server_name example.com www.example.com  ;
    listen 5.4.....0:80;
    listen [2a.....:a4]:80;

    return 301 https://$host$request_uri;

    error_log /var/www/user/data/logs/example.com-frontend.error.log;
    access_log /var/www/user/data/logs/example.com-frontend.access.log;
}

网络聊天配置为使用这些设置:

SOCKET_CHAT_URL="wss://example.com"
SOCKET_CHAT_PORT=50500

由于我有升级标头,426 Upgrade required 错误对我来说看起来很奇怪。 我知道有很多与此问题相关的类似线程,但是,它们都建议创建一个我已经拥有的升级标头。

我已经尝试过:

  • 同时使用 SOCKET_CHAT_URL="ws://example.com" 和 "wss://example.com"
  • 将 proxy_pass 行更改为 https:https://0.0.0.0:50500;
  • 将 WebSocket 行更改为服务器 IP:wss://123.312.123.321
  • wss://example.com/chat 格式

另外,我的标题:

General
Request URL: https://example.com/chat
Request Method: GET
Status Code: 426 
Remote Address: 5**.***.***.*50:443
Referrer Policy: strict-origin-when-cross-origin

Response Headers
date: Mon, 06 Sep 2021 21:11:50 GMT
sec-websocket-version: 13
server: nginx/1.18.0
strict-transport-security: max-age=31536000
upgrade: websocket
x-powered-by: Ratchet/0.4.3

Request Headers
:authority: example.com
:method: GET
:path: /chat
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: uk-UA,uk;q=0.9
cache-control: max-age=0
sec-ch-ua: "Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.31 Safari/537.36

【问题讨论】:

    标签: linux nginx websocket debian


    【解决方案1】:

    好的,服务器运行在 HTTP/2 上,不支持 WebSockets。 此外,使用 Nginx 无法仅将 1 个站点切换到 HTTP/1.1,您应该为此切换整个服务器。

    我们已经切换到 sockets.io

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 2019-07-29
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      相关资源
      最近更新 更多