Nginx & Caddy 错误页面拦截配置指南 🛡️

🚨 本文详细介绍如何配置 Nginx 和 Caddy 服务器的错误页面拦截功能,提供用户友好的错误提示,提升网站用户体验。
📋 目录导航
✨ 核心功能特点
- 🎯 错误拦截:自动捕获和处理各种HTTP错误状态码
- 🎨 自定义页面:支持完全自定义的错误页面设计
- 🔧 灵活配置:可根据不同错误类型设置不同处理方式
- ⚡ 高性能:轻量级错误处理机制,不影响正常请求
- 📱 响应式设计:错误页面适配各种设备屏幕
🛠️ 一、Nginx 错误页面配置
1. 📝 完整配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| server { listen 5553 ssl; listen [::]:5553 spcsl;
server_name xunlei.mobufan.eu.org;
ssl_certificate /etc/nginx/keyfile/cert.pem; ssl_certificate_key /etc/nginx/keyfile/key.pem; location / { proxy_pass http://10.10.10.245:2345; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Range $http_range; proxy_set_header If-Range $http_if_range; proxy_redirect off; proxy_buffering on; proxy_http_version 1.1; client_max_body_size 20000m; } charset utf-8; error_page 404 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; internal; default_type text/html; proxy_no_cache 1; proxy_cache_bypass 1; } }
|
2. 🔧 错误处理部分详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| charset utf-8; error_page 404 /404.html; error_page 500 502 503 504 /50x.html;
location = /404.html { root /var/www/html; internal; add_header Cache-Control "no-cache, no-store, must-revalidate"; }
location = /50x.html { root /var/www/html; internal; add_header Access-Control-Allow-Origin "*"; }
|
3. ⚡ 配置生效命令
1 2 3 4 5 6 7 8
| sudo nginx -t
sudo nginx -s reload
sudo systemctl reload nginx
|
🚀 二、Caddy 错误页面配置
1. 📝 完整配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| https://xunlei.meimolihan.eu.org:6663 { encode gzip tls ssl/full_chain.pem ssl/private.key reverse_proxy http://10.10.10.245:2345 { header_up Host {http.reverse_proxy.upstream.hostport} header_up X-Real-IP {remote_host} } handle_errors { @404 expression {http.error.status_code} == 404 @50x expression {http.error.status_code} >= 500 && {http.error.status_code} < 600 handle @404 { rewrite * /404.html root * /var/www/html file_server header Content-Type "text/html; charset=utf-8" } handle @50x { rewrite * /50x.html root * /var/www/html file_server header Content-Type "text/html; charset=utf-8" header Cache-Control "no-cache" } } }
|
2. 🔧 错误处理部分详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| handle_errors { @404 expression {http.error.status_code} == 404 @500 expression {http.error.status_code} == 500 @502 expression {http.error.status_code} == 502 @503 expression {http.error.status_code} == 503 @504 expression {http.error.status_code} == 504 rewrite * /error.html?code={http.error.status_code} root * /var/www/html file_server header Content-Type "text/html; charset=utf-8" header X-Error "Handled by Caddy" }
|
3. ⚡ 配置生效命令
1 2 3 4 5 6 7 8 9 10
| sudo caddy fmt --overwrite /etc/caddy/Caddyfile
sudo systemctl reload caddy
curl -X POST http://localhost:2019/load \ -H "Content-Type: text/caddyfile" \ --data-binary @/etc/caddy/Caddyfile
|
🎨 三、错误页面设计
1. 📝 创建错误页面文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| sudo mkdir -p /var/www/html
sudo tee /var/www/html/50x.html <<'EOF' <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>服务器错误 - 500</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, height: 100vh; display: flex; align-items: center; justify-content: center; color: white; text-align: center; } .error-container { background: rgba(255, 255, 255, 0.1); padding: 2rem; border-radius: 1rem; backdrop-filter: blur(10px); } h1 { font-size: 4rem; margin: 0; } p { font-size: 1.2rem; } .home-btn { background: white; color: padding: 0.8rem 2rem; text-decoration: none; border-radius: 2rem; display: inline-block; margin-top: 1rem; transition: transform 0.3s; } .home-btn:hover { transform: translateY(-2px); } </style> </head> <body> <div class="error-container"> <h1>500</h1> <h2>服务器内部错误</h2> <p>抱歉,服务器遇到了意外错误。请稍后再试。</p> <a href="/" class="home-btn">返回首页</a> </div> </body> </html> EOF
|
2. 📱 响应式错误页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| sudo tee /var/www/html/404.html <<'EOF' <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>页面未找到 - 404</title> <style> /* 响应式设计 */ @media (max-width: 768px) { .error-container { margin: 1rem; padding: 1rem; } h1 { font-size: 3rem; } } </style> </head> <body> <!-- 页面内容 --> </body> </html> EOF
|
⚙️ 四、高级配置技巧
1. 🔄 基于位置的错误处理
1 2 3 4 5 6 7 8 9 10
| location /api/ { error_page 500 /api/50x.html; proxy_pass http://backend; }
location /static/ { error_page 404 /static/404.html; root /var/www/static; }
|
2. 📊 错误日志记录
1 2 3 4 5 6 7 8 9 10 11 12 13
| error_log /var/log/nginx/error.log warn;
server { error_page 404 = @404_error; location @404_error { access_log /var/log/nginx/404.log; return 404; } }
|
3. 🚀 性能优化配置
1 2 3 4 5 6 7 8 9 10 11
| location = /50x.html { root /var/www/html; internal; expires 1h; add_header Cache-Control "public"; gzip on; gzip_types text/html; }
|
🔍 五、测试与调试
1. 🧪 测试错误页面
1 2 3 4 5 6 7 8
| curl -I http://localhost/nonexistent-page
curl -X POST http://localhost/api/test-error
curl -k https://localhost:6663
|
2. 📋 调试技巧
1 2 3 4 5 6 7 8 9 10 11 12
| tail -f /var/log/nginx/error.log
journalctl -u caddy -f
nginx -t caddy validate
curl http://localhost/50x.html
|
3. 🐛 常见问题排查
1 2 3 4 5 6 7 8 9
| ls -la /var/www/html/50x.html
[ -d /var/www/html ] && echo "目录存在" || echo "目录不存在"
systemctl status nginx systemctl status caddy
|
⚠️ 六、注意事项
1. 🔒 安全注意事项
1 2 3 4 5 6
| chmod 644 /var/www/html/50x.html chown www-data:www-data /var/www/html/50x.html
|
2. 📊 性能考虑
- ✅ 错误页面应该轻量级,避免大量资源加载
- ✅ 适当缓存错误页面,但不要缓存太久
- ✅ 确保错误处理不会影响正常请求性能
3. 🎯 用户体验建议
- 友好的错误消息:提供清晰、友好的错误说明
- 导航选项:包含返回首页或联系支持的链接
- 品牌一致性:错误页面设计与网站风格一致
- 多语言支持:根据需要提供多语言错误页面
4. 🆘 紧急恢复
如果配置错误导致服务不可用:
1 2 3 4 5 6 7 8 9
| sudo nginx -s stop sudo cp /etc/nginx/nginx.conf.backup /etc/nginx/nginx.conf sudo nginx
sudo systemctl stop caddy sudo cp /etc/caddy/Caddyfile.backup /etc/caddy/Caddyfile sudo systemctl start caddy
|
🎯 配置完成检查清单
- [ ] Nginx/Caddy 错误页面配置完成
- [ ] 错误页面HTML文件已创建并测试
- [ ] 文件权限和所有权设置正确
- [ ] 配置语法检查通过
- [ ] 错误处理功能测试正常
- [ ] 日志记录配置正确
- [ ] 备份配置文件已创建
💡 提示:配置完成后建议进行全面的错误测试:
1 2 3
| curl -o /dev/null -s -w "%{http_code}\n" http://localhost/nonexistent curl -o /dev/null -s -w "%{http_code}\n" http://localhost/api/error
|
通过以上配置,您的网站将能够优雅地处理各种错误情况,为用户提供更好的体验! 🚀🛡️
Nginx & Caddy 错误页面拦截配置指南 🛡️