docs: New common uses page for Caddyfile

This commit is contained in:
Matthew Holt 2020-05-16 12:49:03 -06:00
parent 5f1bd6374f
commit 10071ee555
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,122 @@
---
title: Common Caddyfile Uses
---
# Common Caddyfile Uses
This page presents some complete, minimal Caddyfile configurations for common use cases, which you might find helpful when building your own configs.
These are not drop-in solutions; you will have to customize your domain name, ports/sockets, directory paths, etc. They are intended to illustrate only the basic requirements for the most common use cases.
#### Menu
- [Static file server](#static-file-server)
- [Reverse proxy](#reverse-proxy)
- [PHP](#php)
- [Redirect `www.` subdomain](#redirect-www-subdomain)
- [Trailing slashes](#trailing-slashes)
## Static file server
```
example.com
root * /var/www # optional; default root is current directory
file_server
```
## Reverse proxy
All requests:
```
example.com
reverse_proxy localhost:5000
```
Just requests having a path starting with `/api/`; static files for everything else:
```
example.com
reverse_proxy /api/* localhost:5000
root * /var/www # optional; default root is current directory
file_server
```
## PHP
With FastCGI running, something like this works for most modern PHP apps:
```
example.com
php_fastcgi unix//run/php/php-fpm.sock
```
If your PHP site relies on static files too, you may need to enable a static file server (but this depends on your PHP app):
```
example.com
php_fastcgi /blog/* localhost:9000
root * /var/www # optional; default root is current directory
file_server
```
## Redirect `www.` subdomain
To **add** the `www.` subdomain with an HTTP redirect:
```
example.com {
redir https://www.example.com{uri}
}
www.example.com {
...
}
```
To **remove** it:
```
www.example.com {
redir https://example.com{uri}
}
example.com {
...
}
```
## Trailing slashes
You will not usually need to configure this yourself; the [`file_server` directive](/docs/caddyfile/directives/file_server) will automatically add or remove trailing slashes from requests by way of HTTP redirects, depending on whether the requested resource is a directory or file, respectively.
HTTP redirects are external, but you can internally [`rewrite`](/docs/caddyfile/directives/rewrite) add the slash if you want both URIs to be used for the same resource.
To add or remove a trailing slash:
```
example.com
rewrite /add /add/
rewrite /remove/ /remove
```
To perform the equivalent change externally (with a redirect), simply replaces `rewrite` with [`redir`](/docs/caddyfile/directives/redir):
```
example.com
redir /add /add/
redir /remove/ /remove
```

View file

@ -32,6 +32,7 @@
<li><a href="/docs/caddyfile/directives">Directives</a></li>
<li><a href="/docs/caddyfile/matchers">Request matchers</a></li>
<li><a href="/docs/caddyfile/options">Global options</a></li>
<li><a href="/docs/caddyfile/common-uses">Common uses</a></li>
</ul>
</li>
<li><a href="/docs/modules/">Modules</a></li>