Nginx as LB to redirect/return to upstream server instead of Proxy

Maxim Dounin mdounin at mdounin.ru
Mon Oct 15 14:45:39 UTC 2018


Hello!

On Mon, Oct 15, 2018 at 03:15:39PM +0200, Aleksandar Lazic wrote:

> Hi.
> 
> Am 15.10.2018 um 09:08 schrieb anish10dec:
> > We want to use Nginx as LB in a way so that Nginx can return 301 or 302
> > redirect to client instead of Proxying request to backend/upstream servers.
> > 
> > It is required as Server which is configured as LB is having limited
> > throughput of 1 Gbps while upstream servers are having throughput of 10Gbps
> > .
> > 
> > We want users to directly connect to Upstream Server for Data delivery. 
> > Nginx LB Server to make sure that all upstream are up and functional before
> > giving 301 or 302 redirect to any of upstream server
> >
> > Example: 
> > 
> > http://nginxlb.com/data/download
> > 
> > Nginx LB Returns Redirected URL to Client 301 or 302 ( That upstream should
> > be up)
> > 
> > http://upstreamserver1.com/data/download
> > http://upstreamserver2.com/data/download
> > 
> > Is this possible by :
> > 
> > return 301 http://$upstream_addr/data/download
> 
> I would do this with maps, rewrite and upstream variables.
> 
> https://nginx.org/en/docs/http/ngx_http_map_module.html
> https://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables
> https://nginx.org/en/docs/http/ngx_http_rewrite_module.html
> 
> Untested:
> ###
> map $upstream_addr $direct_domain {
>     default       nginxlb.com;
>     IP1 upstreamserver1.com;
>     IP2 upstreamserver2.com;
> }
> 
> return 301 http://$direct_domain/data/download
> ###

This won't work, because the $upstream_addr variable won't be 
available without an actual upstream connection.

To balance traffic to multiple servers without proxying, the 
split_clients module can be used.  For example, something like 
this should world, balancing based on client's address (untested 
though):

    split_clients $remote_addr $domain {
        50% upstreamserver1.com;
        *   upstreamserver2.com;
    }

    return 302 http://$domain/data/download;

Docs are available here:

http://nginx.org/en/docs/http/ngx_http_split_clients_module.html

Note thought that this won't do any checks on whether the upstream 
server is up or not.  If checks are needed, a better approach 
might be to use some more sophisticated logic to return such 
redirects.  Most simple solution would be to actually proxy 
requests to the upstream servers, and let these servers to return 
actual redirects to themselves.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list