Square CloudSquare Cloud

Mixed Content: an HTTPS page loading resources over HTTP

Mixed Content happens when a page loaded over HTTPS requests a resource over HTTP. The browser blocks scripts, stylesheets, fonts and API calls, and tries to load images, audio and video over HTTPS; the fix is serving everything over HTTPS.

Updated 7 min read

Learning objectives

After reading this article you will be able to:

  • Read the Mixed Content messages in Chrome and Firefox
  • Know what the browser blocks and what it upgrades to HTTPS
  • Fix hardcoded URLs, HTTP APIs, WebSockets and links generated behind a proxy
On this page

Everything worked on localhost. After the deploy, with HTTPS, the product list doesn't load, a font is missing and the browser console turns red with "Mixed Content". The code is the same; what changed is the page's scheme.

A page loaded over HTTPS that requests anything over http:// mixes a protected connection with one that anybody on the path can read and change. The browser responds by blocking that request or by retrying it over HTTPS. Below are the messages, the rules and the fix for each cause.

What is Mixed Content?#

Mixed Content is any resource that a page loaded over HTTPS requests over HTTP or another insecure protocol.

It is not a cosmetic issue. A script downloaded over HTTP can be swapped by whoever controls the network, on public Wi-Fi for example, and that script then controls the whole page: it reads what the user types, changes links and sends data elsewhere. web.dev sums it up by saying an attack on active mixed content lets an attacker do almost anything to the page. The page's HTTPS is worthless if part of it arrives unprotected. The basics are in HTTP vs HTTPS and what is SSL/TLS.

What does the error look like in the console?#

In Chrome, a blocked resource produces this message (here, a fetch call to the API):

text
Mixed Content: The page at 'https://www.example.com/' was loaded over HTTPS, but requested an insecure resource 'http://api.example.com/products'. This request has been blocked; the content must be served over HTTPS.

Instead of resource, you will see script, stylesheet, font, image, frame or XMLHttpRequest endpoint, depending on the type. When the browser retries the request over HTTPS, the message is a warning, not an error:

text
Mixed Content: The page at 'https://www.example.com/' was loaded over HTTPS, but requested an insecure element 'http://www.example.com/img/logo.png'. This request was automatically upgraded to HTTPS, For more information see https://blog.chromium.org/2019/10/no-more-mixed-messages-about-https.html

A WebSocket with ws:// has its own message:

text
Mixed Content: The page at 'https://www.example.com/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://api.example.com/ws'. This request has been blocked; this endpoint must be available over WSS.

In Firefox, the equivalent messages are:

text
Blocked loading mixed active content “http://api.example.com/products”
Upgrading insecure display request ‘http://www.example.com/img/logo.png’ to use ‘https’

In your JavaScript, a blocked fetch rejects like any network failure, with a TypeError that doesn't say why. The reason is in the console and, in Chrome, also in the DevTools Issues tab, which lists every insecure resource on the page.

What does the browser block, and what does it upgrade to HTTPS?#

The W3C Mixed Content specification splits requests into two groups. Upgradeable content is retried over HTTPS automatically. Everything else is blockable and does not load.

Resource Example What the browser does
Plain image <img src="http://..."> Retries over HTTPS
Audio and video <audio src>, <video src>, <source> Retries over HTTPS
Responsive image <img srcset>, <picture> Blocks
Image on an IP address <img src="http://203.0.113.10/a.png"> Blocks
Script and stylesheet <script src>, <link rel="stylesheet"> Blocks
API calls fetch(), XMLHttpRequest, sendBeacon() Blocks
Iframe, font, <object> <iframe src>, @font-face Blocks
WebSocket new WebSocket("ws://...") Blocks

Two details in that table catch a lot of people. First, the upgrade has no plan B: if the server doesn't answer over HTTPS, the image simply doesn't load, and the browser doesn't fall back to HTTP. Second, an IP address instead of a domain turns the upgrade off: the specification doesn't upgrade those requests, and the browser blocks them.

Older names are still around: "passive" or "display" content was the upgradeable kind, and "active" was the blockable kind. Firefox still uses display and active in its messages. Details vary a little between browsers, so test in the ones your audience uses.

Why does the error only show up after deploying?#

In development, the page opens over http://localhost, and an HTTP page calling an HTTP API mixes nothing. Even with local HTTPS, MDN notes that loopback addresses such as http://localhost and http://127.0.0.1 count as secure origins, so calling them is not mixed content. In production the page opens over HTTPS, and every http:// URL left in the code becomes Mixed Content. The causes, from most to least common:

  1. An API URL with http:// in a build-time variable. VITE_API_URL, NEXT_PUBLIC_API_URL and the like are baked into the JavaScript at build time. If they point to http:// or to an IP:port, the HTTPS front end can't call the API.
  2. Absolute http:// URLs in HTML, CSS or content. Old templates, images pasted into an editor and the site URL stored in a CMS database.
  3. Links generated by the application itself behind a proxy. When a reverse proxy terminates HTTPS and forwards HTTP to the app, the framework thinks the request was HTTP and builds redirects and absolute URLs with http://. The same mix-up causes redirect loops.
  4. A WebSocket with ws:// hardcoded.
  5. A third-party resource that only exists over HTTP, or that loads from an IP address.

