Using nginx as a proxy
17 Nov 2016When running applications in docker containers, it can make sense to put a proxy server in front. It’s relatively simple to setup an nginx server to sit in front of any application which I’ll demonstrate in this article.
Configuration
In order to get started, we’ll use the nginx image hosted up on dockerhub. This particular image allows us to specify a configuration file to a web server relatively simply.
To setup the scenario, we have a node.js application running on port 3000
of the host machine that we’d look to proxy through the nginx proxy. Here’s how the configuration would look, over port 80
:
There’s even a rewrite
here that takes the my-api
part of the original request URI out of the forwarded request, so that the node.js application can be treated directly off the root.
Start me up!
To now get this started, we need to sub-in this configuration file as if it were part of the running container.
Security
Yep. Now we need to use SSL and put the application over 443
! First up, let’s create a self-signed certificate using OpenSSL.
Now that we’ve got our certificate nginx.crt
and key nginx.key
, we can change the configuration now to proxy our application securely:
Now when we start up the container, we not only need to expose 443
for SSL, but we’ll also volume-in our certificate and key:
Now you can proxy your other dockerized web-applications through nginx without much hassle at all.