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

后端 未结 8 1843
旧巷少年郎
旧巷少年郎 2021-02-04 02:12

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 distribute

相关标签:
8条回答
  • 2021-02-04 02:42

    It is possible using HAProxy and its UNIX domain socket interface: http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#9.2.

    It supports switching a server or an entire front-end from down to up and back again on the fly. With a configuration file that defines two sets of front-ends, each configured for one specific state, you would be able to achieve what you want.

    0 讨论(0)
  • 2021-02-04 02:54

    As an update:Hipache stores its host configuration in redis, which can easily be manipulated at runtime. It's also based on node.js and node-http-proxy.

    0 讨论(0)
  • 2021-02-04 02:55

    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

    0 讨论(0)
  • 2021-02-04 02:56

    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");
            }
    
    0 讨论(0)
  • 2021-02-04 02:57

    From Docs:

    nginx -s reload
    

    -s is for 'signal', where signal can be 'quit', 'reload', 'reopen', or 'stop'.

    0 讨论(0)
  • 2021-02-04 03:03

    There is an interesting project using nginx Lua to allow dynamic configuration of nginx and doing exactly what you want (https://github.com/samalba/hipache-nginx)

    It is written by the guys behind Hipache.

    0 讨论(0)
提交回复
热议问题