Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The browser refuses a ws:// connection opened from your HTTPS React app

ErrorFailed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.

Short answer

Your page is on HTTPS but the socket URL starts with ws://, and browsers refuse that mix. Use a wss:// URL, or a relative URL that follows the page's scheme, and make sure something in front of the socket server speaks TLS. Local development hosts are the one exception, and only some of them.

  • Applies to React 19, Chrome stable, nginx 1.26, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the socket URL is built. A hard-coded ws:// string or an environment variable holding one is the usual source.

    grep -rn -E "ws://|new WebSocket" src/ .env* 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The error appears only on the deployed HTTPS site, not on http://localhostThe URL is hard-coded with the ws:// schemeFix
    2. 2Switching the URL to wss:// makes the connection fail or hang insteadThe socket server does not speak TLSFix
    3. 3After wss:// works, the socket closes by itself when nothing is sentConnections idle for a minute drop behind the proxyFix
    4. 4ws:// works for localhost or a private IP but fails for a custom hostnameLocal development against a device on your networkFix

What does the error look like?

  • Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.

Cause 1 of 4

The URL is hard-coded with the ws:// scheme

1Check

Search the source and the build-time environment files for the ws:// string. Browsers do not allow a non-secure socket from a page loaded over HTTPS.

grep -rn "ws://" src/ .env* 2>/dev/null

2Fix

Use wss:// for the socket. The WebSocket constructor also accepts a relative URL and maps https to wss, so a path alone follows whatever scheme the page uses.

// explicit secure URL
const socket = new WebSocket("wss://example.com/ws");

// or relative, resolved against the page (https page -> wss)
const socket2 = new WebSocket("/ws");

3Verify

Reload the deployed page with the console open; the constructor no longer throws.

# in the browser console after load
new WebSocket('wss://example.com/ws').readyState

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 4

The socket server does not speak TLS

1Check

wss is WebSocket over TLS, on port 443 by default. A plain Node.js or Java server on a high port answers wss:// with garbage. Test it from the shell.

curl -vkI https://example.com/ws

2Fix

Terminate TLS in a reverse proxy and pass the upgrade headers to the plain socket server. nginx needs Upgrade and Connection set explicitly, because they are hop-by-hop headers.

location /ws {
    proxy_pass http://127.0.0.1:3001;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

3Verify

Test the config, reload, and open the socket from the HTTPS page.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

Cause 3 of 4

Connections idle for a minute drop behind the proxy

1Check

nginx closes a proxied WebSocket that sends no data for 60 seconds by default.

grep -rn "proxy_read_timeout" /etc/nginx/

2Fix

Raise proxy_read_timeout, or send WebSocket ping frames from the server at a shorter interval.

location /ws {
    proxy_read_timeout 300s;
}

3Verify

Leave the page idle past a minute and check the socket stays open.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

Cause 4 of 4

Local development against a device on your network

1Check

Chrome exempts ws:// from mixed content blocking only when the host is recognisably local: a loopback or private IP literal, localhost or a .local name. A custom hostname is not exempt.

grep -rn "ws://" src/

2Fix

Use wss:// for a local endpoint reached through a custom hostname.

const socket = new WebSocket("wss://devbox.example.test:8443/ws");

3Verify

Open the page and watch the console for the error.

# browser console: no mixed content error

Confirmed by developer.chrome.com

Did this fix it?

How do I stop it coming back?

  • Never write a scheme into socket URLs; build them from the page or from one config value.
  • Run the staging site on HTTPS too, so this fails before release.
  • Send ping frames from the server so proxies do not close idle sockets.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.