How to use Nginx web server caddy
May 30, 2023 pm 12:19 PMIntroduction to Caddy
Caddy is a powerful, highly scalable web server that has currently gained 38K Stars on GitHub. Caddy is written in Go language and can be used for static resource hosting and reverse proxy.
Caddy has the following main features:
Compared with Nginx’s complex configuration, its original Caddyfile configuration is very simple;
The configuration can be dynamically modified through the Admin API it provides;
supports automated HTTPS configuration by default, and can automatically apply for HTTPS certificates and configure them;
Can be expanded to tens of thousands of sites;
can be executed anywhere, with no additional dependencies;
is written in Go language , memory security is more guaranteed.
Installation
First of all, we install Caddy directly on CentOS 8. Installation using the DNF tool is undoubtedly the simplest. The Docker installation method will be introduced later.
Use the following command to install Caddy through the DNF tool. After successful installation, Caddy will be registered as a system service;
dnf install 'dnf-command(copr)' dnf copr enable @caddy/caddy dnf install caddy
Use systemctl status caddy to check the status of Caddy. You can find that Caddy has been registered as a system service. service, but it is not enabled yet.
Usage
Let’s experience the basic use of Caddy. They are common operations for web servers. You can definitely use them!
Basic usage
Let us try to use Caddy to get started, specifying that Caddy runs on the 2015 port and returns the "Hello, world!" message.
Using the caddy command directly will output the common commands of Caddy. Basically, you will know how to use it by reading the introduction. The common commands are marked;
Use caddy The start command allows the Caddy service to run in the background;
Caddy uses the JSON format configuration file by default. However, since it is more troublesome to write the JSON format configuration, Caddyfile is also provided. Simple configuration form, use the following command to automatically convert Caddyfile into JSON configuration;
caddy adapter
We can first create a file named Caddyfile with the following content, and then use caddy adapter to convert it into JSON configuration, Then use caddy reload to make the configuration effective. The configuration will listen to the 2015 port and return Hello, world!;
:2015 respond "Hello, world!"
Then we use the curl command to access localhost:2015 and the specified information will be returned;
Of course we can also use the Admin API provided by Caddy to view the configuration information, just use the following command;
curl localhost:2019/config/
The current JSON configuration is as follows, if you use JSON configuration directly You need to write the following configuration, using Caddyfile is indeed much more convenient!
{ "apps": { "http": { "servers": { "srv0": { "listen": [":2015"], "routes": [{ "handle": [{ "body": "Hello, world!", "handler": "static_response" }] }] } } } } }
Basic syntax of Caddyfile
The following case will use Caddyfile for configuration. We need to understand its syntax. The specific syntax rules of Caddyfile are as follows.
Introduce the keywords in the above picture to help understanding.
Keyword | Explanation | Use |
---|---|---|
Global options block | Server global configuration | Can be used to configure whether to enable HTTPS and Admin API, etc. |
Snippet | Reusable configuration snippets | After definition, it can be referenced through the import keyword |
Site Block | Single website configuration | Static proxy can be configured through file_server and reverse_proxy Dynamic agents can be configured |
Matcher definition | Match definition | By default the directive will have a global impact, through which the scope of influence can be specified |
Comment | Comment | Use the # symbol starting with |
Site address | Website address | HTTPS is used by default. If you need to enable HTTP, you need to specify the |
Directive | directive | directive given at the beginning of http:// Caddy’s powerful features |
反向代理
反向代理就是當(dāng)請(qǐng)求訪問你的代理服務(wù)器時(shí),代理服務(wù)器會(huì)對(duì)你的請(qǐng)求進(jìn)行轉(zhuǎn)發(fā),可以轉(zhuǎn)發(fā)到靜態(tài)的資源路徑上去,也可以轉(zhuǎn)發(fā)到動(dòng)態(tài)的服務(wù)接口上去。我們以代理域名為例,講解如何進(jìn)行靜態(tài)和動(dòng)態(tài)代理。
靜態(tài)代理
靜態(tài)代理就是將請(qǐng)求代理到不同的靜態(tài)資源路徑上去,這里我們將對(duì)docs.macrozheng.com的請(qǐng)求代理到我的文檔項(xiàng)目中,對(duì)mall.macrozheng.com的請(qǐng)求代理到mall的前端項(xiàng)目中。
首先我們修改下本機(jī)的host文件:
192.168.3.106 docs.macrozheng.com
192.168.3.106 mall.macrozheng.com
然后將我們的文檔項(xiàng)目和mall前端項(xiàng)目上傳到Caddy的html目錄中去,并進(jìn)行解壓操作:
修改Caddyfile文件,使用如下配置,修改完成后使用caddy reload命令刷新配置;
http://docs.macrozheng.com { root * /mydata/caddy/html/docs file_server browse } http://mall.macrozheng.com { root * /mydata/caddy/html/mall file_server browse }
如果你的Caddyfile文件格式不太合格的話,會(huì)出現(xiàn)如下警告,直接使用caddy fmt --overwrite格式化并重寫配置即可解決;
通過docs.macrozheng.com即可訪問部署好的文檔項(xiàng)目了:
通過mall.macrozheng.com即可訪問到部署好的前端項(xiàng)目了。
動(dòng)態(tài)代理
動(dòng)態(tài)代理就是把代理服務(wù)器的請(qǐng)求轉(zhuǎn)發(fā)到另一個(gè)服務(wù)上去,這里我們將把對(duì)api.macrozheng.com的請(qǐng)求代理到演示環(huán)境的API服務(wù)上去。
首先我們修改下本機(jī)的host文件,添加如下規(guī)則
192.168.3.106 api.macrozheng.com
修改Caddyfile文件,使用如下配置,修改完成后使用caddy reload命令刷新配置;
http://api.macrozheng.com { reverse_proxy http://admin-api.macrozheng.com }
之后通過api.macrozheng.com/swagger-ui.html即可訪問到mall-admin的API文檔頁面了。
文件壓縮
如果我們的服務(wù)器帶寬比較低,網(wǎng)站訪問速度會(huì)很慢,這時(shí)我們可以通過讓Caddy開啟Gzip壓縮來提高網(wǎng)站的訪問速度。這里我們以mall的前端項(xiàng)目為例來演示下它的提速效果。
我們需要修改Caddyfile文件,使用encode指令開啟Gzip壓縮,修改完成后使用caddy reload命令刷新配置;
http://mall.macrozheng.com { root * /mydata/caddy/html/mall encode { gzip } file_server browse }
有個(gè)比較大的JS文件壓縮前是1.7M;
壓縮后為544K,訪問速度也有很大提示;
另外我們可以看下響應(yīng)信息,如果有Content-Encoding: gzip這個(gè)響應(yīng)頭表明Gzip壓縮已經(jīng)啟用了。
地址重寫
有的時(shí)候我們的網(wǎng)站更換了域名,但還有用戶在使用老的域名訪問,這時(shí)可以通過Caddy的地址重寫功能來讓用戶跳轉(zhuǎn)到新的域名進(jìn)行訪問。
我們需要修改Caddyfile文件,使用redir指令重寫地址,修改完成后使用caddy reload命令刷新配置;
http://docs.macrozheng.com { redir http://www.macrozheng.com }
此時(shí)訪問舊域名docs.macrozheng.com會(huì)直接跳轉(zhuǎn)到www.macrozheng.com去。
按目錄劃分
有時(shí)候我們需要使用同一個(gè)域名來訪問不同的前端項(xiàng)目,這時(shí)候就需要通過子目錄來區(qū)分前端項(xiàng)目了。
比如說我們需要按以下路徑來訪問各個(gè)前端項(xiàng)目;
www.macrozheng.com #訪問文檔項(xiàng)目
www.macrozheng.com/admin #訪問后臺(tái)項(xiàng)目
www.macrozheng.com/app #訪問移動(dòng)端項(xiàng)目
我們需要修改Caddyfile文件,使用route指令定義路由,修改完成后使用caddy reload命令刷新配置。
http://www.macrozheng.com { route /admin/* { uri strip_prefix /admin file_server { root /mydata/caddy/html/admin } } route /app/* { uri strip_prefix /app file_server { root /mydata/caddy/html/app } } file_server * { root /mydata/caddy/html/www } }
HTTPS
Caddy能自動(dòng)支持HTTPS,無需手動(dòng)配置證書,這就是之前我們?cè)谂渲糜蛎麜r(shí)需要使用http://開頭的原因,要想使用Caddy默認(rèn)的HTTPS功能,按如下步驟操作即可。
首先我們需要修改域名的DNS解析,直接在購買域名的網(wǎng)站上設(shè)置即可,這里以docs.macrozheng.com域名為例;
請(qǐng)使用以下命令確認(rèn)DNS解析記錄是否正確,注意所配置的服務(wù)器的80和443端口需要在外網(wǎng)中可以正常訪問:
curl "https://cloudflare-dns.com/dns-query?name=docs.macrozheng.com&type=A" \ -H "accept: application/dns-json"
修改Caddyfile配置文件,進(jìn)行如下配置;
docs.macrozheng.com { root * /mydata/caddy/html/docs file_server browse }
然后使用caddy run命令啟動(dòng)Caddy服務(wù)器即可,是不是非常方便!
caddy run
Docker支持
當(dāng)然Caddy也是支持使用Docker進(jìn)行安裝使用的,其使用和直接在CentOS上安裝基本一致。
首先使用如下命令下載Caddy的Docker鏡像;
docker pull caddy
然后在/mydata/caddy/目錄下創(chuàng)建Caddyfile配置文件,文件內(nèi)容如下;
http://192.168.3.105:80 respond "Hello, world!"
之后使用如下命令啟動(dòng)caddy服務(wù),這里將宿主機(jī)上的Caddyfile配置文件、Caddy的數(shù)據(jù)目錄和網(wǎng)站目錄掛載到了容器中;
docker run -p 80:80 -p 443:443 --name caddy \ -v /mydata/caddy/Caddyfile:/etc/caddy/Caddyfile \ -v /mydata/caddy/data:/data \ -v /mydata/caddy/html:/usr/share/caddy \ -d caddy
之后使用docker exec進(jìn)入caddy容器內(nèi)部執(zhí)行命令;
docker exec -it caddy /bin/sh
輸入Caddy命令即可操作,之后的操作就和我們直接在CentOS上安裝一樣了。
The above is the detailed content of How to use Nginx web server caddy. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

Practical Tips for Improving PhpStorm Performance in CentOS Systems This article provides a variety of methods to help you optimize the performance of PhpStorm in CentOS systems and thus improve development efficiency. Before implementing any optimization measures, be sure to back up important data and verify the results in the test environment. 1. System-level optimization and streamline system services: Disable unnecessary system services and daemons to reduce system resource usage. Interfaceless Mode: Switching to interfaceless mode can significantly save resources if you do not need a graphical interface. Uninstall redundant software: Remove software packages and services that are no longer in use and free up system resources. 2. PHP configuration optimization enable OPcache: install and configure OPcache extensions to display

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.
