tutorials|August 30, 2022|2 min read

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

TL;DR

Create a subdomain in Cloudflare DNS, configure Nginx as a reverse proxy in Docker to route the subdomain to your admin panel (e.g., Strapi on port 8080), and secure it with SSL.

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

Introduction

I have my main website, which I run on Lets say: gyanbyte.com. Now, there is my admin panel, or admin-ui which is a separate deployable artifact altogether. My admin panel runs on port 8080. It can be any CMS like Strapi, which runs on 8080.

Website runs with Cloudflare.

One way to open admin panel is IP:8080. But, I want to use SSL and a proper domain name. Another way could be to write a proxy which redirects /admin to that admin panel. But, there is no such path that can differentiate my main website UI and this admin panel.

Create Subdomain in Cloudflare

Login to cloudflare, open DNS settings for your website. I assume, you have two records already:

  • A-record with name as your website name, value as your hosting IP
  • CNAME-record with name as www and value as yourwebsite.com

Now, we need to add another CNAME record. With name as admin and value as yourwebsite.com So, I want my website to open as admin.yourwebsite.com

Save it. Now, it is the turn to configure your nginx proxy.

Generate SSL Cert for admin Subdomain

Before moving to Nginx, you also need to create SSL certificate. I used Lets-Encrypt. Now, I have my cert for three names:

Configure Nginx Proxy

Configuration file:

upstream my_ui {
    least_conn;

    server my_ui:3000 max_fails=3 fail_timeout=60 weight=1;
}
upstream my_api {
    ip_hash;
    least_conn;

    server my_api:8080 max_fails=3 fail_timeout=60 weight=1;
}
upstream my_admin {
    ip_hash;
    least_conn;

    server my_api:8080 max_fails=3 fail_timeout=60 weight=1;
}

server {
    listen 80;
    server_name yourwebsite.com www.yourwebsite.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name yourwebsite.com www.yourwebsite.com;

    keepalive_timeout   70;
    client_max_body_size 50M;

    ssl_certificate /etc/letsencrypt/live/yourwebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourwebsite.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location /  {
        add_header Pragma "no-cache";
        add_header Cache-Control "no-cache, must-revalidate";
        gzip_static on;

        proxy_pass http://my_ui/;
    }

    location /api/ {
        rewrite ^/api/?(.*)$ /$1 break;
        proxy_pass http://my_api;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }
}

server {
    listen 443 ssl;
    server_name admin.yourwebsite.com;

    keepalive_timeout   70;
    client_max_body_size 50M;

    ssl_certificate /etc/letsencrypt/live/yourwebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourwebsite.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location /  {
        add_header Pragma "no-cache";
        add_header Cache-Control "no-cache, must-revalidate";
        gzip_static on;

        proxy_pass http://my_admin/;
    }
}

Restart or reload your Nginx server. I use docker, just restart your containers.

And, it will just open well, with your admin.yourwebsite.com

Hope it helps.

Related Posts

How to Deploy Strapi with Next.js Frontend with Nginx Proxy and URL Redirect with Docker

How to Deploy Strapi with Next.js Frontend with Nginx Proxy and URL Redirect with Docker

Agenda I will cover following in this post: Prepare Docker image for Next.js app…

How to Copy Local Docker Image to Another Host Without Repository and Load

How to Copy Local Docker Image to Another Host Without Repository and Load

Introduction Consider a scenario where you are building a docker image on your…

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

Introduction In our previous post How to configure Grafana on docker, we saw how…

How to connect to a running mysql service on host from a docker container on same host

How to connect to a running mysql service on host from a docker container on same host

Introduction I have a host running mysql (not on a container). I have to run an…

How to configure Grafana (Free version) with oAuth Okta, with SSL on Docker,Nginx and Load dashboard from json

How to configure Grafana (Free version) with oAuth Okta, with SSL on Docker,Nginx and Load dashboard from json

Introduction In this post, we will see: use Grafana Community Edition (Free…

React JS router not working on Nginx docker container

React JS router not working on Nginx docker container

Problem Statement I developed a simple ReactJS application where I have used…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…