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

Node+Nginx部署WebSocket应用遇426 Upgrade Required错误求助

Alright, let's break down why you're seeing that 426 Upgrade Required error and fix it up.

First, the root cause here is that your Nginx config is forwarding all requests (including regular HTTP page loads) to your Node.js WebSocket server—but that server only knows how to handle WebSocket handshake requests, not standard HTTP GET/POST requests.

Your test code works because it's directly initiating a WebSocket connection (which sends the required Upgrade: websocket header), so the handshake goes through smoothly. But when you try to load your website in a browser, the browser sends a regular HTTP GET request to / with no Upgrade header. Your WebSocket server receives this, realizes it's not a WebSocket handshake, and throws the 426 error to signal "hey, I need an upgrade to WebSocket protocol here".

Now let's fix this with two common, practical solutions:


Solution 1: Split Requests in Nginx (Separate Static Files and WebSocket)

Your Nginx config already has a root /var/www/html line, but right now the location / block is overriding it and forwarding everything to your WebSocket server. Let's adjust it so static pages are served directly by Nginx, and only WebSocket traffic goes to Node.js:

Update your Nginx server block like this:

server {
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name mywebsite.com www.mywebsite.com;

    # Serve static HTML/CSS/JS directly from /var/www/html
    location / {
        try_files $uri $uri/ =404;
    }

    # Forward WebSocket traffic to your Node.js server
    location /ws {
        proxy_pass http://127.0.0.1:8080/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /path/to/cert; # managed by Certbot
    ssl_certificate_key /path/to/key; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

Then update your client's WebSocket connection string to point to the new path:

var ws = new WebSocket("wss://mywebsite.com/ws");

Your existing Node.js WebSocket server code doesn't need any changes—it'll handle the /ws path just fine.


Solution 2: Add HTTP Serving to Your Node.js App

If you want your Node.js app to handle both static pages and WebSocket connections (instead of relying on Nginx for static files), you can use a framework like Express to serve static content alongside your WebSocket server:

First install Express if you haven't already:

npm install express

Then update your Node.js code:

const express = require('express');
const http = require('http');
const WebSocket = require('ws');

const app = express();
// Serve static files from a "public" directory (put your HTML/CSS/JS here)
app.use(express.static('public'));

// Create an HTTP server that uses Express for static content
const server = http.createServer(app);

// Attach the WebSocket server to the HTTP server
const wss = new WebSocket.Server({ server });

console.log("Server running on port 8080 started at: " + new Date());

wss.on('connection', function(ws) {
    console.log("Connected to websocket: " + ws);
    const introComment = JSON.stringify({
        user: "Welcome!",
        data: {
            body: "Welcome to the realtime feed!",
            name: "realtime-intro-connection-message",
        },
    });
    ws.send(introComment);
});

server.listen(8080);

With this setup, your original Nginx config will work as-is—Nginx forwards all requests to Node.js, which now handles both static page loads and WebSocket handshakes correctly.


Either solution will fix that 426 error. The first is better if you want Nginx to handle static asset caching and delivery (which is what it's optimized for), while the second keeps all your app logic centralized in Node.js.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:57:52