Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Vizalo profile imageSam Newby
Sam Newby forVizalo

Posted on

     

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 aCaddyfile 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 modeExit 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 modeExit 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 modeExit fullscreen mode

Verifying the Setup

Test your configuration:

# Test API endpointcurl example.com/api/users# Test admin endpointcurl example.com/admin/dashboard# Test frontendcurl example.com
Enter fullscreen modeExit 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 modeExit 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 modeExit 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.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
phocks profile image
Joshua Byrd
200 BIO OK
  • Email
  • Location
    Brisbane, Australia
  • Work
    Software Developer at ABC News Digital
  • Joined
• Edited on• Edited

Thanks for this. Just pointing out that if you use this config and then try to go to/api (without the trailing slash) it will be handled by the end /* fallback. Seems like you can fix this by doinghandle /api*

or by doing a redirect:

handle /art {        redir * /art/    }
Enter fullscreen modeExit fullscreen mode

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

The modern cloud that doesn't break the bank.

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp