60000 Star! A cross platform, fast and lightweight Web server

[introduction]: a lightweight Web server written in Go.

brief introduction

Caddy is a lightweight Web server written in Go. It is an extensible server platform.

Compared with well-known Web servers such as Apache and Nginx, it provides compiled executable files and realizes real out of the box. Without any configuration, you can have free HTTPS, automatically convert Markdown files into HTML and other personalized functions. If it is to build small and medium-sized Web services, it is completely sufficient and saves time and worry.

Functional features:

  • The user-defined Caddyfile file can be used for function configuration.
  • The JSON API can be used for dynamic configuration.
  • HTTPS is automatically used by default, and Caddy is the only Web server that automatically uses HTTPS by default.
  • Highly scalable modular architecture.
  • Run independently without any external dependencies.
  • It is written in Go language with higher memory security than other server programming languages.
  • Caddy is not only a flexible and efficient static file server, but also a powerful and scalable reverse proxy.
  • As a reverse proxy, it supports active and passive health check, load balancing, open circuit, cache and other functions.

The project address is:

https://github.com/caddyserve...

Download and install

Debian, Ubuntu, raspberry pie

The system service named caddy is automatically created by using the following command:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

Fedora,RedHat,Centos

Fedora or RHEL/CentOS 8 use the following command:

dnf install 'dnf-command(copr)'
dnf copr enable @caddy/caddy
dnf install caddy

RHEL/CentOS 7 uses the following commands:

yum install yum-plugin-copr
yum copr enable @caddy/caddy
yum install caddy

Docker

docker pull caddy

Homebrew

brew install caddy

Quick start

Getting started with API

First, start the Caddy. At this time, the Caddy is idle. The default configuration is empty:

caddy start

We can dynamically add configuration for it through API:

curl localhost:2019/load \
    -X POST \
    -H "Content-Type: application/json" \
    -d @- << EOF
    {
        "apps": {
            "http": {
                "servers": {
                    "hello": {
                        "listen": [":2015"],
                        "routes": [
                            {
                                "handle": [{
                                    "handler": "static_response",
                                    "body": "Hello, world!"
                                }]
                            }
                        ]
                    }
                }
            }
        }
    }
EOF

Or save the above configuration information to the json file:

curl localhost:2019/load \
  -X POST \
  -H "Content-Type: application/json" \
  -d @caddy.json

Access the following servers:

curl localhost:2015Hello, world!

Using Caddyfile

Caddyfile is configured as follows:

localhost { respond "Hello, world!"}localhost:2016 { respond "Goodbye, world!"}

Call API for update reading:

curl localhost:2019/load \ -X POST \ -H "Content-Type: text/caddyfile" \ --data-binary @Caddyfile

Access the following servers:

curl https://localhost
Hello, world!

curl https://localhost:2016
Goodbye, world!

An example of Caddyfile is as follows:

Static file

There are two ways to publish static files in a folder that needs to be associated through Caddy:

caddy file-server

# Or specify the port
caddy file-server --listen :2015

# If you do not have an index file but want to display a list of files, use the -- browse option
caddy file-server --browse

# You can use another folder as the site root
caddy file-server --root ~/mysite

Reverse proxy

Suppose there is a backend HTTP service deployed to 127.0 0.1:9000, create a reverse proxy and execute the following command:

caddy reverse-proxy --to 127.0.0.1:9000

Or use Caddyfile for configuration:

:2016

reverse_proxy 127.0.0.1:9000

To take effect after updating Caddyfile, reload or restart Caddy.

Open source outposts share popular, interesting and practical open source projects on a daily basis. Participate in maintaining the open source technology resource library of 100000 + Star, including Python, Java, C/C + +, Go, JS, CSS and node js,PHP,. NET, etc.

Keywords: IDE

Added by bucko on Sun, 02 Jan 2022 23:56:23 +0200