Nginx

如何解決 Nginx WebSocket 安全(wss)“需要升級錯誤 426”?

  • May 5, 2020

我試圖在我的 Nginx 伺服器上配置一個 Websocket 代理,但不幸的是,我沒有讓它工作。我在這裡閱讀了各種文章,但我無法讓伺服器正常工作。我認為這與客戶端與伺服器的連接有關。本地 om 我的電腦一切正常

客戶端程式碼:

var port = new osc.WebSocketPort({
         url: "wss://circusfamilyprojects.nl/"
       });

伺服器程式碼:

var wss = new WebSocket.Server({
   port: 8083
});

這是我在 Ngnix 上的配置:

# custom code for hop by hop headers
   map $http_upgrade $connection_upgrade {
       default upgrade;
       ''      close;
   }


   #custom code for connection with websocket this is the port on the server
    upstream websocket  {
       server 178.62.209.37:8083;
   }

server {

     listen [::]:443 ssl ipv6only=on; # managed by Certbot
     listen 443 ssl; # managed by Certbot
     ssl_certificate /etc/letsencrypt/live/circusfamilyprojects.nl/fullchain.pem; # managed by Cert$
     ssl_certificate_key /etc/letsencrypt/live/circusfamilyprojects.nl/privkey.pem; # managed by Ce$
     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

      root /var/www/html/vue/cfdomotica/server/public;

       # Add index.php to the list if you are using PHP
       index index.html index.htm index.nginx-debian.html;

      server_name  circusfamilyprojects.nl www.circusfmailyprojects.nl; # managed by Certbot

     location / {
           proxy_pass http://websocket;
           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_pass_request_headers on;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection $connection_upgrade;

       }
}

server {
   if ($host = www.circusfamilyprojects.nl) {
       return 301 https://$host$request_uri;
   } # managed by Certbot


   if ($host = circusfamilyprojects.nl) {
       return 301 https://$host$request_uri;
   } # managed by Certbot


       listen 8080 ;
       listen [::]:8080 ;
   server_name  circusfamilyprojects.nl;
   return 404; # managed by Certbot
}

在我的瀏覽器中,我收到消息“狀態程式碼:需要 426 升級”希望有人能幫助我,在此先感謝!

我自己解決了這個問題,將埠 443 的伺服器塊和 WebSocket 客戶端的伺服器塊分開。我在埠 8086 上為客戶端做了伺服器塊。

引用自:https://serverfault.com/questions/1015277