参考资料
使用Nginx配置CDN负载均衡
使用Nginx配置CDN负载均衡
Nginx可以作为高效的CDN负载均衡器,将请求分发到多个CDN节点。以下是详细的配置方法:
基础配置方案
1. 配置upstream模块
在nginx.conf
或站点配置文件中添加upstream块:
- http {
- upstream cdn_pool {
- # 配置多个CDN节点
- server cdn1.yourdomain.com;
- server cdn2.yourdomain.com;
- server cdn3.yourdomain.com;
- # 可选的负载均衡方法:
- # least_conn; # 最少连接
- # ip_hash; # IP哈希保持会话
- # random; # 随机分配
- # 权重配置示例
- server cdn1.yourdomain.com weight=5;
- server cdn2.yourdomain.com weight=3;
- server cdn3.yourdomain.com weight=2;
- }
- }
2. 配置server块
- server {
- listen 80;
- server_name yourdomain.com;
- location / {
- proxy_pass http://cdn_pool;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- # 健康检查配置
- proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
- proxy_connect_timeout 2s;
- proxy_read_timeout 5s;
- proxy_send_timeout 5s;
- }
- }
高级配置方案
1. 基于地理位置的CDN选择
- http {
- # 定义不同区域的CDN
- upstream cdn_asia {
- server asia-cdn1.yourdomain.com;
- server asia-cdn2.yourdomain.com;
- }
- upstream cdn_europe {
- server eu-cdn1.yourdomain.com;
- server eu-cdn2.yourdomain.com;
- }
- upstream cdn_america {
- server us-cdn1.yourdomain.com;
- server us-cdn2.yourdomain.com;
- }
- # 使用geo模块
- geo $geo {
- default cdn_america;
- 1.0.0.0/8 cdn_asia;
- 2.0.0.0/8 cdn_europe;
- # 更多IP范围...
- }
- map $geo $upstream_pool {
- cdn_asia cdn_asia;
- cdn_europe cdn_europe;
- cdn_america cdn_america;
- }
- }
- server {
- listen 80;
- server_name yourdomain.com;
- location / {
- proxy_pass http://$upstream_pool;
- # 其他proxy设置...
- }
- }
2. 动态CDN选择(基于性能)
- http {
- lua_shared_dict cdn_performance 10m;
- init_by_lua_block {
- local cdn_list = {
- { url = "cdn1.yourdomain.com", weight = 5 },
- { url = "cdn2.yourdomain.com", weight = 3 },
- { url = "cdn3.yourdomain.com", weight = 2 }
- }
- ngx.shared.cdn_performance:set("cdn_list", cjson.encode(cdn_list))
- }
- }
- server {
- listen 80;
- server_name yourdomain.com;
- location / {
- access_by_lua_block {
- local cjson = require "cjson"
- local http = require "resty.http"
- local cdn_list = cjson.decode(ngx.shared.cdn_performance:get("cdn_list"))
- -- 测试CDN响应时间
- local best_cdn = cdn_list[1]
- local min_latency = math.huge
- for _, cdn in ipairs(cdn_list) do
- local httpc = http.new()
- httpc:set_timeout(1000) -- 1秒超时
- local start = ngx.now()
- local res, err = httpc:request_uri("http://"..cdn.url.."/ping")
- local latency = ngx.now() - start
- if res and res.status == 200 then
- local score = cdn.weight / (latency + 1)
- if score > (best_cdn.weight / (min_latency + 1)) then
- best_cdn = cdn
- min_latency = latency
- end
- end
- httpc:close()
- end
- ngx.var.best_cdn = best_cdn.url
- }
- proxy_pass http://$best_cdn;
- proxy_set_header Host $host;
- # 其他proxy设置...
- }
- }
健康检查配置
1. 被动健康检查
- upstream cdn_pool {
- server cdn1.yourdomain.com max_fails=3 fail_timeout=30s;
- server cdn2.yourdomain.com max_fails=3 fail_timeout=30s;
- server cdn3.yourdomain.com max_fails=3 fail_timeout=30s;
- }
2. 主动健康检查(需要Nginx Plus)
- upstream cdn_pool {
- zone cdn_pool 64k;
- server cdn1.yourdomain.com;
- server cdn2.yourdomain.com;
- server cdn3.yourdomain.com;
- health_check interval=5s fails=3 passes=2 uri=/health;
- }
缓存优化配置
- proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CDN_CACHE:10m inactive=60m use_temp_path=off;
- server {
- # ...其他配置...
- location / {
- proxy_cache CDN_CACHE;
- proxy_cache_key "$scheme://$host$request_uri";
- proxy_cache_valid 200 302 10m;
- proxy_cache_valid 404 1m;
- proxy_pass http://cdn_pool;
- # ...其他proxy设置...
- }
- }
日志配置
- log_format cdn_log '$remote_addr - $remote_user [$time_local] '
- '"$request" $status $body_bytes_sent '
- '"$http_referer" "$http_user_agent" '
- 'CDN: $upstream_addr Response: $upstream_response_time';
- server {
- access_log /var/log/nginx/cdn_access.log cdn_log;
- error_log /var/log/nginx/cdn_error.log;
- # ...其他配置...
- }
配置验证和重载
- # 测试配置语法
- sudo nginx -t
- # 重载配置
- sudo nginx -s reload
注意事项
-
确保所有CDN节点配置相同的缓存规则
-
监控各CDN节点的性能表现
-
定期检查Nginx负载均衡状态:
- # 查看Nginx状态
- sudo nginx -T
- # 查看连接状态
- sudo ss -tulnp | grep nginx
-
对于HTTPS站点,需要配置SSL终止或SSL透传
以上配置可以根据您的具体需求进行调整,实现高效的多CDN负载均衡方案。