nginx: HTTPS and Custom Domain with mkcert on localhost

Here are three steps for HTTPS and Custom Domain:

  1. Edit the hosts file
  2. Edit the nginx.conf
  3. Install mkcert

1) Edit the hosts file

Add the code like so:

127.0.0.1 my-website.test

2) Edit the nginx.conf

Add the code in the [http] section:

http {
    server {
        listen       80;
        server_name  my-website.test;
        root   /opt/homebrew/var/www/my-website.test/;
        index  index.html index.htm;
    }
}

3) Install mkcert

Here are the steps to use mkcert:

$ brew install mkcert
$ mkcert -install
$ mkcert my-website.test

You can find the keys in the folder where you enter the command. Lastly, connect the certificates in nginx.conf like so:

http {
    server {
        listen       80;
        listen 443 ssl;
        ssl_certificate /opt/homebrew/var/www/ssl/my-website.test;
        ssl_certificate_key /opt/homebrew/var/www/ssl/my-website.test-key.pem;
        server_name  my-website.test;
        root   /opt/homebrew/var/www/my-website.test/;
        index  index.html index.htm;
    }
}

That's it. If it's not working, restart your nginx or laptop.