How do you find every HTTP resource?#

The console only shows the open page and clears on every navigation, so walk through your main pages with DevTools open, as web.dev recommends. To avoid relying on your eyes alone, search the code and the published HTML:

bash
grep -rnI "http://" src public
curl -s https://www.example.com/ | grep -oE 'http://[^" >]+'

Plain links (<a href="http://...">) usually aren't mixed content, because they load nothing into the page. On large sites or sites with user content, a report-only policy shows what real visitors run into, without blocking anything:

http
Content-Security-Policy-Report-Only: default-src https: 'unsafe-inline' 'unsafe-eval'; report-uri /csp-report

The browser sends a report to /csp-report for every resource not served over HTTPS. 'unsafe-inline' and 'unsafe-eval' keep inline scripts out of the reports; data: and blob: URLs still show up, so filter for what matters.

How do you fix each case?#

Resources on your own site: use relative paths or https://, as MDN advises. A path that starts with / inherits the page's scheme:

html
<!-- before -->
<img src="http://www.example.com/img/logo.png" alt="Logo">
<!-- after -->
<img src="/img/logo.png" alt="Logo">

In a CMS, change the site URL in the settings to https:// and replace the old URLs stored in the content.

An API over HTTP: no setting on the page makes the browser accept it. Give the API HTTPS, with a domain and a certificate (Let's Encrypt issues them at no cost), or serve the API from the site's own domain, on a route such as /api, with your server forwarding the calls. Then update the variable and run a new build:

bash
VITE_API_URL=https://api.example.com

With the API on another domain, even over HTTPS, the CORS rules apply.

WebSocket: build the URL from the page, so it uses wss:// in production and ws:// on localhost without code changes:

js
const url = new URL("/ws", window.location.href);
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
const socket = new WebSocket(url);

Other connection failures are covered in WebSocket connection failed.

Links generated behind a proxy: teach the framework to trust the proxy. In Express, app.set("trust proxy", 1) makes req.protocol reflect the X-Forwarded-Proto header. Only turn it on when there really is a proxy in front: the Express documentation points out that if the trusted proxy doesn't overwrite the X-Forwarded-* headers, the client can send any value it likes. When you don't know what the proxy sends, the simplest way out is to store the public URL in a variable (PUBLIC_URL=https://www.example.com) and build absolute links from it. The proxy's role is explained in what is a reverse proxy.

Safety net: the upgrade-insecure-requests directive tells the browser to treat every http:// URL on the page as https://, third-party ones included (navigation links to other sites are not changed). Send it as a header or as a tag in <head>:

http
Content-Security-Policy: upgrade-insecure-requests
html
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

It doesn't create HTTPS where there is none: a resource unavailable over HTTPS still fails. And it doesn't replace HSTS, which protects the navigation of visitors who arrive through an http:// link. Both are covered in HTTP security headers.

How to do this on Square Cloud#

On Square Cloud, the page already ships over HTTPS: every published web application gets a *.squareweb.app subdomain with automatic TLS, on every plan, and traffic arriving over HTTP is redirected to HTTPS. From Standard up, a custom domain gets an automatic DV certificate that renews by itself. That fixes the page, not the resources: an http:// URL inside your code is still Mixed Content.

Your application listens on 0.0.0.0 on the port in the PORT variable, and the platform publishes that traffic over HTTPS and WSS. So when your front end calls an API of yours hosted there, use the public address with https://, and wss:// for a WebSocket. In a front end built with Vite, VITE_API_URL=https://my-api.squareweb.app belongs in the environment where the build runs (CI or your machine), because the value is baked into the files. A server's PUBLIC_URL, used to build absolute links, goes in the application's environment variables and applies on the next restart.

The headers your application sends, such as HSTS, pass through untouched, so Content-Security-Policy: upgrade-insecure-requests can come from your own server. On a static site, the <meta> tag does the same job. See the hosting features for sites on the Sites page.

Website hosting

Static and SSR sites with automatic TLS and a global CDN

Website hosting

FAQs

Why does my site work on localhost and break after deploying?

In development the page usually opens over http://localhost, so nothing is mixed. In production it opens over HTTPS, and every http:// URL your code still uses gets blocked or upgraded by the browser.

Does upgrade-insecure-requests fix Mixed Content?

It makes the browser request the page's resources over HTTPS, third-party ones included. It only works if the resource exists over HTTPS: otherwise the request fails, with no fallback to HTTP. It is a safety net, not a replacement for fixing the URLs.

How do I call an API that only answers over HTTP from an HTTPS site?

Put the API behind HTTPS, with a domain and a TLS certificate, or have an HTTPS server of yours forward the calls to it. The browser won't let an HTTPS page call an API over HTTP.

What are passive and active mixed content?

They are the older names for the two categories. The current specification talks about upgradeable content (images, audio and video, which the browser retries over HTTPS) and blockable content (everything else). Firefox still says display and active in its messages.

Next articleCore Web Vitals: LCP, INP and CLS explainedCore Web Vitals are Google's three page experience metrics: LCP, INP and CLS. Learn the good thresholds, how to measure them and what your server can change.

More in Websites

See all 25 articles