How to Set Up HTTPS for Localhost in 30 Seconds
If you are an application engineer and need to implement OAuth, you might have typed https://localhost/ into your browser to access your application. It’s because some OAuth services only accept https for the redirect_url field. If you did it, unfortunately, you got an error message about SSL!
Of course, we can find a solution for this problem, but most of the articles recommended running a proxy server on the local machine directly. This method will not only take a time but make our machines dirty. Every programmer likes clean environment except for our room.
Then I made a containerized proxy which terminates SSL. I published this proxy on DockerHub and GitHub. If you are suffering from a local proxy, let’s try this!
For example, if you are a Mac user, all you have to do is just copy & paste this command, and you can access to https://localhost/ in 30 seconds (please change port number according to your application)! Oh, it’s your second time to launch this proxy? If so, it will take only 1 second!
docker run -it \
-e "HOST_IP=`ipconfig getifaddr en0`" \
-e "PORT=3000" \
-p 443:443 \
--rm \
esplo/docker-local-ssl-termination-proxyThe below sections are explanation for this program.
Accessing to localhost as https is a tiresome job
Accessing a local application server as https requires a certification and specific settings. Usually, an application server for development will be launched as http, so it is an annoying extra task.
The difficult points of a proxy server in Docker container
The problem is: how to insert an IP address of the host machine into the setting file for Nginx?
First difficult point is to obtain IP address of the host machine, which is required to access from a Docker container to the host network (i.e. the proxy to your local application). In Docker for Mac, localhost is unreachable from the host machine if the network mode is host(I suppose Docker on a Linux machine can do this), so I passed an IP of the host machine to a container in bridge mode. Also this IP address cannot be obtained from a Docker container, it has to be given as an environment variable.
Second difficult point is a setting file for Nginx, which is not capable to handle environment variables. Fortunately, envsubst command do great job solving this problem.
Other things are the same to launching a proxy server on your host machine directly.
Explanation of the code
I’ll explain some of the code in my repository.
Ordinary Dockerfile
This is an ordinary SSL Termination Proxy by Nginx. The setting file is not an actual file during the Docker build. envsubst is called in entrypoint.sh so as to replace environment variables in it when the proxy launches.
write names of environment variables in nginx.conf
In nginx.conf, the names of environment variables are specified and they are replaced in entrypoint.sh. The setting itself is pretty easy — just simple SSL settings and transfer accesses from 443 to your port.
http {
...
upstream app {
server ${HOST_IP}:${PORT};Conclusion
I introduced an easy to use proxy. If you have a trouble to access https://localhost/, let’s try and enjoy your original job!
If you know a single command to obtain a local IP address of a host machine on both Mac and Linux, please notify me!
