使用Nginx配置CDN负载均衡

参考资料

  1. 什么是Range回源?
  2. 单连接带宽限制 设置教程
  3. 跟随CDN服务 与 跟随源站有什么区别
  4. 什么是鉴权URL?
  5. CDN配置:监控告警及规则条件参数设置指南
  6. CDN详细分析讲解,包括添加URL跳转时的参数说明
  7. 如何配置自定义响应头参数
  8. 如何配置页面优化

使用Nginx配置CDN负载均衡

使用Nginx配置CDN负载均衡

Nginx可以作为高效的CDN负载均衡器,将请求分发到多个CDN节点。以下是详细的配置方法:

基础配置方案

1. 配置upstream模块

nginx.conf或站点配置文件中添加upstream块:

 
  1. http {
  2.     upstream cdn_pool {
  3.         # 配置多个CDN节点
  4.         server cdn1.yourdomain.com;
  5.         server cdn2.yourdomain.com;
  6.         server cdn3.yourdomain.com;
  7.         
  8.         # 可选的负载均衡方法:
  9.         # least_conn;   # 最少连接
  10.         # ip_hash;     # IP哈希保持会话
  11.         # random;      # 随机分配
  12.         
  13.         # 权重配置示例
  14.         server cdn1.yourdomain.com weight=5;
  15.         server cdn2.yourdomain.com weight=3;
  16.         server cdn3.yourdomain.com weight=2;
  17.     }
  18. }

2. 配置server块

 
  1. server {
  2.     listen 80;
  3.     server_name yourdomain.com;
  4.     
  5.     location / {
  6.         proxy_pass http://cdn_pool;
  7.         proxy_set_header Host $host;
  8.         proxy_set_header X-Real-IP $remote_addr;
  9.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  10.         
  11.         # 健康检查配置
  12.         proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  13.         proxy_connect_timeout 2s;
  14.         proxy_read_timeout 5s;
  15.         proxy_send_timeout 5s;
  16.     }
  17. }

高级配置方案

1. 基于地理位置的CDN选择

 
  1. http {
  2.     # 定义不同区域的CDN
  3.     upstream cdn_asia {
  4.         server asia-cdn1.yourdomain.com;
  5.         server asia-cdn2.yourdomain.com;
  6.     }
  7.     
  8.     upstream cdn_europe {
  9.         server eu-cdn1.yourdomain.com;
  10.         server eu-cdn2.yourdomain.com;
  11.     }
  12.     
  13.     upstream cdn_america {
  14.         server us-cdn1.yourdomain.com;
  15.         server us-cdn2.yourdomain.com;
  16.     }
  17.     
  18.     # 使用geo模块
  19.     geo $geo {
  20.         default         cdn_america;
  21.         1.0.0.0/8      cdn_asia;
  22.         2.0.0.0/8      cdn_europe;
  23.         # 更多IP范围...
  24.     }
  25.     
  26.     map $geo $upstream_pool {
  27.         cdn_asia     cdn_asia;
  28.         cdn_europe   cdn_europe;
  29.         cdn_america  cdn_america;
  30.     }
  31. }
  32. server {
  33.     listen 80;
  34.     server_name yourdomain.com;
  35.     
  36.     location / {
  37.         proxy_pass http://$upstream_pool;
  38.         # 其他proxy设置...
  39.     }
  40. }

