mkcert + Vite & Next.js Local HTTPS (2026)
Configure mkcert with Vite and Next.js for trusted local HTTPS. Full vite.config.js, next dev --experimental-https, .test domains + hosts file setup.
Locahl Team
Table of Contents
Run mkcert once, then wire the certificates into Vite or Next.js for trusted https:// local dev.
One-time mkcert setup
brew install mkcert
mkcert -install
mkdir -p certs
mkcert -cert-file certs/local-cert.pem -key-file certs/local-key.pem \
localhost 127.0.0.1 ::1 myproject.test "*.myproject.test"Add to hosts (see edit hosts on Mac or Windows 10 & 11):
127.0.0.1 myproject.test
127.0.0.1 api.myproject.testFull mkcert walkthrough: mkcert SSL local on Mac.
---
Vite + mkcert (React, Vue, Svelte)
// vite.config.js
import { defineConfig } from 'vite';
import fs from 'fs';
import path from 'path';
const certDir = path.resolve(__dirname, 'certs');
export default defineConfig({
server: {
host: 'myproject.test',
port: 5173,
https: {
key: fs.readFileSync(path.join(certDir, 'local-key.pem')),
cert: fs.readFileSync(path.join(certDir, 'local-cert.pem')),
},
},
});Open https://myproject.test:5173 — green padlock, Service Workers enabled.
Vite env variables
# .env.local
VITE_API_URL=https://api.myproject.test:3001Run a second Vite/API server on api.myproject.test with the same cert (wildcard *.myproject.test covers it).
---
Next.js + mkcert
Option A: built-in experimental HTTPS (recommended)
next dev --experimental-https \
--experimental-https-key ./certs/local-key.pem \
--experimental-https-cert ./certs/local-cert.pemAdd to package.json:
{
"scripts": {
"dev": "next dev --experimental-https --experimental-https-key ./certs/local-key.pem --experimental-https-cert ./certs/local-cert.pem"
}
}Option B: custom .test hostname
mkcert must include the exact hostname. Regenerate if needed:
mkcert myproject.test localhost 127.0.0.1Add to /etc/hosts or Windows hosts, then open https://myproject.test:3000.
---
Monorepo: Vite frontend + Next.js API
| Service | URL | Config |
|---|---|---|
| Next.js API | https://api.myproject.test:3000 | next dev --experimental-https ... |
| Vite SPA | https://myproject.test:5173 | vite.config.js https block |
Use one mkcert command with both names:
mkcert myproject.test api.myproject.test "*.myproject.test" localhostAfter hosts changes, flush DNS on Mac or ipconfig /flushdns on Windows.
---
Troubleshooting
| Error | Fix |
|---|---|
| NET::ERR_CERT_AUTHORITY_INVALID | mkcert -install, restart browser → [[mkcert-net-err-cert-authority-invalid-mac |
| Certificate hostname mismatch | Regenerate mkcert including exact URL hostname |
| Vite HMR WebSocket fails | Set server.hmr: { host: 'myproject.test' } in vite.config.js |
| Next.js only serves localhost | Pass hostname via --hostname myproject.test if needed |
---
.gitignore
certs/
*.pemCommit a scripts/setup-certs.sh instead of the keys.
Conclusion
mkcert + hosts + Vite/Next.js gives production-like HTTPS locally. Generate certs once, map .test domains in hosts, point each dev server at the PEM files, and flush DNS after hosts edits.
Ready to simplify your workflow?
Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.
- Intuitive visual interface
- Automatic DNS flush
- Multi-environment management
- Automatic backups
- JSON Import/Export
Frequently Asked Questions
Does Next.js support mkcert certificates natively?
Yes — Next.js 13+ supports next dev --experimental-https with --experimental-https-key and --experimental-https-cert pointing to mkcert PEM files.
Can Vite use a custom .test domain with mkcert?
Yes. Generate mkcert for myproject.test, add 127.0.0.1 myproject.test to hosts, set server.host in vite.config.js.
Where is the full mkcert install guide?
See the complete [[https-local-ssl-certificate-mac|mkcert SSL local setup on Mac]] — this article focuses on Vite and Next.js wiring.
Related Articles
Edit Hosts File on Windows 11 24H2 (2026)
Windows 11 24H2 hosts file guide: Smart App Control, Controlled Folder Access, Notepad admin, PowerShell, ipconfig /flushdns and Clear-DnsClientCache.
Locahl Team
DNS & Hosts File Cheat Sheet (Mac, Windows, Linux)
Printable cheat sheet: flush DNS commands, hosts file paths, mkcert setup, and browser cache clears for macOS, Windows 11, and Linux (2026).
Locahl Team
Developer tools team
Can You Edit the Hosts File Without Admin Rights?
Can you edit the hosts file without administrator rights? The honest answer plus real alternatives on Windows, Mac and Linux when you cannot elevate (proxy, Docker, local DNS, SSH).
Locahl Team
Developer tools team
How to Use Subdomains on localhost for Local Development
Use subdomains on localhost: the free *.localhost trick browsers resolve automatically, fixed subdomains via the hosts file, and wildcard subdomains with dnsmasq on Mac and Linux.
Locahl Team
Developer tools team
Flush DNS on Windows 10 & 11: ipconfig /flushdns (2026)
Flush the DNS cache on Windows 10 and 11 with ipconfig /flushdns. Step-by-step command, verification, and fixes when changes still do not apply.
Locahl Team
Developer tools team