Logo

dev-resources.site

for different kinds of informations.

Path-Based Reverse Proxying with Caddy

Published at
10/30/2024
Categories
caddy
server
vps
webserver
Author
nwby
Categories
4 categories in total
caddy
open
server
open
vps
open
webserver
open
Author
4 person written this
nwby
open
Path-Based Reverse Proxying with Caddy

Caddy is a great web server built with Go and can be used for a multitude of things. Here at Vizalo we use it on nearly all of our servers that power our network.

This guide explains how to set up Caddy as a reverse proxy that routes traffic to different backend services based on URL paths. This is useful when you have multiple services running on different ports and want to expose them under a single domain.

Basic Setup

Create a Caddyfile in your project directory:

example.com {
    handle /api/* {
        reverse_proxy localhost:3000
    }

    handle /admin/* {
        reverse_proxy localhost:8080
    }

    handle /* {
        reverse_proxy localhost:5000
    }
}
Enter fullscreen mode Exit fullscreen mode

This configuration will:

  • Send /api/* requests to a service running on port 3000
  • Send /admin/* requests to a service running on port 8080
  • Send all other requests to port 5000

A More Complete Example

Here's a more practical example that includes common settings you might need:

example.com {
    # API Service
    handle /api/* {
        reverse_proxy localhost:3000 {
            header_up Host {upstream_hostport}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
        }
    }

    # Admin Dashboard
    handle /admin/* {
        reverse_proxy localhost:8080 {
            # Health checks
            health_uri /health
            health_interval 30s

            # Timeout settings
            timeout 30s
        }
    }

    # Frontend App
    handle /* {
        reverse_proxy localhost:5000 {
            # Load balancing
            lb_policy round_robin
            lb_try_duration 30s
        }

        # Enable compression
        encode gzip
    }

    # Global options
    log {
        output file /var/log/caddy/access.log
        format json
    }
}
Enter fullscreen mode Exit fullscreen mode

Running Multiple Backend Services

For testing, you might run these simple backend services:

# API Service (Node.js/Express)
node api.js        # Runs on :3000

# Admin Dashboard (Go)
go run admin.go    # Runs on :8080

# Frontend (React)
npm start         # Runs on :5000
Enter fullscreen mode Exit fullscreen mode

Verifying the Setup

Test your configuration:

# Test API endpoint
curl example.com/api/users

# Test admin endpoint
curl example.com/admin/dashboard

# Test frontend
curl example.com
Enter fullscreen mode Exit fullscreen mode

Common Patterns

Stripping Path Prefixes

If your backend service doesn't expect the /api prefix:

handle /api/* {
    uri strip_prefix /api
    reverse_proxy localhost:3000
}
Enter fullscreen mode Exit fullscreen mode

Adding Headers

Add authentication headers or API keys:

handle /api/* {
    reverse_proxy localhost:3000 {
        header_up X-API-Key {env.API_KEY}
    }
}
Enter fullscreen mode Exit fullscreen mode

That's it. The beauty of Caddy is that it handles HTTPS certificates automatically and has sensible defaults for most settings. Most of the "extra" configurations shown above are only needed for specific use cases.

server Article's
30 articles in total
Favicon
Singularity: Streamlining Game Development with a Universal Framework
Favicon
VPS Servers for Linux - Everything You Need to Know
Favicon
SLOT THAILAND™ LINK SITUS SLOT GACOR TERBAIK GAMPANG MAXWIN 2024
Favicon
Why adventuresinminecraft.com is the Best Minecraft Server for Customization and Survival
Favicon
Free VPS Hosting and Windows VPS to Understand How They Work on Websites
Favicon
How to Send Emails in Python Using Gmail’s Free SMTP Mail Server API
Favicon
How to write GraphQL resolvers effectively
Favicon
Path-Based Reverse Proxying with Caddy
Favicon
How I Set Up My Own Server (and Why You Should Too)
Favicon
Unlocking the Power of AWS Route 53: Your Complete Guide to DNS Management
Favicon
Unlock Efficient Data Exchange: Import & Export Excel in ASP.NET Core
Favicon
Copy file between server and local machine ( from windows to linux server )
Favicon
Client Boundaries
Favicon
Server actions in Next.js
Favicon
Self-host - Part 3 - MySQL and PostgreSQL Database Backup to Local Drive
Favicon
need suggestions
Favicon
SQL Operators Made Easy for Beginners
Favicon
What is Localhost in Development Mode?
Favicon
How to Customize the Fastify Logger
Favicon
Websocket starter in Rust with client and server example
Favicon
Unraid: Das ultimative Tool für deine Heimserver 🚀
Favicon
Drawbacks to Using Rack Server Unit as Desktop Computer?
Favicon
How to View Your index.php File in a Browser
Favicon
NextJs Server Actions: Why and How
Favicon
Self-host - Part 2 - Zero-Downtime Deployment using Docker Swarm
Favicon
Decoding Web Hosting: Understanding the landlords of the digital Realm Landscape
Favicon
Kaptan ve Tayfa - Kaptan Sunucu Kurulumu
Favicon
argc, argv의 차이
Favicon
Server side (vulnerability scanning)
Favicon
Running npm install on a Server with 1GB Memory using Swap

Featured ones: