Search code examples
imagenginxhttp-redirectproxyload-balancing

Nginx Load Balancing


I want to load balance my website with nginx.

The load balancing in nginx wiki is proxy, so the actual file being downloaded from the frontend server. (http://wiki.nginx.org/LoadBalanceExample)

This is how I need the balancing:

user request file:

  • http:// site.com/image1.jpg

nginx redirect user to one of the servers (with Location header):

  • http:// s1.site.com/image1.jpg
  • http:// s1.site.com/image1.jpg
  • http:// s3.site.com/image1.jpg

Is this possible with nginx?


Solution

  • http {
      split_clients "${remote_addr}" $server_id {
        33.3% 1;
        33.3% 2;
        33.4% 3;
      }
    
      server {
        location ~* \.(gif|jpg|jpeg)$ {
          return 301 "${scheme}://s${server_id}.site.com${request_uri}";
        }
      }