Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 holds simillar files.

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

like image 811
Romans Ruskovs Avatar asked Sep 26 '11 13:09

Romans Ruskovs


People also ask

What is return 301 in nginx?

Visitor–> Website Page–> Website is under maintenance. On the other hand, a permanent Nginx redirect informs the web browser that it should permanently link the old page or domain to a new location or domain. To map this change, the redirects response code 301 is used for designating the permanent movement of a page.

What is Proxy_pass in nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.


1 Answers

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;
    }
  }
}
like image 186
kolbyjack Avatar answered Oct 13 '22 07:10

kolbyjack