Preamble
You are familiar with traditional web servers such as Nginx, Apache, … I think you should try caddyserver. You will no longer worry, tired of having to go to read documents, search through the configuration settings for the system to work smoothly. Instead, you only need 1 minute to create and configure the server with just a few lines of code for the server system to start running. It is extremely simple, and the documents are bright and easy to understand, so maybe in the near future it will be a rival of nginx.
Introduce
Caddy is a powerful web server platform, written in Go as an open-source and being developed very strongly by the community. It targets HTTP / 2.0 and uses HTTPS by default. It is also a great option for: load balancing, api port, ingress controller, process supervisor, task scheduler.
Setting
You can download it directly from github using curl:
1 2 | $ curl -OL "https://github.com/caddyserver/caddy/releases/latest/download/ASSET" |
Use wget:
1 2 | $ wget "https://github.com/caddyserver/caddy/releases/latest/download/ASSET" |
Install for Debian, Ubuntu, Raspbian
1 2 3 4 5 | echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list sudo apt update sudo apt install caddy |
Install for Fedora, RedHat, CentOS
Fedora or RHEL / CentOS 8:
1 2 3 4 | dnf install 'dnf-command(copr)' dnf copr enable @caddy/caddy dnf install caddy |
RHEL / CentOS 7:
1 2 3 4 | yum install yum-plugin-copr yum copr enable @caddy/caddy yum install caddy |
Install for macOS
1 2 | brew install caddy |
After the installation is complete you can check if the caddy is working with the command:
1 2 | caddy version |
Check the status of caddy:
1 2 | systemctl status caddy |
You can stop the caddy with the command:
1 2 | sudo systemctl stop caddy |
To get changes in the config file you can run:
1 2 | sudo systemctl reload caddy |
Note: Do not stop the service when changing the config. Stopping the service will result in system downtime. Instead, use the reload command instead.
Configuration
Caddy gives us 2 ways to configure with JSON file and Caddyfile:
With Jsonfile
First we create a json file with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <span class="token punctuation">{</span> <span class="token property">"apps"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"http"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"servers"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"example"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"listen"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">":2015"</span> <span class="token punctuation">]</span> <span class="token punctuation">,</span> <span class="token property">"routes"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token property">"handle"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token property">"handler"</span> <span class="token operator">:</span> <span class="token string">"static_response"</span> <span class="token punctuation">,</span> <span class="token property">"body"</span> <span class="token operator">:</span> <span class="token string">"Hello, world!"</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
To apply this file we need to use an api of candy:
1 2 3 4 5 | curl localhost:2019/load -X POST -H "Content-Type: application/json" -d @caddy.json |
To check if we have successfully configured or not, use the command:
1 2 3 | curl localhost:2015 Hello, world! |
if “Hello, world!” appears congratulations you have successfully configured. You can refer to JSON document. here. https://caddyserver.com/docs/json/
with Caddyfile
To configure with caddyfile you need to create a file named Caddyfile To run the “Helllo world” application as above, with Caddyfile is completely simple as follows:
1 2 3 4 | localhost tls internal respond "Hello, world!" |
As the configuration file above has localhost this is the domain name you need to configure. In addition, to configure https for Caddy Server you can quickly configure using internal tls config, Caddy will use the local certificate file for this site.
Application
Let’s say our web application has a frontend built with nuxtjs and is running at port 3000. and the backend laravel api runs at port 8000. so how to forward into this application with Caddy. Very simple you create a Caddyfile file with the following command:
1 2 3 4 5 6 7 | localhost tls internal reverse_proxy localhost:3000 route /api/* { uri strip_prefix /api reverse_proxy localhost:9000 |
We have already configured the application with: Client: localhost Server API: localhost / api /
Above are 2 common ways of configuring caddy. Try comparing through these 2 configurations. You can refer to the home page here https://caddyserver.com/docs/getting-started
Compare JSON and Caddyfile
Compare pros and cons of json and caddyfile:
Conclude
Caddy is still quite new. and being actively developed by the community. Perhaps one day not too far it will become popular. So find it today. Pro. Hello, Sincerely and win!
Refer :