Redirect request to CDN using nginx

I have a couple of server addreses like cdn1.website.com, cdn2.website.com, cdn3.website.com. Each of them contains simillar files.

The request comes to my server and I want to redirect it or rewrite it to a random cdn server. Is it possible?

+7
source share
2 answers

You can try using 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; } } } 
+7
source

This, of course, is possible. Nginx comes with load balancing:

 upstream mysite { server www1.mysite.com; server www2.mysite.com; } 

This defines 2 servers for load balancing. By default, requests will be equally distributed across all defined servers. However, you can add weights to server entries.

In your server configuration {}, you can now add the following to send incoming requests to the load balancer (for example, to load the balance of all requests for the image catalog):

 location /images/ { proxy_pass http://mysite; } 

See the documentation for a more detailed description.

0
source

All Articles