78 lines
3.7 KiB
Markdown
78 lines
3.7 KiB
Markdown
# Caddy web server and reverse proxy
|
|
|
|
Usually we will have multiple services and websites running on our VPS. So we need a web server which will listen on `HTTP` and `HTTPS` ports and redirect the traffic to correct service.
|
|
|
|
## Installation
|
|
|
|
I'll install caddy as a `systemd` service on my ubuntu machine. Hence I'll following [debian docs](https://caddyserver.com/docs/install#debian-ubuntu-raspbian) to install caddy.
|
|
To verify if caddy is installed and running run the following command.
|
|
```bash
|
|
sudo service caddy status
|
|
```
|
|
Caddy web server is now started on your VPS and you can visit `domain-name.com` and see the caddy homepage. Caddy homepage will tell you where to edit caddy config and where you can put your static files for your webserver.
|
|
|
|
## TLS Certificates
|
|
|
|
Right now Caddy is running on HTTP only `80` port. To use automatic HTTPS replace the `:80` port in caddy config to your domain name.
|
|
Before that ensure DNS record of domain name points to IP address of VPS.
|
|
```diff
|
|
- :80 {
|
|
+ domain-name.com {
|
|
}
|
|
```
|
|
|
|
After making any change to caddy config we also need to restart the caddy service.
|
|
```bash
|
|
sudo systemctl reload caddy
|
|
```
|
|
Now visit `domain-name.com` again and it will be serving site over HTTPS.
|
|
Caddy will automatically provision and renew TLS certificates from [LetsEncrypt](https://letsencrypt.org)
|
|
|
|
## Redirects
|
|
|
|
You might want to setup few redirects to your root domain for like IP Address and `www` sub domain.
|
|
|
|
```Caddyfile
|
|
192.168.1.1,
|
|
www.domain-name.com {
|
|
redir https://domain-name.com{uri}
|
|
}
|
|
```
|
|
You can setup this rule for multiple domains by either writing them space seperated or comma seperated as above.
|
|
`{uri}` specified at the end of domain name preserves the path from the URL user entered.
|
|
Now if anyone visits your website via server's IP address or using `www` subdomain they will be redirected to your root domain.
|
|
**It just looks cleaner.**
|
|
|
|
## Multiple file caddy config
|
|
|
|
Caddy supports writing config in multiple files so that we don't polluting our main config. Just import all other caddy configs to the main caddy config.
|
|
Usually we can structure our config files in any way it doesn't matter. But there are some common ways to structure it.
|
|
|
|
Place all other config files inside `/etc/caddy/conf.d/` directory and import Caddyfile from this directory to main config file.
|
|
```Caddyfile
|
|
import conf.d/*.Caddyfile
|
|
```
|
|
|
|
## Handling errors
|
|
|
|
We can setup seperate error pages for different kind of error. But I'll be using single `error.html` page which is a [caddy template](https://caddyserver.com/docs/caddyfile/directives/templates) page and I can fill in error msg and error code using [placeholders](https://caddyserver.com/docs/modules/http.handlers.templates#placeholder).
|
|
For reference look at [error.html](./error.html) file.
|
|
|
|
To learn more about it check the Caddy docs **RTFM** about [handling errors](https://caddyserver.com/docs/caddyfile/directives/handle_errors).
|
|
|
|
## Reverse Proxy
|
|
|
|
**Most of the devs don't know what reverse proxy is. So maybe [check this](https://en.wikipedia.org/wiki/Reverse_proxy) out.**
|
|
|
|
Lets say we have `pokemon` API running on PORT `8080` and we want any traffic coming to subdomain `pokemon.domain-name.com` to be redirected to the pokemon server.
|
|
To achieve this we can setup reverse proxy for this subdomain.
|
|
To setup reverse proxy first we need to set `A Record` of sub-domain to the IP address of the VPS.
|
|
Then we can define the reverse proxy in our caddy config like mentioned below.
|
|
|
|
```Caddyfile
|
|
pokemon.domain-name.com {
|
|
reverse_proxy :8080
|
|
}
|
|
```
|
|
Checkout this sample reverse proxy [config](./pokemon.Caddyfile) and ofc read [caddy docs](https://caddyserver.com/docs/quick-starts/reverse-proxy) to check what else you can do with reverse proxy.
|