question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Websockets fail with HTTPS and http/2 if there is a reverse proxy on the line.

See original GitHub issue

ViteJS fails to upgrade to websockets when using HTTPS and http/2 and there is a reverse proxy between the server and the browser.

Reproduction

Create a new container and install a blank new ViteJS project.

npm init @vitejs/app test --template vue
cd test
npm install
npx vite --https --host my-test.mydomain.com --port 8443

Install a reverse proxy, and tell it to use your container, and a test domain. For example haproxy on rpm based systems:

dnf -y install haproxy

Set /etc/haproxy.cfg to this minimal config:

frontend port8443
    bind *:8443 ssl crt /etc/haproxy/certificate.pem alpn h2,http/1.1
    default_backend default

backend default
    server my-test 10.0.0.4:8443 ssl verify none alpn h2,http/1.1

… where 10.0.0.4 is the IP of your container with the vitejs instance.

certificate.pem should be a certificate valid for a domain. Use this domain in /etc/hosts to map to the ip of the haproxy server.

Once it’s all set up, open the domain in the browser. Vite will load, … but then, it will go into an endelss loop trying to reconnect. The broswer’s inspector will have a message, its a bit hard to catch:

Logs

In chrome, observe:

[vite] connecting...
client:200 WebSocket connection to 'wss://my-test.mydomain.com:8443/' 
failed: Error during WebSocket handshake: Unexpected response code: 404
(anonymous) @ client:200

In firefox:

[vite] connecting...
Firefox can’t establish a connection to the server at wss://my-test.mydomain.com:8443/.

Workaround

Use only http/1.1 - in haproxy, remove alpn h2,http/1.1 on both sides. However, this might be not an option for people using hosting services.

System Info

  • vite version: vite/2.0.0-beta.36
  • Operating System: Fedora 32 / 33
  • Node version: tested with v12.19.0, v12.20.1 and v14.14.0
  • Package manager (npm/yarn/pnpm) and version: npm 6.14.8, 6.14.10

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
xorikcommented, Jun 17, 2021

It works perfectly in vue cli. It said:

It seems you are running Vue CLI inside a container. and after that websocket for HMR works perfectly, even if port on the proxy is different. E.g. I have https://some.server/ that proxies to http://localhost:8080.

But for the vite dev server, it totally ignores the port from URL, and uses port from the --port argument (3000 by default). So I’m forced to do something like https://some.server:3000 (same port as on vite dev), to make it work.

Would be nice to have similar behavior with vue cli.

UPD this config helped with the issue:

  server: {
    hmr: {
      host: 'some.server,
      port: 443,
      protocol: 'wss'
    }
  }
3reactions
benatkincommented, Jun 17, 2021

Adding @xorik’s code to vite.config.js worked. If there’s some way to automatically detect it, that would be nice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NGINX double proxy not allowing websocket connections
I can confirm that normal http/https requests are working with my double proxy setup. Here's my current config. Proxy 1 server { #...
Read more >
How to use Nginx as a Reverse proxy for HTTPS and WSS
Step 2 - Create the SSL Certificate. TLS/SSL works by using a combination of a public certificate and a private key. The SSL...
Read more >
Why WebSockets over HTTP/2 (RFC8441) is Critical for ...
In this video, I'll discuss RFC8441 bootstrapping WebSockets with HTTP/2 which is a critical protocol to allow WebSockets tunneling to scale ...
Read more >
Constant websocket errors - Troubleshooting
This log line would appear whenever the server fails to read any message within a given timeout, and it disconnects the websocket connection ......
Read more >
Using SSE Instead Of WebSockets For Unidirectional Data ...
The reverse, when the server is proactively pushing updates to your website, it is called server push. Nowadays, there are a few ways...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found