[英]Redirect request to CDN using nginx
I have a couple of server addreses, like cdn1.website.com, cdn2.website.com, cdn3.website.com. 我有几个服务器地址,如cdn1.website.com,cdn2.website.com,cdn3.website.com。 Each of them holds simillar files.
他们每个人都持有simillar文件。
Request comes to my server and I want to redirect or rewrite it to a random cdn server. 请求来到我的服务器,我想将其重定向或重写为随机的cdn服务器。 Is it possible ?
可能吗 ?
You could try using the split clients module: 您可以尝试使用拆分客户端模块:
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;
}
}
}
This is of course possible. 这当然是可能的。 Nginx comes with load balancing:
Nginx带有负载均衡:
upstream mysite {
server www1.mysite.com;
server www2.mysite.com;
}
This defines 2 servers for load balancing. 这定义了2个服务器用于负载平衡。 By default requests will be equally distributed across all defined servers.
默认情况下,请求将平均分配到所有已定义的服务器。 You can however add weights to the server entries.
但是,您可以为服务器条目添加权重。
Inside your server {} configuration you can now add the following to pass incoming requests to the load balancer (eg to load balance all requests for the images directory): 在您的服务器{}配置中,您现在可以添加以下内容以将传入请求传递给负载均衡器(例如,对图像目录的所有请求进行负载均衡):
location /images/ {
proxy_pass http://mysite;
}
Have a look at the documentation for a more detailed description. 有关更详细的说明,请查看文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.