How to Setup Cloudflare Origin Certificates with Nginx on Ubuntu Linux

dual screen 1745705 640

This post shows students and new users steps to create Cloudflare origin certificates for use with Nginx on Ubuntu Linux. If you really want to enhance your server security using Cloudflare, make sure to use its Full SSL (Strait) SSL/TLS and Origin Certificate with your setup.

Using Cloudflare’s origin certificate, you can create an end-to-end SSL/TLS encryption between both your servers and Cloudflare proxy servers thus making sure that all connections to your servers are encrypted.

You can create a free certificates signed by Cloudflare to install on the origin server, and because the certificate is free and provided by Cloudflare, you can choose a longer validation period — which can be set to up to 15 years, and the ability to include all your subdomains with a wildcard *.example.com.

We showed you how to do that with Apache2 HTTPS server.

When you’re ready to setup your server and Cloudflare to use Origin Certificate, follow the steps below:

How to sign up for Cloudflare

The first step in this tutorial is to sign up for Cloudflare account. This post assumes that you already have registered a domain name. If you don’t, then go and get one before continuing further.

If you already have a Cloudflare account, then skip the registration below.

https://dash.cloudflare.com/sign-up

Type in your email address and click Create Account.

Cloudflare WordPress setup

Once the account is created and you’ve verified your email address and logged back into Cloudflare account, click the button or link (Add a Site) to add a site to your account.

Cloudflare WordPress setup

Next, type in the domain name you have registered. Cloudflare service will help speed up and protect the site you add.

Cloudflare WordPress setup

Next, Cloudflare will begin to query your domain DNS provider for the records in the DNS table. If the domain is online, Cloudflare should find it and import the records into its DNS systems.

Cloudflare WordPress setup

After that, select the plan you want to use for the site. For this tutorial, we’re going to be using Cloudflare free plan.

Cloudflare WordPress setup

When you’re done, you should see two nameservers provided to you by Cloudflare. What you need to do is logon to your domain provider’s portal. where you have your domain, and replace the nameservers with the ones Cloudflare gives you.

cloudflare setup name servers

For example, our example.com site is hosted with Google Domains. Logon to your Google Domains account and select to use custom nameservers.

You’ll have to option to enter the nameservers provided to you by Cloudflare. Save your changes when you’re done.

Cloudflare WordPress Setup

Once you’ve saved your custom nameservers changes,  go back to your Cloudflare account and wait for Cloudflare to see the changes. Depending on your domain provider, it make take up to an hour for the DNS changes to be visible on Cloudflare.

cloudflare overview active

Once all is ready, you’ll see your site status as Active.

When everything is done, you should also see your Cloudflare account with DNS entries as shown below. Your DNS records might have more entries then the two below. These two entries are the most important for running your website.

Cloudflare WordPress Setup

After that, click on Crypto tab and choose to enable Full (strict) SSL. This should turn on SSL for the site.

Cloudflare WordPress Setup

While still on Crypto tab, scroll down to Origin Certificates. Then click the button to create certificate.

Use the free TLS certificate signed by Cloudflare to install on your origin server. Origin Certificates are only valid for encryption between Cloudflare and your origin server.

cloudflare wordpress setup 8

Next, choose to Let Cloudflare generate a private key and a CSR for the domain. Click Next.

WordPress Cloudflare

Then copy a paste these into a text file on onto your server.

On Ubuntu, run the commands below to create the Private key, Certificate and Origin pull files (3 files in total). Copy and paste each content into the respective file. and save.

For the Private key file. run this, then copy and paste the private key given to you into the file and save.

sudo nano /etc/ssl/private/cloudflare_key_example.com.pem

For the certificate file, run this and copy and paste the certificate content into the file and save.

sudo nano /etc/ssl/certs/cloudflare_example.com.pem

You’ll also want to download Cloudflare Origin Pull certificate. You can download that from the link below:

Set up authenticated origin pulls · Cloudflare SSL docs

Zone-Level — Cloudflare certificate

Under Zone-level certificate, expand the certificate button, the copy its content.

Next, run the commands below to create a origin-pull-ca.pem file, then paste the certificate content into the file below and save.

sudo nano /etc/ssl/certs/origin-pull-ca.pem

Once done, you should have three files. The cloudflare_key_example.com.pem, cloudflare_example.com.pem and origin-pull-ca.pem.

We will use these file in Nginx config below

cloudflare wordpress setup 10

After saving the key, certificate and origin pull certificates files. Continue below.

Still on the Crypto page in your Cloudflare account, enable Always use HTTPS and you may also change settings for HSTS but not necessary.

cloudflare wordpress setup 11

Next, turn on Authenticated Origin Pulls and Opportunistic Encryption, and continue.

cloudflare wordpress setup 12

Then, turn on Automatic HTTPS Rewrites and continue.

cloudflare wordpress setup 13

Next, move to the Page Rules tab. then create a new rule for the site. then type URL and choose Always Use HTTPS

http://* example.com/*

Alwyas Use HTTPS

cloudflare wordpress setup 15

Save your settings and you’re done with setting up Cloudflare.

How to configure Nginx with Cloudflare

Finally, configure Nginx site configuration file for your website. This file will control how users access your website content. Run the commands below to create a new configuration file called example.com

sudo nano /etc/nginx/sites-available/example.com

Then copy and paste the content below into the file and save it. Replace the highlighted line with your own domain name and directory root location.

Also make sure to reference the certificate files created above during Cloudflare setup.

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name  example.com www.example.com;
    root   /var/www/html/example.com;
    index  index.php;

    ssl_certificate /etc/ssl/certs/cloudflare_example.com.pem;
    ssl_certificate_key /etc/ssl/private/cloudflare_key_example.com.pem;
    ssl_client_certificate /etc/ssl/certs/origin-pull-ca.pem;
    ssl_verify_client on;

    client_max_body_size 100M;
  
    autoindex off;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

Save the file and exit.

How to enable server block with Nginx

After configuring the server block above, enable it by running the commands below

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service

Then open your browser and browse to the server domain name.

That should do it!

Conclusion:

This post showed you how to enable Cloudflare origin certificate to enhance and secure the connection between Cloudflare’s server and your own servers. If you find any error above, please use the comment form below to report.

3 Comments

  1. Just a heads up, in Step 2: Configure NGINX, the command line example there has apache2 instead of NGINX

  2. this article is DATED, and has hella cramatical issues.

    1. please post link to newer article

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.