Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure nginx to act as a load balancer for proxies?

Tags:

curl

nginx

proxy

I am aware that nginx can be configured to act as a load balancer, but I'm wondering if it is possible to load balance between proxies? Let's say I have multiple proxies running on localhost, and I want to use nginx to provide a single point of connection so that I can rotate between the proxies. I am trying to achieve something similar to the post here, which is using HAProxy instead of nginx. I have the following nginx.conf:

events {  }

http {
    upstream proxies {
        server localhost:9998;
        server localhost:9999;
        server localhost:10000;
    }
    server {
        listen 8080;

        location / {
            proxy_pass http://proxies;
        }
    }
}

However, when I send a curl request like this:

curl http://icanhazip.com -x localhost:8080

It ignores the url, and I get a response similar to what I would expect if I had directly sent a request to one of the proxy servers like so:

curl localhost:9999

Of course, I did not really expect it to work, since there must be some option to tell nginx to treat the upstream servers as proxies themselves. However, I was not able to find how to do this after searching online.

like image 324
b_pcakes Avatar asked Aug 23 '16 06:08

b_pcakes


People also ask

Can a proxy server act as a load balancer?

A load balancer or Web proxy server allows all applications in the domain to be represented as a single address to external clients, and is required when using in-memory replication for client session information.

Can Nginx act as load balancer?

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.

Is Nginx proxy manager a load balancer?

Nginx Proxy Manager is a tool in the Load Balancer / Reverse Proxy category of a tech stack.

Can Nginx act as proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.


2 Answers

The nginx docs say upstreams are distributed using a round-robin method.

By default, requests are distributed between the servers using a weighted round-robin balancing method

https://nginx.org/en/docs/http/ngx_http_upstream_module.html

like image 75
mhumesf Avatar answered Oct 31 '22 09:10

mhumesf


as you can see in your haproxy post haproxy akt as a forward proxy

option http_proxy

What this option mean is described in the manual https://cbonte.github.io/haproxy-dconv/1.6/configuration.html#4-option%20http_proxy

It sometimes happens that people need a pure HTTP proxy which understands basic proxy requests without caching nor any fancy feature. In this case, it may be worth setting up an HAProxy instance with the "option http_proxy" set. In this mode, no server is declared, and the connection is forwarded to the IP address and port found in the URL after the "http://" scheme.

No host address resolution is performed, so this only works when pure IP addresses are passed. Since this option's usage perimeter is rather limited, it will probably be used only by experts who know they need exactly it. This is incompatible with the HTTP tunnel mode.

As far as I know nginx does not have this feature.

A similar question it this. https://superuser.com/questions/604352/nginx-as-forward-proxy-for-https

Why can't you use the haproxy as described in the post of your link?

like image 37
Aleksandar Avatar answered Oct 31 '22 10:10

Aleksandar