[英]Does nginx have a Routing API?
I am trying to see if nginx (OSS, not commercial) can be used not only as a load balancer, but as a network router/switch in the event that I need to shut my app down and redirect traffic to a CDN/static page, etc. 我试图看看nginx(OSS,不是商业)不仅可以用作负载均衡器,还可以用作网络路由器/交换机,如果我需要关闭我的应用并将流量重定向到CDN /静态页面等
I was hoping to find a REST API that might allow me to configure routing rules on the fly but alas, I see nothing. 我希望找到一个可以允许我动态配置路由规则的REST API,但是,我什么都没看到。
Does nginx provide this functionality out of the box? nginx是否提供开箱即用的功能? Or can I pair it with something that does? 或者我可以将它配对吗? This would be a Java app it is balancing, and I see there is an nginx-clojure module. 这将是一个平衡的Java应用程序,我看到有一个nginx-clojure模块。 So perhaps I could expose a REST endpoint through Java (running on the nginx server) somehow...thoughts? 所以也许我可以通过Java(在nginx服务器上运行)以某种方式暴露一个REST端点......想法?
If you use nginx-clojure to do such work you need 如果你使用nginx-clojure来完成你需要的工作
eg 例如
## InitHandler is used to initialize shared map
jvm_init_handler_name my.InitHandler;
upstream myApp {
.....
}
## requests will be redirected to upstream staticBackend when my app is down
upstream staticBackend {
....
}
server {
....
## share state/rules among nginx worker processes
shared_map routerRules tinymap?space=32k&entries=256;
## $mybackend will be changed by rewrite handler MyRouter
set $mybackend "";
location / {
rewrite_handler_type java;
rewrite_handler_name my.MyRouter;
proxy_pass http://$mybackend;
}
location /restapi {
content_handler_type java;
content_handler_name my.MyRouterApi;
}
}
public class InitHandler implements NginxJavaRingHandler {
public static NginxSharedHashMap<String, String> rules;
public Object[] invoke(Map<String, String> fakeRequest) {
rules = NginxSharedHashMap.build("routerRules");
}
}
public class MyRouter implements NginxJavaRingHandler {
public Object[] invoke(Map<String, String> req) {
String backend = InitHandler.rules.get("mybackend");
if (backend == null) {
backend = "myApp";
}
((NginxJavaRequest)req).setVariable("mybackend", backend);
return nginx.clojure.java.Constants.PHASE_DONE;
}
}
public class MyRouterApi implements NginxJavaRingHandler {
public Object[] invoke(Map<String, String> req) {
String backend = req.get(MiniConstants.QUERY_STRING);
/*chek backend ...... */
//update the entry whose key is "mybackend" in the shared map
InitHandler.rules.put("mybackend", backend);
return new Object[] {200, null, "OK"};
}
}
More docs can be found from https://nginx-clojure.github.io/ . 可以从https://nginx-clojure.github.io/找到更多文档。
BTW the embedding API will make dev/testing with nginx-clojure quite easy. BTW 嵌入API将使用nginx-clojure进行开发/测试非常容易。
There is no API out of the box. 没有开箱即用的API。 You have to write some endpoint-snippets and use variables in your upstream config. 您必须编写一些端点片段并在上游配置中使用变量。
Take a look at: 看一眼:
https://github.com/openresty/lua-nginx-module#readme https://github.com/openresty/lua-nginx-module#readme
It's fairly simple. 这很简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.