
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)
Fix NET::ERR_CERT_COMMON_NAME_INVALID when testing local domains in /etc/hosts. Learn mkcert SAN multi-domain certificates, Nginx and HSTS caveats.
Table of Contents
- How to fix NET::ERR_CERT_COMMON_NAME_INVALID with the hosts file
- Why this error happens during hosts file testing
- Solution comparison matrix
- Recommended fix: Generate SAN certificates with mkcert
- 1. Install mkcert
- 2. Install the local certificate authority
- 3. Generate a certificate covering all hosts file domains
- 4. Configure your local web server (Nginx example)
- Why you should avoid .dev and .app for local testing
NET::ERR_CERT_COMMON_NAME_INVALID occurs when the SSL/TLS certificate presented by your web server does not match the domain name typed into the browser. When you use your hosts file to route a test or staging domain to your local machine, the default certificate returned by your server is rejected by browser security checks.
How to fix NET::ERR_CERT_COMMON_NAME_INVALID with the hosts file
To fix NET::ERR_CERT_COMMON_NAME_INVALID when using the hosts file, install a local certificate authority with `mkcert -install`, generate a TLS certificate that includes all your test domains and subdomains (using Subject Alternative Names), and configure your local web server (Nginx, Caddy, or Apache) to serve this certificate over port 443.
Why this error happens during hosts file testing
When you add a custom entry to your hosts file:
127.0.0.1 myclient-site.testHere is what happens during the network request:
myclient-site.test to 127.0.0.1 via your hosts file.127.0.0.1.localhost or a different vhost).myclient-site.test against the allowed names in the certificate's Subject Alternative Name (SAN) field.NET::ERR_CERT_COMMON_NAME_INVALID.See also our guide on local HTTPS certificates and fixing mkcert certificate authority errors.
Solution comparison matrix
| Method | Security Level | Browser Compatibility | Best Use Case |
|---|---|---|---|
mkcert certificate | High (trusted local CA) | Seamless (Chrome, Safari, Firefox) | Local development on .test domains |
| Caddy automatic reverse proxy | High | Fully automatic | Microservices and local reverse proxying |
| Self-signed certificate without SAN | Low | Rejected by modern Chrome | Not recommended |
| Let's Encrypt DNS challenge | High | Universal | Staging servers accessible over the internet |
Recommended fix: Generate SAN certificates with mkcert
Since the deprecation of the legacy Common Name field in favor of Subject Alternative Name (SAN, RFC 2818), modern certificates must explicitly list all domain aliases.
1. Install mkcert
On macOS (via Homebrew):
brew install mkcert
brew install nss # Recommended for Firefox supportOn Linux (Ubuntu/Debian):
sudo apt install libnss3-tools
curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64"
chmod +x mkcert-v*-linux-amd64
sudo cp mkcert-v*-linux-amd64 /usr/local/bin/mkcertOn Windows (PowerShell via Chocolatey):
choco install mkcert2. Install the local certificate authority
mkcert -installThis registers a custom root CA in your operating system trust store.
3. Generate a certificate covering all hosts file domains
Include all your test hostnames, wildcards, and loopback addresses in one command:
mkcert myproject.test "*.myproject.test" api.myproject.test localhost 127.0.0.1This produces two files in your current working directory:
myproject.test+4.pem(the public certificate)myproject.test+4-key.pem(the private key)
4. Configure your local web server (Nginx example)
server {
listen 443 ssl http2;
server_name myproject.test api.myproject.test;
ssl_certificate /path/to/myproject.test+4.pem;
ssl_certificate_key /path/to/myproject.test+4-key.pem;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
}
}Reload Nginx (sudo nginx -s reload) and refresh the page. The green lock icon will display without certificate warnings.
Why you should avoid .dev and .app for local testing
Some developers use myproject.dev or myproject.app in their hosts file.
.devand.appare real Top-Level Domains owned by Google Registry.- They are included in the browser HSTS preload list.
- Any connection attempt is converted to strict HTTPS, and browsers strictly block the "Proceed to site" bypass option if the certificate is invalid.
For local development in /etc/hosts, always use reserved TLDs specified by RFC 2606:
.test(e.g.,app.test).example.localhost
Read why you should use .test for local development for more details.
Frequently Asked Questions
This error indicates that the SSL/TLS certificate returned by the local server does not match the exact domain name typed into the browser address bar.
The hosts file only handles network routing (IP resolution). The SSL certificate is presented by the web server during the TLS handshake after the IP connection is established.
Use mkcert to create a locally trusted certificate authority (CA) and issue a certificate covering your custom test domains (.test or .local).
Domains under preloaded HSTS TLDs like .dev and .app require valid TLS certificates. Chrome strictly forbids manual bypass on HSTS-enforced domains.
Related Articles
Is Editing the Hosts File Safe? (2026)
Is it safe to edit the hosts file? Risks, backups, what can go wrong, when to use hosts vs DNS, and how to revert changes on Windows, Mac and Linux.
Sleezr Team
Fix mkcert NET::ERR_CERT_AUTHORITY_INVALID
Fix NET::ERR_CERT_AUTHORITY_INVALID with mkcert on Mac by reinstalling the local CA, regenerating certificates and checking hostnames.
Sleezr Team
Developer tools team
Local HTTPS on Mac with mkcert (Vite, Next.js & .test Domains)
Install mkcert on Mac, trust the local CA, and generate HTTPS certs for localhost and .test domains. Includes Vite, Next.js, Docker, and NET::ERR_CERT_AUTHORITY_INVALID fixes.
Sleezr Team
ERR_CONNECTION_REFUSED on Hosts File Domains: 6 Fixes (2026)
Custom domain in your hosts file returning ERR_CONNECTION_REFUSED? Fix port mismatches, 0.0.0.0 vs 127.0.0.1 binding, Docker mappings and reverse proxies.
Sleezr Team
Developer tools team
Fix EACCES: permission denied on /etc/hosts (2026)
Fix EACCES: permission denied when editing /etc/hosts in Node.js, CLI tools and shell scripts. Learn safe sudo elevation, tee syntax and permissions.
Sleezr Team
Developer tools team
Fix ERR_TOO_MANY_REDIRECTS with Hosts File (2026)
Infinite redirect loop (ERR_TOO_MANY_REDIRECTS) after hosts file edits? Fix WordPress siteurl, Nginx X-Forwarded-Proto, www mismatches and 301 caches.
Sleezr Team
Developer tools team