Build a secure connection with NGINX container and Cloudflare

Cloudflare, one of the most important security platform in the world, is an interesting solution for surely publish and maintain contents over the internet. The ability to handle DNS acts as a reverse proxy and take care of the incoming connection from the Internet to my own server are the main reasons why I choose this platform for my website… BTW I keep under control my websites gaining the ability to make some improvements like certificate handling (I dream a world without HTTP clear connections).

In this post, I’ll show how to successfully configure Cloudflare to establish a secure connection with NGINX (in a container using Jwilder) and also handle a valid certificate for worldwide browsers.

The architecture

The architecture is very simple: all you need is a 2nd level Internet domain, a Cloudflare account configured as authoritative DNS and a host with docker and docker-compose installed. (For this project you can use also Kubernetes with an on-premise load-balancer).

The configurations

If networking is well configured, the steps to successfully realize this solution are really easy. The important things are the host availability and the correct certificate permissions.

All starts with the docker host… Just use this configuration for the front-end

And here you’ll find the template. Just make sure that the following statements are included in the container:

After docker-compose up, it’s time to set up the “real” backend application. In this case, it’s possible to use my simple Apache-PHP image for application container:

Pay attention to the network: in order to ensure the correct communication through server frontend and backend, it’s important to specify to use the correct network name, in this case, “fe_network”, just check the output of the command

Cloudflare certificate and tunings

The next steps are:

  • Create and use Cloudflare or 3rd party SSL certificate:
    • Under Crypto menu, go to Edge Certificates and be sure you’ve got a universal certificate.
    • Optionally you can order an SSL Certificate or upload a previously purchased.
  • Create the Origin certificate
    • It is possible to issue a single origin certificate (with private key) per application or wildcard certificate, depending on the deployment. (I highly suggest to issue a per application certificate to avoid unpredictable behaviors)
    • Then simply copy the private keys and the certificates in the ./nginx/certs directory following the naming [fqdn].key and [fqdn].crt (Note: it’s possible to copy and paste if you’re on the SSH terminal) (Note2: remember to “chown/chmode” the certificate and the private key to avoid any forbidden usage)
    • After a Jwilder refresh (depending on the version/installation in some case it’s necessary to compose down and up the application containers) the connection between Cloudflare and NGINX container is ready.
  • Tune Cloudflare in order to force all element to work together:
    • Under SSL (Crypto menu), choose SSL mode to Full (make sure you’ve got an active universal certificate)
    • Set to “ON” the section Always use HTTPS and also enable the automatic HTTPS Rewrite
  • Test: Just use your preferred browser and point the URL to https://[fqdn]. All will work showing the “green” locket and check if you’re using the right certificate: the issuer must be Cloudflare.

That’s all folks! Enjoy!

   Send article as PDF