Comparing Caddy to Nginx and Apache
Tags: frontend, backend, opensource, devops
Published: 2023-09-26 12:43:55

As the complexity of modern Web applications increases, choosing a suitable Web server is not just a technical choice, but also a matter of performance, security, and maintenance costs. Caddy, Nginx, and Apache are all excellent choices, but what makes them different?

## What is Caddy?

Caddy is an open-source HTTP/2 web server written in Go language. Its main advantages are automatic support for HTTPS and simple configuration.

```bash
your-domain.com {
    reverse_proxy /api/* 127.0.0.1:8080
}
```

### Advanced Configuration of Caddy

From automatic HTTPS to static file serving, Caddy provides a wealth of features, such as:


```bash
header / {
    X-Custom-Header "Some value"
}
```

### Other Configurations of Caddy

Caddy provides a very rich set of configuration options to meet various needs. The following are some commonly used configurations in Caddyfile:

**Compression:** Use the `encode` directive to enable content encoding for responses.

```bash
encode gzip zstd
```

**Logging:** `log` directives allow you to configure the output and format of logs.

```bash
log {
    output file /path/to/logfile.log
    format json
}
```
**Request rewrite:** `rewrite` Directive can change the properties of the request.

```bash
rewrite /api/* /new-api/*
```

**Request and Response Headers:** You can use the `header` directive to add, remove, or modify HTTP headers.

```bash
header / {
    X-Custom-Header "Some value"
    -Server
}
```

Rate Limiting: limitDirectives can rate limit requests.

```bash
limit rate 10r/s burst 5
```

**Basic Authentication:** Use `basicauth` to enable Basic HTTP authentication for a specific path.

```bash
basicauth /protected/* {
    user1 hashed_password1
    user2 hashed_password2
}
```

**Cross-Origin Resource Sharing (CORS):** CORS headers can be manually configured using third-party plugins or `header` directives.

**Health checks:** When using load balancing, health checks can be configured to determine which backends are healthy.

```bash
reverse_proxy / {
    to backend1:8080 backend2:8080
    health_path /healthcheck
    health_interval 30s
}
```

**Static File Service:** file_serverThe directive can make Caddy act as a static file server.

```bash
file_server
```

**Custom Error Pages:** Use handle_errorsto define how errors are handled.

```bash
handle_errors {
    rewrite * /error{http.error.status_code}.html
    file_server
}
```

Caddy is a scalable and flexible web server that can be deeply customized to meet your needs. If you need more features or configuration options, please consult [Caddy's official documentation.](https://caddyserver.com/docs/)

### How to configure Caddy as a reverse proxy and load balancer for a Spring Boot project

To configure load balancing and reverse proxy for your Spring Boot project using Caddy, you can follow these steps:

#### Install Caddy

First, make sure you have Caddy installed. If it is not installed yet, please refer to the official documentation to [install](https://caddyserver.com/docs/install) the appropriate version.

#### Create Caddyfile

Caddyfile is the configuration file of Caddy. Create a file named `Caddyfile` in the directory where you plan to run Caddy.

#### Configure Caddyfile

The following is an example configuration of a Caddyfile to provide load balancing and reverse proxy for two Spring Boot instances:

```bash
your-domain.com {
    reverse_proxy / {
        lb_policy round_robin
        to 127.0.0.1:8080 127.0.0.1:8081
    }
}
```

Here is an explanation of the configuration:

`your-domain.com:` Replace this with your domain name.
`reverse_proxy /:` Configure Caddy as a reverse proxy.
`lb_policy round_robin:` Set the load balancing policy to round-robin.
`to 127.0.0.1:8080 127.0.0.1:8081:` Specify the address and port of the backend Spring Boot instance. In this example, we have two instances running on `8080` and `8081` ports.

#### Start Caddy

```bash
caddy run
```
This will start Caddy, which will provide load balancing and reverse proxy for your Spring Boot project based on the configuration of the Caddyfile.

#### Precaution

Make sure that your Spring Boot project instance is indeed running on the configured port and is accessible from the Caddy server. If your Spring Boot project and Caddy are on different machines, you need to ensure that the port and IP address are configured correctly.

Additionally, if you are using Caddy's automatic HTTPS feature, make sure that the domain name correctly points to the IP address of the Caddy server and that the DNS settings are configured correctly.

## Nginx and Apache: Tradition and Strength

Nginx is a high-performance HTTP and reverse proxy server. Its configuration method is declarative and very flexible.

```bash
location / {
    proxy_pass http://localhost:8080;
}
```

### Apache

Apache is a long-established web server that supports rich modules and .htaccess configurations.

```bash
<VirtualHost *:80>
    ProxyPass /app/ http://localhost:8080/
</VirtualHost>
```

## Caddy vs Nginx vs Apache

I have listed the main features and functions of these three servers through a comparison table, from development languages ​​to cross-platform capabilities, to provide you with a comprehensive perspective.

The following is a comparison table of Caddy, Nginx, and Apache, listing some of their key features and functions:

| Features/Functions | Caddy | Nginx | Apache |
|---|---|---|---|
| Development language | Go | C | C |
| License | Apache 2.0 | 2-Clause BSD-like | Apache 2.0 |
| Automatic HTTPS | Yes (supported by default) | Requires additional modules and configuration | Requires additional modules and configuration |
| HTTP/2 & HTTP/3 | Support | Support | Supported (additional configuration required) |
| Configuration method | Caddyfile | nginx.conf | .htaccess and httpd.conf |
| Reverse proxy | Native support | Native support | Requires mod_proxy module |
| Load balancing | Native support | Native support | Requires mod_proxy_balancer module |
| Module/plugin system | Support (dynamic loading) | Supported (usually compiled statically) | Support (dynamic loading) |
| Performance | High (especially in default configuration) | High | Moderate (but can be optimized) |
| Safety | Designed to be secure (default HTTPS) | Safe, but configuration needs to be taken care of | Safe, but need to pay attention to configuration and modules |
| Beginner friendly | High (automatic HTTPS, simple configuration) | Moderate (configuration is slightly complex) | Low (configuration and module management are more complex) |
| Cross-platform | Yes | Yes | Yes |

This table is a simplified comparison, focusing primarily on general functionality and key features. Each server has unique features and advantages, and your choice depends on your specific needs and preferences. For example, while Caddy's automatic HTTPS and simple configuration are very popular with beginners, Nginx and Apache may have advantages in some complex configurations and large deployment scenarios.

## Conclusion

Choosing a web server depends not only on technical requirements but also on long-term maintenance and learning costs. Caddy's simplicity and automatic HTTPS capabilities make it popular in modern web development. But Nginx and Apache also have their unique advantages and long-term stability.