0

Let's say I have ip_hash; turned on for load balancing between 4 different servers. So, client's IP address is used as a hashing key to determine which server his requests get routed to.

However, for file upload, it's best to keep all files in a single server. So, I want all /upload requests get routed to server 1 for any client. This means all requests obey IP-hash, except POST /upload which must be sent to server 1.

Is there a way to create this exception in NGINX? Thanks!

Saitama
  • 477
  • 6
  • 18

1 Answers1

0

Define two upstream containers, one with full load balancing and another with the POST specific service requirements:

upstream balancing { ... }
upstream uploading { ... }

Also, within the http container, define a map of the request method:

map $request_method $upstream {
    default balancing;
    POST    uploading;
}

Finally, within the server container, define a specific proxy_pass for the /upload URI:

location / {
    proxy_pass http://balancing;
}
location /upload {
    proxy_pass http://$upstream;
}

The upstream specification is evaluated from the value of the REQUEST_METHOD.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81