2. 动态CDN选择(基于性能)

 
  1. http {
  2.     lua_shared_dict cdn_performance 10m;
  3.     
  4.     init_by_lua_block {
  5.         local cdn_list = {
  6.             { url = "cdn1.yourdomain.com", weight = 5 },
  7.             { url = "cdn2.yourdomain.com", weight = 3 },
  8.             { url = "cdn3.yourdomain.com", weight = 2 }
  9.         }
  10.         ngx.shared.cdn_performance:set("cdn_list", cjson.encode(cdn_list))
  11.     }
  12. }
  13. server {
  14.     listen 80;
  15.     server_name yourdomain.com;
  16.     
  17.     location / {
  18.         access_by_lua_block {
  19.             local cjson = require "cjson"
  20.             local http = require "resty.http"
  21.             
  22.             local cdn_list = cjson.decode(ngx.shared.cdn_performance:get("cdn_list"))
  23.             
  24.             -- 测试CDN响应时间
  25.             local best_cdn = cdn_list[1]
  26.             local min_latency = math.huge
  27.             
  28.             for _, cdn in ipairs(cdn_list) do
  29.                 local httpc = http.new()
  30.                 httpc:set_timeout(1000) -- 1秒超时
  31.                 local start = ngx.now()
  32.                 local res, err = httpc:request_uri("http://"..cdn.url.."/ping")
  33.                 local latency = ngx.now() - start
  34.                 
  35.                 if res and res.status == 200 then
  36.                     local score = cdn.weight / (latency + 1)
  37.                     if score > (best_cdn.weight / (min_latency + 1)) then
  38.                         best_cdn = cdn
  39.                         min_latency = latency
  40.                     end
  41.                 end
  42.                 httpc:close()
  43.             end
  44.             
  45.             ngx.var.best_cdn = best_cdn.url
  46.         }
  47.         
  48.         proxy_pass http://$best_cdn;
  49.         proxy_set_header Host $host;
  50.         # 其他proxy设置...
  51.     }
  52. }

健康检查配置

1. 被动健康检查

 
  1. upstream cdn_pool {
  2.     server cdn1.yourdomain.com max_fails=3 fail_timeout=30s;
  3.     server cdn2.yourdomain.com max_fails=3 fail_timeout=30s;
  4.     server cdn3.yourdomain.com max_fails=3 fail_timeout=30s;
  5. }

2. 主动健康检查(需要Nginx Plus)

 
  1. upstream cdn_pool {
  2.     zone cdn_pool 64k;
  3.     server cdn1.yourdomain.com;
  4.     server cdn2.yourdomain.com;
  5.     server cdn3.yourdomain.com;
  6.     
  7.     health_check interval=5s fails=3 passes=2 uri=/health;
  8. }

缓存优化配置

 
  1. proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CDN_CACHE:10m inactive=60m use_temp_path=off;
  2. server {
  3.     # ...其他配置...
  4.     
  5.     location / {
  6.         proxy_cache CDN_CACHE;
  7.         proxy_cache_key "$scheme://$host$request_uri";
  8.         proxy_cache_valid 200 302 10m;
  9.         proxy_cache_valid 404 1m;
  10.         
  11.         proxy_pass http://cdn_pool;
  12.         # ...其他proxy设置...
  13.     }
  14. }

日志配置

 
  1. log_format cdn_log '$remote_addr - $remote_user [$time_local] '
  2.                    '"$request" $status $body_bytes_sent '
  3.                    '"$http_referer" "$http_user_agent" '
  4.                    'CDN: $upstream_addr Response: $upstream_response_time';
  5.                    
  6. server {
  7.     access_log /var/log/nginx/cdn_access.log cdn_log;
  8.     error_log /var/log/nginx/cdn_error.log;
  9.     
  10.     # ...其他配置...
  11. }

配置验证和重载

 
  1. # 测试配置语法
  2. sudo nginx -t
  3. # 重载配置
  4. sudo nginx -s reload

注意事项

  1. 确保所有CDN节点配置相同的缓存规则

  2. 监控各CDN节点的性能表现

  3. 定期检查Nginx负载均衡状态:

     
      
    1. # 查看Nginx状态
    2. sudo nginx -T
    3. # 查看连接状态
    4. sudo ss -tulnp | grep nginx
  4. 对于HTTPS站点,需要配置SSL终止或SSL透传

以上配置可以根据您的具体需求进行调整,实现高效的多CDN负载均衡方案。

TAG:使用Nginx配置CDN负载均衡

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值