I want to use Nginx in a bit of an unconventional way. I want to load balance between a few servers, but rather than proxy content for those servers I want to redirect to them.
That is, if a request comes into my Nginx server, it should 302 redirect to another server by randomly picking one out of an upstream block or equivalent. I don't even need the other upstream block features, such as weighting (but it wouldn't hurt to have them available if possible).
For now, I have the following config, which does what I need but is a bit ugly:
upstream boxes {
server 127.0.0.1:46011;
server 127.0.0.1:46012;
}
server {
listen 46011;
return 302 http://box11.example.com$request_uri;
}
server {
listen 46012;
return 302 http://box12.example.com$request_uri;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://boxes;
}
}
Is there a simpler way to handle this situation without resorting to proxying to myself?
You can get a random variable from misc module:
Then choose internal location based on it, with simple weighting:
Redirects:
Other variant: use return 302 $some_variable; (and full URLs in map) directly in "/" location.