Redirect request to CDN using nginx Redirect request to CDN using nginx nginx nginx

Redirect request to CDN using nginx


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:

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. 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 (e.g. 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.