Caddyfile Advanced Setup

Self-Hosting Jan 2, 2021

Our Caddy Setup Guide can be found here:

Caddy v2 Reverse Proxy Simple Setup Guide
What is Caddy? Caddy has a wide range of use cases including: * Web Server * Reverse Proxy * Sidecar Proxy * Load Balancer * API Gateway * Ingress Controller * System Manager * Process Supervisor * Task Scheduler Today we will be installing and setting up Caddy as a Reverse Proxy. This w…

What will be covered?

Headers

What are headers?
HTTP headers are name or value pairs that are displayed in the request and response messages within the message header for HTTP. These Headers can be to inform the browser of the data it is receiving such as a PNG or HTML content. Headers can also be sent to increase the security and privacy for your website visitors.

Recomended HTTP Security Headers

Caddyfile Header Example:

example.com {
  reverse_proxy localhost:8080
  header {
        Strict-Transport-Security max-age=31536000;
        X-Content-Type-Options nosniff;
        X-XSS-Protection "1; mode=block;"
        X-Robots-Tag none;
        X-Frame-Options SAMEORIGIN;
        Referrer-Policy no-referrer;
  }
}

Want to remove a header such as server: Caddy?

example.com {
  reverse_proxy localhost:8080
  header {
      -server
  }
}

Place - infront of the header to strip it.

Custom SSL Certificates

If you have a Custom Certificate it can be added with the following syntax.
tls <PEM File Path> <Private Key File Path>

Caddyfile Custom Certificate Example:

example.com {
  reverse_proxy localhost:8080
  tls /file/path/certificate.pem /file/path/certificate.key
}

ZeroSSL ACME Setup

If you would rather use ZeroSSL instead of Let's Encrypt to issue SSL certificates for your website, it can be configured as follows.

Caddyfile Custom ACME Example:

example.com {
  reverse_proxy localhost:8080
  tls [email protected] {
    ca https://acme.zerossl.com/v2/DV90
  }
}

File Server

Caddy comes with a built in file browser. Simple just set the directory you want to serve and Caddy handles the rest.

Views:
caddy-file-browser-1
caddy-file-browser-2

Caddyfile File Server Example:

example.com {
  root * /file/path/content
  file_server browse 
}

You can hide files with the following syntax:

example.com {
  root * /file/path/content
  hide homework.jpeg
  file_server browse 
}

Caddy File Browser Docs

Our other Caddy Guides:

How to enable Caddy HTTP Basic Authentication (basicauth) Guide
What is HTTP Basic Authentication?When visiting a website, protected with Basic Auth the browser will prompt theuser to enter a username and password before any recourses are loaded. The client will then send an Authorization header with each request to thewebsite, to maintain authentication.T…

Cover Image Credit: https://unsplash.com/@ibecomecommunication

Tags