Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to configure nginx (or other quick reverse proxy) dynamically?

Tags:

Suppose we have several identical nodes which are the application servers of some n-tier service. And suppose we use Apache ZooKeeper to keep all the config's of our distributed application. Plus we have an nginx as a load balancer and reverse proxy in front of this application.

So let's say we perform a command which changes data only on node1, and for some period of time node2 differs from node1. And we want proxy to redirect all that special requests (which need that specific data) to node1 until all the infomation has migrated to node2 and node2 has the same data as node1.

Is there any way to make nginx (or other proxy) read its config from Apache ZooKeeper? Or more broader: is there any way to effectively switch proxy configuration on fly? And of course it should be done without (or with minimal) downtime of the whole system - so restarting nginx is not the option.

like image 313
Dima Avatar asked Jan 24 '12 06:01

Dima


People also ask

What method could you use to make dynamic configuration changes with NGINX plus?

NGINX Plus adds additional methods for modifying configuration dynamically. By using DNS as the definitive repository for upstream servers, you can make changes without touching NGINX Plus configuration files.

Can NGINX do reverse proxy?

The benefits of using Nginx as a reverse proxy include: Clients access all backend resources through a single web address. The reverse proxy can serve static content, which reduces the load on application servers such as Express, Tomcat or WebSphere.

Is NGINX proxy or reverse 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.

Is NGINX a load balancer or reverse proxy?

NGINX Plus and NGINX are the best-in-class reverse proxy and load balancing solutions used by high-traffic websites such as Dropbox, Netflix, and Zynga. More than 350 million websites worldwide rely on NGINX Plus and NGINX Open Source to deliver their content quickly, reliably, and securely.


2 Answers

Nginx has two methods of changing configuration:

  • HUP signal to the master process results in "reload". Nginx starts a bunch of new workers and lets the old workers to shutdown gracefully, i.e. they finish existing requests. There is no interruption of service. This method of configuration change is very lightweight and quick, but has few limitations: you cannot change cache zones or re-compile Perl scripts.

  • USR2 signal, then WINCH and then QUIT to the master process result in "executable upgrade" and this sequence lets completely re-read whole configuration and even upgrade the Nginx executable. It reloads disk caches as well (which maybe time consuming). This method results in no interruption of service too.

Official documentation

like image 59
Alexander Azarov Avatar answered Oct 02 '22 15:10

Alexander Azarov


Please try Nginx-Clojure. We can use a clojure/java/groovy rewrite handler to access zookeeper then update some nginx variables to dynamically change proxy target. e.g.

In nginx.conf

set $mytarget "";  location / {    rewrite_handler_type java;    ## We will change $mytarget in MyRewriteHandler    rewrite_handler_name my.MyRewriteHandler;    proxy_pass $mytarget; } 

In MyRewriteHandler.java

public static class MyRewriteHandler implements NginxJavaRingHandler {          @Override         public Object[] invoke(Map<String, Object> request) {            //access zookeeper            ...............            //change nginx variable mytarget            ((NginxJavaRequest)request).setVaraible("mytarget", "http://some-host-or-url");         } 
like image 43
xfeep Avatar answered Oct 02 '22 15:10

xfeep