Question

Installing Cloudflare edge certificates on a Droplet with dockerised Nginx

Sorry, I know that was quite a mouthful.

Here’s what I’m trying to do in order to get a Cloudflare tunnel to connect from the internet to an application running on a Droplet:

  • Install SSL cert on Dockerized Nginx

I’m happy to use either the free cert that Cloudflare generates or a dedicated / third-party one which I purchased (whatever gives the least trouble).

If I want to go with the Cloudflare option: I have the certs. I’m just lost and confused as to how to get them into the Nginx container (which I think has to be a container for the app/architecture that it’s part of.)

Any pointers or ways to do this simply?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 17, 2024

Hey!

One quick option here would be to setup an Nginx service on the Droplet itself and use Let’s encrypt with certbot for the SSL management:

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

Then in the Nginx config you can setup a reverse proxy rule to forward the traffic from the Nginx service running on the Droplet on port 443 to the docker container itself.

Alternatively, as you mentioned, setting up Cloudflare edge certificates on a Dockerized Nginx instance running on a Droplet would require a few key steps.

First, you need to have your Cloudflare SSL certificates ready. These usually include:

  • A .pem file for the certificate itself
  • A .key file for the private key

If you have chosen to use the free Universal SSL certificate from Cloudflare, you should have these files. If you opt for a dedicated certificate or one purchased from a third party, ensure you have the corresponding files.

After that, you need to modify your Docker configuration to include these certificates and ensure Nginx uses them. Here’s how you can do it:

Step A: Create a Directory for Certificates

On your Droplet, create a directory to store your SSL certificates. This directory will be mounted to the Docker container.

mkdir -p /etc/nginx/certs
cp path_to_your_certificate.pem /etc/nginx/certs/
cp path_to_your_private_key.key /etc/nginx/certs/

Step B: Update Nginx Configuration

Modify your Nginx configuration file to use the SSL certificates. You can create a new configuration file or edit an existing one.

server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /etc/nginx/certs/your_certificate.pem;
    ssl_certificate_key /etc/nginx/certs/your_private_key.key;

    location / {
        proxy_pass http://your_application;
    }
    # The rest of your Nginx configuration
}

Make sure to replace yourdomain.com, your_certificate.pem, and your_private_key.key with the actual names.

Step C: Update Dockerfile or Docker Compose File

To make the certificates available inside the container, you need to mount the directory you created into the Docker container. Here’s an example modification for a Docker Compose file:

services:
  nginx:
    image: nginx:latest
    ports:
      - "443:443"
    volumes:
      - /etc/nginx/certs:/etc/nginx/certs
      - ./path_to_your_nginx_conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app

Once your Docker container is running with the new configuration, you might need to reload Nginx to apply the changes without stopping the container:

docker exec -it <nginx-container-id> nginx -s reload

Next, make sure your DNS settings in Cloudflare are set to proxy through Cloudflare. This setting is indicated by an orange cloud icon in your DNS settings. This setup will encrypt the traffic between Cloudflare and your Nginx server using the SSL certificate you installed.

Finally, test your setup to ensure that everything is configured correctly. You can use tools like curl or visit your domain in a browser to check the SSL certificate details.

curl -Iv https://yourdomain.com

Let me know if you have any questions!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel