Local Web Development With HTTPS
When developing a web application I usually reach a point where the backend/API is ready, and I need to develop and test its frontend with the real API in conditions as close as possible to the supposed environment that it is going to run. One of those conditions is using HTTPS to access the API.
One common solution to this problem is to use a paid service like Ngrok, to map a public HTTPS enabled URL to a localhost port.
But in this post, I will describe another option that is free and does not require access to a remote service.
I will show you how to do it in a Debian machine but it can easily be ported to other Unix derivatives like Mac OSX.
Three steps are needed:
- Configure a local development domain name
- Create valid local SSL certificates
- Configure a local reverse proxy
Configure DNS
Supose the new local development domain name is api.local
As root, edit the file /etc/hosts
and add a line like this:
127.0.0.1 api.local
Create Certificates With Mkcert
Follow the installation instructions at Mkcert.
Install a local Certificate Authority (CA) with:
mkcert -install
Create a new certificate/key pair with a command like this:
mkcert api.local localhost 127.0.0.1 ::1
As root, copy the generated files to/etc/ssl/certs
.
Configure Nginx as a reverse HTTP proxy:
Install nginx:
sudo apt install nginx
Create a new file on /etc/nginx/sites-available
and name it like
api-local.conf
.
Copy and paste the following snippet to the configuration file. Pay attention to make the appropriate changes in:
- local server address and port
- log files folder
- cert files names and folder
|
|
As root, create a symbolic link to this file in the /etc/nginx/sites-enabled
folder:
ln -s /etc/nginx/sites-enabled/dev-local.conf /etc/nginx/sites-available/api-local.conf
Restart nginx:
sudo systemctl restart nginx
The configuration done so far does not need to be changed between reboots.
For every development or testing session, you only need to start the local
backend server. After that,
the API will be available at the address
https://api.local
.