Skip to content

Instantly share code, notes, and snippets.

@mehmetsefabalik
Last active April 28, 2024 07:37
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mehmetsefabalik/257aab5a9ce69deb01f71d8b5be25256 to your computer and use it in GitHub Desktop.
Save mehmetsefabalik/257aab5a9ce69deb01f71d8b5be25256 to your computer and use it in GitHub Desktop.
Enable https on your local environment with nginx

enable https on your local environment

install mkcert and create certificates

brew install mkcert
mkcert -install
mkcert local.place-your-domain-here.com localhost 127.0.0.1 ::1

Two .pem files will be generated. rename them as domain.pem and domain-key.pem

add 127.0.0.1 local.place-your-domain-here.com to your /private/etc/hosts file

create nginx config file

touch nginx.conf

events {

}

http {
  server {
    listen 443 ssl;
    listen [::]:443 ssl;
    ssl_certificate ~/domain.pem;
    ssl_certificate_key ~/domain-key.pem;
    server_name local.place-your-domain-here.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
  }
}

run nginx

nginx -c nginx.conf -p .

now you can open https://local.place-your-domain-here.com

@MarvinXu
Copy link

MarvinXu commented Apr 2, 2024

It works! But I get an "not safe" error from the browser

@tikhcloud
Copy link

It works! But I get an "not safe" error from the browser

This is okay. You can add certificate to your browser or just ignore this warning. You are the one who generate this certificate and you have nothing to worry about ;) Also, make sure you use mkcert for testing and developing purposes, this is not suitable for production, and never share your rootCA-key.pem with anyone

@MarvinXu
Copy link

MarvinXu commented Apr 8, 2024

My nginx server is on WSL Ubuntu. I tried mkcert in WSL and import RootCA.pem in the host machine but still gets an error. Then I run mkcert in Windows cmd and copy generated certs to WSL and the error goes away :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment