对请求数据的格式化 html
例如nginx
{body:{}}--->{data:{}}git
执行阶段概念 · OpenResty最佳实践 · 看云 https://www.kancloud.cn/kancloud/openresty-best-practices/50451github
咱们OpenResty作个测试,示例代码以下:web
location /mixed { set_by_lua $a 'ngx.log(ngx.ERR, "set_by_lua")'; rewrite_by_lua 'ngx.log(ngx.ERR, "rewrite_by_lua")'; access_by_lua 'ngx.log(ngx.ERR, "access_by_lua")'; header_filter_by_lua 'ngx.log(ngx.ERR, "header_filter_by_lua")'; body_filter_by_lua 'ngx.log(ngx.ERR, "body_filter_by_lua")'; log_by_lua 'ngx.log(ngx.ERR, "log_by_lua")'; content_by_lua 'ngx.log(ngx.ERR, "content_by_lua")'; }
执行结果日志(截取了一下):spring
set_by_lua rewrite_by_lua access_by_lua content_by_lua header_filter_by_lua body_filter_by_lua log_by_lua
这几个阶段的存在,应该是openresty不一样于其余多数web server编程的最明显特征了。因为nginx把一个会话分红了不少阶段,这样第三方模块就能够根据本身行为,挂载到不一样阶段进行处理达到目的。编程
这样咱们就能够根据咱们的须要,在不一样的阶段直接完成大部分典型处理了。缓存
实际上咱们只使用其中一个阶段content_by_lua,也能够完成全部的处理。但这样作,会让咱们的代码比较臃肿,越到后期愈加难以维护。把咱们的逻辑放在不一样阶段,分工明确,代码独立,后期发力能够有不少有意思的玩法。安全
列举360企业版的一个例子:app
# 明文协议版本
location /mixed { content_by_lua '...'; # 请求处理 } # 加密协议版本 location /mixed { access_by_lua '...'; # 请求加密解码 content_by_lua '...'; # 请求处理,不须要关心通讯协议 body_filter_by_lua '...'; # 应答加密编码 }
内容处理部分都是在content_by_lua阶段完成,初版本API接口开发都是基于明文。为了传输体积、安全等要求,咱们设计了支持压缩、加密的密文协议(上下行),痛点就来了,咱们要更改全部API的入口、出口么?
最后咱们是在access_by_lua完成密文协议解码,body_filter_by_lua完成应答加密编码。如此一来世界都宁静了,咱们没有更改已实现功能的一行代码,只是利用ngx-lua的阶段处理特性,很是优雅的解决了这个问题。
前两天看到春哥的微博,里面说到github的某个应用里面也使用了openresty作了一些东西。发现他们也是利用阶段特性+lua脚本处理了不少用户证书方面的东东。最终在性能、稳定性都十分让人满意。使用者选型很准,不愧是github的工程师。
不一样的阶段,有不一样的处理行为,这是openresty的一大特点。学会他,适应他,会给你打开新的一扇门。这些东西不是openresty自身所创,而是nginx c module对外开放的处理阶段。理解了他,也能更好的理解nginx的设计思惟。
ngx lua模块之ngx.location.capture子请求学习-坏男孩-51CTO博客 https://blog.51cto.com/5ydycm/1900279
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; location / { # any query # default_type text/html; content_by_lua_file lualib/app001.lua; } } } --学习body&args --ngx.location.capture 返回res.status res.body res.header res.truncated ngx.say("First") res = ngx.location.capture("/flumelog",{method=ngx.HTTP_GET,body="name=zzj&age=33&name=badboy",args={arg_a=2,arg_b=3}}) for key,val in pairs(res) do if type(val) == "table" then ngx.say(key,"=>",table.concat(val,",")) else ngx.say(key,"=>",val) end end ngx.exit(123789) # curl http://localhost:8080/flumelog?name=zj&age=3&name=ba
2019/05/25 13:17:13 [error] 9063#0: *4 attempt to set status 123789 via ngx.exit after sending out the response status 200, client: 127.0.0.1, server: , request: "GET /flumelog?name=zj HTTP/1.1", host: "localhost:8080"
[root@flink logs]# tail error.log
Spring Cloud Gateway https://spring.io/projects/spring-cloud-gateway
Spring Cloud Gateway features:
Built on Spring Framework 5, Project Reactor and Spring Boot 2.0
Able to match routes on any request attribute.
Predicates and filters are specific to routes.
Hystrix Circuit Breaker integration.
Spring Cloud DiscoveryClient integration
Easy to write Predicates and Filters
Request Rate Limiting
Path Rewriting