
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.
Manage hosts files without the terminal
ToggleHosts helps you manage environments visually on Windows, macOS, and Linux, with automatic DNS flush and backups.
One-time payment
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:
- The browser resolves
myclient-site.testto127.0.0.1via your hosts file. - The browser initiates a secure HTTPS connection on port 443 of
127.0.0.1. - The local server returns its active SSL certificate (often issued for
localhostor a different vhost). - The browser compares
myclient-site.testagainst the allowed names in the certificate's Subject Alternative Name (SAN) field. - Because there is no match, Chrome and Safari block the request with
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)Editing the hosts file
Is Editing the Hosts File Safe? (2026)
8 min read
Fix mkcert NET::ERR_CERT_AUTHORITY_INVALIDTroubleshooting
Fix mkcert NET::ERR_CERT_AUTHORITY_INVALID
5 min read
Local HTTPS on Mac with mkcert (Vite, Next.js & .test Domains)Local development
Local HTTPS on Mac with mkcert (Vite, Next.js & .test Domains)
7 min read