河北网站建设收益自己的网站中商城怎么做
2026/4/16 20:46:04 网站建设 项目流程
河北网站建设收益,自己的网站中商城怎么做,建设官网站,网站设计论文题目参考前言 Caddy是一款用Go编写的现代Web服务器#xff0c;最大特点是自动HTTPS——只需配置域名#xff0c;Caddy会自动申请和续期Let’s Encrypt证书。本文将带你快速上手Caddy。 一、Caddy vs Nginx 特性CaddyNginx配置语法简洁易读相对复杂自动HTTPS✅ 开箱即用❌ 需certbo…前言Caddy是一款用Go编写的现代Web服务器最大特点是自动HTTPS——只需配置域名Caddy会自动申请和续期Let’s Encrypt证书。本文将带你快速上手Caddy。一、Caddy vs Nginx特性CaddyNginx配置语法简洁易读相对复杂自动HTTPS✅ 开箱即用❌ 需certbot配置热更新✅ API支持❌ 需reload性能优秀极致内存占用较高较低扩展方式插件/编译模块二、快速安装2.1 Linux安装# Ubuntu/Debiansudoaptinstall-y debian-keyring debian-archive-keyring apt-transport-httpscurl-1sLfhttps://dl.cloudsmith.io/public/caddy/stable/gpg.key|sudogpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpgcurl-1sLfhttps://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt|sudotee/etc/apt/sources.list.d/caddy-stable.listsudoaptupdatesudoaptinstallcaddy# CentOS/RHELyuminstallyum-plugin-copr yum coprenablecaddy/caddy yuminstallcaddy2.2 Docker部署# docker-compose.ymlversion:3.8services:caddy:image:caddy:2-alpinecontainer_name:caddyports:-80:80-443:443volumes:-./Caddyfile:/etc/caddy/Caddyfile-./site:/srv-caddy_data:/data-caddy_config:/configrestart:unless-stoppedvolumes:caddy_data:caddy_config:三、Caddyfile配置3.1 基础静态站点# Caddyfile example.com { root * /srv/www file_server } # 就这么简单Caddy会自动申请HTTPS证书3.2 反向代理# 代理到后端服务 api.example.com { reverse_proxy localhost:8080 } # 带路径的代理 example.com { reverse_proxy /api/* localhost:8080 reverse_proxy /ws/* localhost:9000 root * /srv/www file_server }3.3 负载均衡example.com { reverse_proxy { to localhost:8001 to localhost:8002 to localhost:8003 lb_policy round_robin health_uri /health health_interval 10s } }3.4 多站点配置# 站点1 app1.example.com { reverse_proxy localhost:3001 } # 站点2 app2.example.com { reverse_proxy localhost:3002 } # 站点3 - 静态文件 static.example.com { root * /srv/static file_server browse # 启用目录浏览 }四、常用功能4.1 Gzip压缩example.com { encode gzip zstd root * /srv/www file_server }4.2 访问日志example.com { log { output file /var/log/caddy/access.log { roll_size 100mb roll_keep 10 } format json } root * /srv/www file_server }4.3 Basic认证admin.example.com { basicauth { admin $2a$14$xxxx # caddy hash-password生成 } reverse_proxy localhost:8080 }生成密码哈希caddy hash-password# 输入密码获取哈希值4.4 IP限制internal.example.com { allowed { remote_ip 192.168.0.0/16 10.0.0.0/8 } handle allowed { reverse_proxy localhost:8080 } respond Forbidden 403 }4.5 重定向# HTTP重定向到HTTPS默认自动 http://example.com { redir https://{host}{uri} permanent } # 路径重定向 example.com { redir /old-path /new-path permanent root * /srv/www file_server }4.6 请求头设置example.com { header { # 安全头 Strict-Transport-Security max-age31536000; includeSubDomains X-Content-Type-Options nosniff X-Frame-Options DENY # 移除Server头 -Server } reverse_proxy localhost:8080 }五、高级配置5.1 自定义证书# 使用自己的证书 example.com { tls /path/to/cert.pem /path/to/key.pem reverse_proxy localhost:8080 } # 内网环境自签名证书 internal.local { tls internal reverse_proxy localhost:8080 }5.2 WebSocket代理example.com { reverse_proxy /ws/* localhost:9000 { # WebSocket自动支持 } reverse_proxy localhost:8080 }5.3 PHP支持example.com { root * /srv/www php_fastcgi unix//run/php/php8.2-fpm.sock file_server }5.4 SPA应用# React/Vue单页应用 example.com { root * /srv/app try_files {path} /index.html file_server }六、API管理6.1 启用Admin API{ admin localhost:2019 } example.com { reverse_proxy localhost:8080 }6.2 动态配置# 查看当前配置curlhttp://localhost:2019/config/# 更新配置curl-X POST http://localhost:2019/load\-HContent-Type: application/json\-d config.json# 添加路由curl-X POST http://localhost:2019/config/apps/http/servers/srv0/routes\-HContent-Type: application/json\-d{handle:[{handler:static_response,body:Hello}]}七、代理内网服务7.1 场景当需要代理没有公网IP的内网服务时可以使用组网软件如星空组网将服务器组成虚拟局域网# Caddyfile # 代理内网NAS nas.example.com { reverse_proxy 10.26.0.10:5000 # NAS的虚拟内网IP } # 代理内网GitLab git.example.com { reverse_proxy 10.26.0.20:80 # GitLab的虚拟内网IP }7.2 通配符证书*.example.com { tls { dns cloudflare {env.CF_API_TOKEN} } nas host nas.example.com handle nas { reverse_proxy 10.26.0.10:5000 } git host git.example.com handle git { reverse_proxy 10.26.0.20:80 } }八、监控与日志8.1 Prometheus指标{ servers { metrics } } example.com { # ... } # 访问 :2019/metrics 获取指标8.2 结构化日志example.com { log { output file /var/log/caddy/access.log format json level INFO } }九、常见问题9.1 证书申请失败# 检查80/443端口是否开放sudolsof-i :80sudolsof-i :443# 检查域名解析digexample.com# 查看Caddy日志journalctl -u caddy -f9.2 配置验证# 验证配置文件caddy validate --config /etc/caddy/Caddyfile# 格式化配置caddyfmt--overwrite /etc/caddy/Caddyfile9.3 重载配置# systemd方式sudosystemctl reload caddy# API方式caddy reload --config /etc/caddy/Caddyfile十、生产配置示例# /etc/caddy/Caddyfile # 全局配置 { email adminexample.com admin localhost:2019 servers { protocol { experimental_http3 } } } # 主站 www.example.com, example.com { encode gzip zstd header { Strict-Transport-Security max-age31536000 X-Content-Type-Options nosniff X-Frame-Options SAMEORIGIN } log { output file /var/log/caddy/www.log { roll_size 100mb roll_keep 5 } } root * /srv/www file_server } # API服务 api.example.com { reverse_proxy { to localhost:8001 to localhost:8002 lb_policy least_conn health_uri /health health_interval 10s } log { output file /var/log/caddy/api.log } }十一、总结Caddy是一款现代化的Web服务器核心优势特点说明自动HTTPS无需手动配置证书配置简洁Caddyfile语法直观热更新配置变更无需重启功能齐全反向代理、负载均衡、压缩等适用场景个人项目/小型网站需要快速部署HTTPS的场景不想折腾证书配置不适用场景对性能极致要求选Nginx需要复杂模块选OpenResty参考资料Caddy官方文档https://caddyserver.com/docs/Caddyfile语法https://caddyserver.com/docs/caddyfileCaddy GitHubhttps://github.com/caddyserver/caddy本文首发于CSDN转载请注明出处。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询