有没有办法动态配置 nginx(或其他快速反向代理)?
假设我们有几个相同的节点,它们是某个 n 层服务的应用程序服务器。假设我们使用 Apache ZooKeeper 来保存分布式应用程序的所有配置。另外,我们在此应用程序前面有一个 nginx 作为负载均衡器和反向代理。
假设我们执行的命令仅更改节点 1 上的数据,并且在一段时间内节点 2 与节点 1 不同。我们希望代理将所有特殊请求(需要特定数据)重定向到节点 1,直到所有信息都迁移到节点 2 并且节点 2 具有与节点 1 相同的数据。
有没有办法让 nginx(或其他代理)从 Apache ZooKeeper 读取其配置?或者更广泛:有没有办法可以有效地动态切换代理配置?当然,这应该在整个系统没有(或最少)停机时间的情况下完成 - 所以重新启动 nginx 不是一个选择。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Nginx 有两种更改配置的方法:
HUP
信号会导致“重新加载”。 Nginx 启动一堆新的worker,并让旧的worker 优雅地关闭,即它们完成现有的请求。服务不会中断。这种配置更改方法非常轻量且快速,但有一些限制:您无法更改缓存区域或重新编译 Perl 脚本。USR2
信号,然后WINCH
,然后QUIT
到主进程导致“可执行升级”,这个序列让我们完全重新读取整个配置,甚至升级 Nginx 可执行文件。它还会重新加载磁盘缓存(这可能很耗时)。此方法也不会导致服务中断。官方文档
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, thenWINCH
and thenQUIT
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
请尝试 Nginx-Clojure。我们可以使用 clojure/java/groovy 重写处理程序来访问 Zookeeper,然后更新一些 nginx 变量来动态更改代理目标。例如
在 nginx.conf 中
在 MyRewriteHandler.java 中
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
In MyRewriteHandler.java
这可能会晚一些,但如果你有钱的话。 Nginx plus 正是适合您的。 它使用简单的 url 调用来获取新内容即时配置。
This may be late but if you have the money. Nginx plus is exactly for you. It uses a simple url call to get new configurations on the fly.
作为更新:Hipache 将其主机配置存储在 redis 中,可以在运行时轻松操作。它也基于node.js 和node-http-proxy。
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.
有一个有趣的项目,使用 nginx Lua 来允许动态配置 nginx 并完全按照您想要的方式进行操作(https:// github.com/samalba/hipache-nginx)
它是由 Hipache 背后的人编写的。
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.
可以使用 HAProxy 及其 UNIX 域套接字接口: http://cbonte .github.io/haproxy-dconv/configuration-1.5.html#9.2。
它支持动态地将服务器或整个前端从下到上切换。通过定义两组前端的配置文件,每组前端配置为一种特定状态,您将能够实现您想要的。
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.
不确定是否可以在不重新启动服务器的情况下动态更改
nginx
配置。如果我有同样的要求,我可能会深入研究
nodejs
和zookeeper
集成。有几个有趣的开源项目:
node-zookeeper 将
nodejs
与 <代码>动物园管理员;node-http-proxy 可用于负载均衡的代理 http 服务器。
当然,它们缺乏成熟度,但它们可能对您来说很有趣。
Not sure that it is possible to dynamicaly change
nginx
configuration without restarting a server.If I had a same requirement I'd probably dug into
nodejs
andzookeeper
integration.There are several interesting opensource projects:
node-zookeeper integrates
nodejs
withzookeeper
;node-http-proxy proxy http server that can be used for load balancing.
Of course they lack maturity but they might be interesting for you.
来自文档:
-s
代表“信号”,其中信号可以是“退出”、“重新加载”、“重新打开”或“停止”。From Docs:
-s
is for 'signal', where signal can be 'quit', 'reload', 'reopen', or 'stop'.