我有几个服务器地址,比如cdn1.website.com,cdn2.website.com,cdn3.website.com。它们中的每一个都包含相似的文件。
请求到达我的服务器,我想将其重定向或重写到随机的cdn服务器。有可能吗?
发布于 2011-09-27 07:35:35
您可以尝试使用split clients模块:
http {
# Split clients (approximately) equally based on
# client ip address
split_clients $remote_addr $cdn_host {
33% cdn1;
33% cdn2;
- cdn3;
}
server {
server_name example.com;
# Use the variable defined by the split_clients block to determine
# the rewritten hostname for requests beginning with /images/
location /images/ {
rewrite ^ http://$cdn_host.example.com$request_uri? permanent;
}
}
}发布于 2011-09-26 21:48:34
这当然是可能的。Nginx自带负载均衡:
upstream mysite {
server www1.mysite.com;
server www2.mysite.com;
}这为负载均衡定义了2台服务器。默认情况下,请求将均匀分布在所有已定义的服务器上。但是,您可以向服务器条目添加权重。
在您的服务器{}配置中,您现在可以添加以下内容,以将传入请求传递到负载均衡器(例如,对图像目录的所有请求进行负载均衡):
location /images/ {
proxy_pass http://mysite;
}有关更详细的说明,请查看documentation。
https://stackoverflow.com/questions/7556003
复制相似问题