Hugo博客公告弹窗

cloudflare + 反向代理 + haproxy

原理:源站开启cf 小云朵,落地机反代源站,线路机中转落地机

注意:源站域名要在cf后台关闭自动程序,waf放行落地机ip

项目:https://github.com/woniu336/cf-cdn-s

项目2:https://github.com/woniu336/cf-cdn

优化脚本

curl -sS -O https://raw.githubusercontent.com/woniu336/open_shell/main/menu.sh && chmod +x menu.sh && ./menu.sh

放行端口

ufw allow 80/tcp
ufw allow 443/tcp

应用

sudo ufw reload

查看规则

sudo ufw status numbered

删除缓存

sudo rm -rf /usr/local/nginx/cache/proxy/*

快速部署

在反代服务器上执行:

curl -sS -O https://raw.githubusercontent.com/woniu336/cf-cdn-s/main/cf-cdn-s.sh && chmod +x cf-cdn-s.sh && ./cf-cdn-s.sh

先安装nginx,如果要添加www,在配置文件这样表示:server_name 123.cc www.123.cc;

查看缓存大小:

du -sh /usr/local/nginx/cache/proxy

证书配置

单账号配置

如果所有域名都在同一个 Cloudflare 账号下:

  1. 创建凭证文件:
mkdir -p /root/.secrets && nano /root/.secrets/cloudflare.ini
  1. 添加 API Token:
dns_cloudflare_api_token = 你的cloudflare_api_token

多账号配置

如果使用多个 Cloudflare 账号:

  1. 为特定域名创建配置文件:
mkdir -p /root/.secrets && nano /root/.secrets/example.com.ini
  1. 添加对应账号的 API Token:
dns_cloudflare_api_token = cloudflare_api_token

设置权限

对所有配置文件应用安全权限:

chmod 600 /root/.secrets/*.ini

获取真实IP

快速配置

在反代服务器上执行:

curl -sS -O https://raw.githubusercontent.com/woniu336/cf-cdn-s/main/update_nginx.sh && chmod +x update_nginx.sh && ./update_nginx.sh

管理转发 IP

# 查看配置
cat /etc/nginx/nginx.conf

# 移除特定转发 IP
sudo sed -i "/set_real_ip_from 具体IP;/d" /etc/nginx/nginx.conf

# 重启 Nginx
sudo nginx -t && sudo systemctl restart nginx

HAProxy 配置

注意:先获取真实ip,在来安装haproxy,顺序不能颠倒

快速安装

在转发服务器上执行:

curl -sS -O https://raw.githubusercontent.com/woniu336/cf-cdn-s/main/setup_haproxy.sh && chmod +x setup_haproxy.sh && ./setup_haproxy.sh

手动配置

  1. 安装 HAProxy:
sudo apt update && sudo apt install haproxy -y
  1. 备份原配置:
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
  1. 下载并修改配置:
sudo curl -sS -o /etc/haproxy/haproxy.cfg https://raw.githubusercontent.com/woniu336/cf-cdn-s/main/haproxy.cfg
sudo sed -i 's/8\.8\.8\.8/你的IP/g' /etc/haproxy/haproxy.cfg
  1. 验证并重启:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl restart haproxy
  1. 开机自启
sudo systemctl enable haproxy

备份和还原

备份

在反代服务器上执行:

curl -sS -O https://raw.githubusercontent.com/woniu336/cf-cdn-s/main/backup-nginx-ssl.sh && chmod +x backup-nginx-ssl.sh && ./backup-nginx-ssl.sh

还原步骤

  1. 将备份文件复制到目标服务器
  2. 解压备份:
tar -xzf backup文件名.tar.gz -C /tmp/restore/
  1. 还原文件:
sudo cp -r /tmp/restore/letsencrypt/* /etc/letsencrypt/
sudo cp -r /tmp/restore/nginx/conf.d/* /etc/nginx/conf.d/
sudo cp -r /tmp/restore/nginx/certs/* /etc/nginx/certs/
sudo cp -r /tmp/restore/nginx/templates/* /etc/nginx/templates/
sudo cp /tmp/restore/nginx/nginx.conf /etc/nginx/
  1. 设置权限:
sudo chown -R root:root /etc/letsencrypt
sudo chmod -R 600 /etc/nginx/certs/*
sudo chmod 644 /etc/nginx/nginx.conf
sudo chmod -R 644 /etc/nginx/conf.d/*
  1. 重启 Nginx:
sudo systemctl restart nginx

注意:还原前请确保已安装 Nginx 并备份原有配置。

手动申请证书

certbot certonly  -d "*.xxx.com" --manual --preferred-challenges dns-01  --server https://acme-v02.api.letsencrypt.org/directory

续订

certbot renew
CC BY-NC-SA 4.0 转载请注明
最后更新于 2025-02-19 08:29
clarity统计