Featured image of post Nginx & Caddy 重定向配置指南 🌐

Nginx & Caddy 重定向配置指南 🌐

Nginx & Caddy 重定向配置指南 🌐 🔀 本文详细介绍如何使用 Nginx 和 Caddy 服务器实现高效的重ê

Nginx & Caddy 重定向配置指南 🌐

File Server Setup

🔀 本文详细介绍如何使用 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
# 创建并编辑Nginx配置文件
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 证书文件和私钥文件的路径
    ssl_certificate /etc/nginx/keyfile/cert.pem;
    ssl_certificate_key /etc/nginx/keyfile/key.pem;

    ## 要跳转的网址
    ## 访问:https://123.mobufan.eu.org:5553
    ## 跳转到:https://404.mobufan.eu.org:5553
    return 301 https://404.mobufan.eu.org:5553$request_uri;
}
EOF

2. ⚡ 配置生效命令

1
2
3
4
5
6
7
8
# 检查Nginx配置语法
nginx -t

# 重新加载Nginx配置
nginx -s reload

# 或者使用systemctl
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;
    
    # 永久重定向到HTTPS
    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
# 创建Caddy配置目录和文件
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'
## 重定向到 baidu
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 {
        ## 将所有错误重定向到50x.html页面
        rewrite * /50x.html
        ## 指定错误页面的根目录
        root * /var/www/html
        file_server
    }
}
EOF

2. ⚡ 配置生效命令

1
2
3
4
5
6
7
8
# 格式化Caddy配置
cd /usr/local/caddy && ./caddy fmt --overwrite

# 重新加载Caddy配置
cd /usr/local/caddy && ./caddy reload

# 或者使用systemctl
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
# 创建证书目录(Nginx)
mkdir -p /etc/nginx/keyfile
chmod 700 /etc/nginx/keyfile

# 创建证书目录(Caddy)
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

# 或者对于Caddy
chmod 600 /usr/local/caddy/ssl/full_chain.pem
chmod 600 /usr/local/caddy/ssl/private.key

3. 📜 证书自动续期

1
2
3
4
5
# 使用certbot自动续期(Nginx)
certbot renew --nginx --quiet

# Caddy自动处理证书续期(内置功能)
# 无需额外配置,Caddy会自动管理证书

🔧 四、错误处理配置

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
# 查看Nginx错误日志
tail -f /var/log/nginx/error.log

# 查看Caddy错误日志
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优化
    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性能
nginx -T

# 监控Caddy状态
curl localhost:2019/metrics

# 压力测试
ab -n 1000 -c 100 https://example.com:6663/

⚠️ 六、注意事项

1. 🔒 安全注意事项

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 定期更新SSL证书
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配置
nginx -t

# 检查Caddy配置
caddy validate

# 查看服务状态
systemctl status nginx
systemctl status caddy

# 端口占用检查
netstat -tulnp | grep -E '(5553|6663)'

3. 📋 最佳实践

  1. ✅ 总是使用HTTPS重定向
  2. ✅ 配置合适的HTTP状态码(301/302)
  3. ✅ 设置正确的错误处理页面
  4. ✅ 定期备份配置文件
  5. ✅ 监控重定向性能
  6. ✅ 测试重定向逻辑

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

# DNS解析检查
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重定向服务,确保用户访问体验和系统稳定性! 🌐🔀