Chapter 3 · Lesson 21 of 22
Domains and HTTPS

Mixed content explained and how to fix it

Mixed content is an HTTPS page that loads some resources, such as scripts, images or frames, over plain HTTP, which browsers block or upgrade because it weakens the page's security.

By the end you will

Find mixed content in the browser console, fix the links and stop a proxied app from generating http URLs.

Time
12 min
Level
Intermediate
Check
4 questions
You need
  • A site that loads over HTTPS
  • Access to the app code or reverse proxy config

Mixed content happens when a page delivered over HTTPS asks for another resource, such as a script, image or iframe, over plain HTTP. The page itself is encrypted, but that one request is not, so anyone on the network can read or alter it. Browsers react by blocking or rewriting those requests. The typical symptoms are a missing image, a broken script, a missing padlock or a console full of warnings right after you switched a site to HTTPS.

How it works#

Every subresource on a page has a URL, and the scheme at the start of it (http or https) decides how it is fetched. On an HTTPS page, an http URL is a downgrade. Browsers split such requests into two groups:

  • Active mixed content: scripts, stylesheets, iframes, fonts and fetch or XHR calls. These can rewrite the page, so a tampered response compromises everything. Browsers block them.
  • Passive (display) mixed content: images, audio and video. These cannot run code, but can leak information or mislead users. Current browsers generally try to upgrade them to https automatically and may block them when no secure version exists.

Exact behavior varies by browser and version, so do not rely on the browser quietly fixing things. The reliable fix is to serve every resource over HTTPS.

Step 1: Find the offending URLs#

Open the page, open devtools (F12), and look at the Console. Mixed content messages name the exact URL that was blocked or upgraded. The Network tab, filtered by “http:”, also lists them. Search your own codebase and content too:

bash
grep -rn "http://" templates/ static/ --include="*.html" --include="*.css" --include="*.js"

The paths here are placeholders, use your project’s directories. Expect hits in HTML templates, CSS url() values, JavaScript constants, CMS content stored in a database, and third party embeds. Remember that XML namespaces such as http://www.w3.org/2000/svg are identifiers, not requests, and are not a problem.

Choose the first option that applies:

  1. Change the URL to https, and verify that the host actually serves it:
    bash
    curl -sI https://cdn.example.net/library.js | head -n 1
  2. If the file is yours, serve it from your own site and use a relative or root-relative path such as /assets/logo.png. Relative paths follow the page’s scheme.
  3. Drop resources that only exist on http. If a third party has no https version, host a copy or remove the dependency.

Avoid protocol-relative URLs like //example.com/x.js in new code. They were a workaround for the http era and are now unneeded, since https works everywhere.

For content stored in a database (blog posts, CMS fields), run a careful search and replace on the stored URLs using your CMS tooling, and take a backup first.

Step 3: Add a safety net with CSP#

The upgrade-insecure-requests directive of Content-Security-Policy tells the browser to rewrite http subresource URLs to https before fetching them. In nginx:

nginx
add_header Content-Security-Policy "upgrade-insecure-requests" always;

Reload with sudo nginx -t && sudo systemctl reload nginx. This is useful while you clean up old content, but it only works when the https version of the resource exists. It does not fix a missing file, and it does not change your stored data. Treat it as a bridge, not a replacement for fixing the URLs. If you already set a Content-Security-Policy header, add the directive to it rather than sending two headers.

Step 4: Fix apps behind a proxy#

A very common cause is an application behind a reverse proxy. The proxy terminates TLS and forwards plain HTTP to the app, so the app believes every request is insecure and builds absolute links, redirects and asset URLs starting with http://. The page then loads fine but pulls everything back over http.

The proxy has to tell the app what the original scheme was. In nginx:

nginx
location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Then the app must trust that header from the proxy. Each framework has a setting for it (often called trust proxy, proxy headers or forwarded allow ips). Check your framework documentation, and only trust the header from your own proxy, since clients can send fake ones. Without that setting, the header is ignored. See Nginx reverse proxy with certbot for a full proxy block and Redirect HTTP to HTTPS explained for redirect loops that have the same root cause.

Check it worked#

Reload the page with the console open and caching disabled in the Network tab. No mixed content messages should remain and the padlock should show a secure connection. From the command line, fetch the HTML and look for http references:

bash
curl -s https://example.com/ | grep -o 'http://[^"'"'"' )]*' | sort -u

Any URL that appears is a candidate to fix, other than harmless identifiers such as XML namespaces and plain links to other sites.

Edge cases and gotchas#

  • Third party widgets: an old embed or analytics snippet hard codes http. Update the snippet or drop it.
  • CSS and JS inside files: url(http://...) inside a stylesheet or an API base URL in a script bundle is easy to miss in templates.
  • Redirect to http: an https URL that redirects to http still counts as an insecure load.
  • Forms posting to http: a form whose action is http sends data in clear text, and browsers warn about it.
  • WebSockets: use wss:// from an HTTPS page, since ws:// is blocked.
  • HSTS makes it worse: once HSTS is on, any leftover http link to your own host is upgraded by the browser, which hides it from you until the policy expires.

Troubleshooting#

SymptomLikely causeFix
Console says a script was blocked as mixed contentThe page requests an http script URLChange it to https or a relative path
Images missing, others finePassive content with no https versionHost the image yourself or fix the URL
App links and redirects use httpProxy does not send X-Forwarded-Proto, or the app does not trust itAdd the header and enable the app’s proxy trust setting
Fixed templates but old posts still breakStored content in the database has http URLsUpdate stored content after a backup
upgrade-insecure-requests added but resource still failsThe https version of the resource does not existFix the host or serve a copy
Redirect loop after enabling HTTPSThe app sees http and redirects to https againPass X-Forwarded-Proto and trust it

Quick reference#

nginx
proxy_set_header X-Forwarded-Proto $scheme;
add_header Content-Security-Policy "upgrade-insecure-requests" always;
bash
curl -s https://example.com/ | grep -o 'http://[^"'"'"' )]*' | sort -u

Next#

Continue with TLS versions and ciphers in plain language.

Check what you learned4 questions
1Which of these is active mixed content that browsers block?
2What does the Content-Security-Policy directive upgrade-insecure-requests do?
3Where do you find mixed content errors while debugging?
4An app behind nginx generates http:// links. Which header should the proxy pass so the app knows the original scheme?

Frequently asked questions

What is mixed content?

It is an HTTPS page that pulls in resources over plain HTTP, such as images, scripts or iframes. The secure page is only as safe as its weakest request, so browsers restrict it.

What is the difference between active and passive mixed content?

Active content (scripts, stylesheets, iframes, fetch requests) can change the page and is blocked. Passive content (images, audio, video) is lower risk and browsers have moved toward upgrading or blocking it too.

How do I fix mixed content on my website?

Change the resource URLs to https or to relative paths, and make sure the resource exists on https. As a safety net you can add the upgrade-insecure-requests CSP directive.

Why does my app behind a reverse proxy create http links?

The proxy forwards requests to the app over HTTP, and the app builds absolute URLs from what it sees. Pass X-Forwarded-Proto from the proxy and configure the app to trust it.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios