DEV Community

Cover image for Configuring Wildcard Subdomains: A Comparison of Nginx and Caddy
Russell 👨🏾‍💻
Russell 👨🏾‍💻

Posted on

3

Configuring Wildcard Subdomains: A Comparison of Nginx and Caddy

Introduction

Wildcard subdomains allow you to dynamically handle multiple subdomains under a parent domain without explicitly defining the configuration for each subdomain.

An illustration of a multitenant application with subdomains

A common use case for wildcard subdomains is in multitenant applications where each customer accesses the application from their unique domain. Manually configuring each subdomain is impractical in this scenario.

In this article, we will look at configuring wildcard subdomains on Nginx and Caddy and compare both options.

Requirements

  • The article assumes you have Nginx and Caddy installed

Nginx Configuration

To get started, we'll create a new config file. You need root access to update the configuration so we will prefix the command with sudo



sudo nano /etc/nginx/sites-available/app.com


Enter fullscreen mode Exit fullscreen mode

Add the following configuration to the file



server {

    server_name *.app.com;

    location / {
        proxy_pass http://127.0.0.1:8090;
        proxy_set_header Host $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;
    }
}



Enter fullscreen mode Exit fullscreen mode

This configuration proxies all requests to a service running on port 8090. The key bit is server_name *.app.com;, this tells nginx to handle all requests made to *.app.com (customer1.app.com, customer2.app.com ...).

Caddy Configuration

Just like Nginx, we'll begin by editing the main configuration file and prefixing the call with sudo since it requires root access:



sudo nano /etc/caddy/Caddyfile


Enter fullscreen mode Exit fullscreen mode

And the contents:



*.app.com {
request_body {
max_size 10MB
}
reverse_proxy 127.0.0.1:8090 {
transport http {
read_timeout 360s
}
}
}

Enter fullscreen mode Exit fullscreen mode




Comparison and Conclusion

Both Nginx and Caddy have similar configuration patterns they both have distinct approaches so the syntax differs greatly. They however both provide nearly the same levels of flexibility and functionality with Caddy being more akin to JSON and supporting it natively.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay