Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Caddy configuration for Mattermost with automated TLS management #161

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions caddy/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
domain1.example.com {
reverse_proxy mattermost:8065
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
X-XSS-Protection "1; mode=block"
}
}



domain2.example.com {
reverse_proxy mattermost:8065
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
X-XSS-Protection "1; mode=block"
}
}
49 changes: 49 additions & 0 deletions docker-compose.caddy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This Docker Compose file sets up a multi-container application with Caddy and Mattermost services.
# Version 2.4 of the Docker Compose file format is used.

services:

# Caddy service configuration
caddy:
# Specifies that the Caddy service depends on the Mattermost service
depends_on:
- mattermost
# Sets the container name for the Caddy service
container_name: caddy-mattermost
# Uses the Caddy image with a tag specified by the CADDY_IMAGE_TAG environment variable
image: caddy:${CADDY_IMAGE_TAG}
# Sets the restart policy for the Caddy container, defined by the RESTART_POLICY environment variable
restart: ${RESTART_POLICY}
# Security options to disable new privileges
security_opt:
- no-new-privileges:true
# Limits the number of process IDs (PIDs) to 100
pids_limit: 100
# Sets the container file system to read-only mode
read_only: true
# Maps host ports to container ports for HTTP and HTTPS traffic
ports:
- ${HTTPS_PORT}:443
- ${HTTP_PORT}:80
# Mounts volumes for Caddy data and configuration
volumes:
- caddy_data:/data
- caddy_config:/config
- ${CADDY_CONFIG_PATH}:/etc/caddy/Caddyfile:ro

# Mattermost service configuration
mattermost:
# Maps host ports to container ports for Mattermost calls (both UDP and TCP)
ports:
- ${CALLS_PORT}:${CALLS_PORT}/udp
- ${CALLS_PORT}:${CALLS_PORT}/tcp

# Defines named volumes for persistent storage of Caddy data and configuration
volumes:
caddy_data: # Define volume for persistent Caddy config data
caddy_config:

# Defines a custom network named 'mattermost' for Let's Encrypt certificate renewal
networks:
default:
name: mattermost
157 changes: 157 additions & 0 deletions docs/caddy-support-mattermost.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@

# Configuring Caddy with Mattermost - Automated TLS

## Why Caddy for TLS Management?

1. **Zero-configuration HTTPS**: Unlike Nginx which requires manual Let's Encrypt certificate setup, Caddy automatically:
- Obtains certificates
- Renews before expiration
- Updates certificates in real-time
- Handles OCSP stapling

2. **No Additional Containers**: Unlike the Nginx setup which needs:
- Separate certbot container
- Manual renewal scripts
- Volume mounts for certificates
- Systemd timers for renewals

# Configuring Caddy with Mattermost

## Setting up Caddy as reverse proxy

**NOTE:** Commands with a **$** prefix denote those executed as user, **#** as root.

This guide explains how to configure Caddy as a reverse proxy for Mattermost, with automatic HTTPS certificate management.

### 1. Create Caddy configuration directory

```bash
$ mkdir -p ./caddy
$ touch ./caddy/Caddyfile
```

### 2. Basic Caddyfile configuration

Create

Caddyfile

with:

```caddyfile
your-domain.com {
reverse_proxy mattermost:8065
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
X-XSS-Protection "1; mode=block"
}
}
```

### 3. Start Mattermost with Caddy

```bash
$ docker-compose -f docker-compose.yml -f docker-compose.caddy.yml up -d
```

### 4. Verify Configuration

```bash
$ docker logs caddy-mattermost
```

### 5. Certificate Management

Caddy automatically handles SSL/TLS certificates through Let's Encrypt. Requirements:

- DNS A/CNAME records pointing to your server
- Ports 80/443 accessible
- Valid domain name

### 6. Environment Variables

Create `.env` file with:

```plaintext
CADDY_CONFIG_PATH=./caddy/Caddyfile
HTTPS_PORT=443
HTTP_PORT=80
RESTART_POLICY=unless-stopped
CADDY_IMAGE_TAG=2.7.4
```

### 7.Ensure A/CNAME records point to your server

```bash
dig +short your-domain.com
```

### 8. Verify Certificate

```bash
curl -vI https://your-domain.com 2>&1 | grep "SSL certificate"
```

These configurations provide automatic HTTPS, modern security headers, and reverse proxy functionality for Mattermost.





## Setup Guide

### 1. Configure DNS

```bash
$ # Ensure A/CNAME records point to your server
$ dig +short your-domain.com
```

### 2. Basic Caddyfile

```caddyfile
your-domain.com {
reverse_proxy mattermost:8065
# TLS configuration is automatic!
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
X-XSS-Protection "1; mode=block"
}
}
```

### 3. Start Services

```bash
$ docker-compose -f docker-compose.yml -f docker-compose.caddy.yml up -d
```

### 4. Verify Certificate

```bash
$ curl -vI https://your-domain.com 2>&1 | grep "SSL certificate"
```

## Key Benefits

1. **Automatic Management**
- No manual certificate renewal
- No certbot configuration
- No renewal scripts

2. **Security**
- Modern TLS defaults
- OCSP stapling enabled
- HTTP/2 support
- Automatic redirects

3. **High Availability**
- Zero-downtime renewals
- Certificate rotation
- Graceful reloads

This approach significantly simplifies TLS management compared to manual Nginx+certbot setup.
5 changes: 5 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ POSTGRES_DB=mattermost
## Note that this repository requires nginx version 1.25.1 or later
NGINX_IMAGE_TAG=alpine

# Caddy
## The folder containing server blocks and any additional config to Caddyfile
CADDY_CONFIG_PATH=./caddy/Caddyfile
CADDY_IMAGE_TAG=latest

## The folder containing server blocks and any additional config to nginx.conf
NGINX_CONFIG_PATH=./nginx/conf.d
NGINX_DHPARAMS_FILE=./nginx/dhparams4096.pem
Expand Down