Nginx & Caddy 重定向配置指南 🌐

🔀 本文详细介绍如何使用 Nginx 和 Caddy 服务器实现高效的重定向配置,包括 HTTPS 支持和错误处理。
📋 目录导航
✨ 核心功能特点
- 🔁 灵活重定向:支持多种重定向方式(301永久/302临时)
- 🔒 SSL加密:自动处理HTTPS证书和加密连接
- ⚡ 高性能:轻量级配置,高效处理请求转发
- 🛡️ 错误处理:完善的错误页面和故障恢复机制
- 📁 模块化配置:支持配置文件分离,便于管理维护
🔄 一、Nginx 重定向配置
1. 📝 基础重定向配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| touch /etc/nginx/conf.d/123.conf && cat > /etc/nginx/conf.d/123.conf <<'EOF' server { listen 5553 ssl;
server_name 123.mobufan.eu.org;
ssl_certificate /etc/nginx/keyfile/cert.pem; ssl_certificate_key /etc/nginx/keyfile/key.pem;
return 301 https://404.mobufan.eu.org:5553$request_uri; } EOF
|
2. ⚡ 配置生效命令
1 2 3 4 5 6 7 8
| nginx -t
nginx -s reload
systemctl reload nginx
|
3. 🔧 高级重定向选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server { listen 80; server_name example.com; return 301 https://$server_name$request_uri; }
server { listen 443 ssl; server_name example.com; return 301 https://newdomain.com$request_uri; location /old-path { return 301 https://$server_name/new-path; } }
|
🚀 二、Caddy 重定向配置
1. 📝 基础重定向配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| mkdir -pm 755 /usr/local/caddy/conf.d && \ touch /usr/local/caddy/conf.d/baidu.conf && \ chmod u+x /usr/local/caddy/conf.d/baidu.conf && \ cat > /usr/local/caddy/conf.d/baidu.conf <<'EOF'
https://baidu.meimolihan.eu.org:6663 { encode gzip tls /usr/local/caddy/ssl/full_chain.pem /usr/local/caddy/ssl/private.key redir https://baidu.com{uri} handle_errors { rewrite * /50x.html root * /var/www/html file_server } } EOF
|
2. ⚡ 配置生效命令
1 2 3 4 5 6 7 8
| cd /usr/local/caddy && ./caddy fmt --overwrite
cd /usr/local/caddy && ./caddy reload
systemctl reload caddy
|
3. 🔧 高级重定向选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| https://site1.example.com:6663, https://site2.example.com:6663 { redir https://newdomain.com{uri} permanent }
https://example.com:6663 { @old { path /old/* } redir @old https://example.com/new{path} permanent }
https://temp.example.com:6663 { redir https://othersite.com{uri} temporary }
|
⚙️ 三、SSL证书配置
1. 📁 证书文件管理
1 2 3 4 5 6 7
| mkdir -p /etc/nginx/keyfile chmod 700 /etc/nginx/keyfile
mkdir -p /usr/local/caddy/ssl chmod 700 /usr/local/caddy/ssl
|
2. 🔐 证书权限设置
1 2 3 4 5 6 7
| chmod 600 /etc/nginx/keyfile/cert.pem chmod 600 /etc/nginx/keyfile/key.pem
chmod 600 /usr/local/caddy/ssl/full_chain.pem chmod 600 /usr/local/caddy/ssl/private.key
|
3. 📜 证书自动续期
1 2 3 4 5
| certbot renew --nginx --quiet
|
🔧 四、错误处理配置
1. 🚨 Nginx错误页面配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 5553 ssl; server_name 123.mobufan.eu.org; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /404.html { root /var/www/html; internal; } location = /50x.html { root /var/www/html; internal; } }
|
2. 🛡️ Caddy错误处理
1 2 3 4 5 6 7 8 9 10
| https://example.com:6663 { handle_errors { @404 { expression {http.error.status_code} == 404 } rewrite @404 /404.html file_server } }
|
3. 📋 错误日志配置
1 2 3 4 5
| tail -f /var/log/nginx/error.log
journalctl -u caddy -f
|
📊 五、性能优化
1. ⚡ Nginx性能优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { listen 5553 ssl; keepalive_timeout 65; keepalive_requests 100; sendfile on; tcp_nopush on; tcp_nodelay on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; }
|
2. 🚀 Caddy性能优化
1 2 3 4 5 6 7 8
| https://example.com:6663 { encode zstd gzip header { Cache-Control "public, max-age=3600" } }
|
3. 📈 监控和调优
1 2 3 4 5 6 7 8
| nginx -T
curl localhost:2019/metrics
ab -n 1000 -c 100 https://example.com:6663/
|
⚠️ 六、注意事项
1. 🔒 安全注意事项
1 2 3 4 5 6 7 8 9 10
| certbot renew --dry-run
find /etc/nginx/ -name "*.conf" -exec chmod 644 {} \; find /usr/local/caddy/conf.d/ -name "*.conf" -exec chmod 644 {} \;
ufw allow 5553/tcp ufw allow 6663/tcp
|
2. 🛠️ 故障排除
1 2 3 4 5 6 7 8 9 10 11 12
| nginx -t
caddy validate
systemctl status nginx systemctl status caddy
netstat -tulnp | grep -E '(5553|6663)'
|
3. 📋 最佳实践
- ✅ 总是使用HTTPS重定向
- ✅ 配置合适的HTTP状态码(301/302)
- ✅ 设置正确的错误处理页面
- ✅ 定期备份配置文件
- ✅ 监控重定向性能
- ✅ 测试重定向逻辑
4. 🆘 常见问题解决
1 2 3 4 5 6 7 8 9
| openssl verify -CAfile /path/to/ca-bundle.pem /path/to/certificate.pem
curl -I -L http://example.com
dig example.com nslookup example.com
|
🎯 配置完成检查清单
- [ ] Nginx/Caddy 配置文件已创建
- [ ] SSL 证书路径正确配置
- [ ] 重定向逻辑测试通过
- [ ] 错误页面配置完成
- [ ] 防火墙端口已开放
- [ ] 服务重启并生效
- [ ] 重定向功能测试正常
💡 提示:配置完成后建议使用以下命令测试重定向:
1 2
| curl -I https://123.mobufan.eu.org:5553 curl -I https://baidu.meimolihan.eu.org:6663
|
通过以上配置,您可以实现高效、安全的Web重定向服务,确保用户访问体验和系统稳定性! 🌐🔀