openresty干系启动命令service openresty start
ngx_lua的干系api利用解释及干系利用ngx_lua github地址lua基本语法openrestry最佳实践ngx_lua的日常利用场景ngx_lua的实行顺序,可以看这张图
通过nginx直接进行一些值的显示,此处用到的一样平常是content_by_lua模块,lua 1.9.5版本中是content_by_lua_block通过nginx作访问权限掌握,包括重写等,不过nginx也可以直接重写ngx_lua的实例业务场景
老板哀求访问一个url时进行用户时作权限掌握,有权限者可以查看url,无权限者则直接返回缺点
个中开拓职员写了一个接口,能通过传入的两个参数(报表名和用户名),返回对应的值
个中实现过程如下
1.上岸入系统lebi.letv.cn中2.用户须要访问报表链接,个中报表链接均为http://xxx/views/xxx模式3.访问报表时,nginx先通过lua进行掌握,先向开拓职员供应的接口http://10.58.91.84:8080/m/api/permission/getSchedulePermission通报报表名和用户名,个中报表名从报表访问链接中获取,用户名从cookie中获取4.ngx_lua掌握访问要求,同时作干系的处理
开拓接口返回值解释
开拓接口返回值设置三种:
http状态码403为没权限http状态码200为通过验证http状态码500为做事缺点
干系curl测试状态如下
http 403:[root@10-110-157-48 conf.d]# curl -i 'http://10.58.91.84:8080/m/api/permission/getSchedulePermission?username=marility&url=http://a/b/c'HTTP/1.1 403 ForbiddenServer: Apache-Coyote/1.1Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Thu, 01 Mar 2018 08:26:05 GMT{"success":false,"errorMsg":"没有权限,请联系管理员"}http 200:[root@10-110-157-48 conf.d]# curl -i 'http://10.58.91.84:8080/m/api/permission/getSchedulePermission?username=letv&url=http://a/b/c'HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Thu, 01 Mar 2018 09:45:24 GMT{"Msg":"有权限查看","success"}http 500:[root@10-110-157-48 conf.d]# curl -i 'http://10.58.91.84:8080/m/api/permission/getSchedulePermission?username=letv&url='HTTP/1.1 500 Internal Server ErrorServer: Apache-Coyote/1.1Content-Type: application/json;charset=utf-8Transfer-Encoding: chunkedDate: Thu, 01 Mar 2018 10:07:03 GMTConnection: close{"errorMsg":"java.lang.ArrayIndexOutOfBoundsException: 2","success":false}
以上测试中url中直接传入测试url=http://a/b/c, 实际中该当动态传入访问报表的链接
ngx_lua中的掌握location ~ ^/views {access_by_lua 'local res = ngx.location.capture("/matrix_proxy/m/api/permission/getSchedulePermission", {args={username=ngx.var.cookie_example , url=ngx.var.request_uri}})if res.status == ngx.HTTP_FORBIDDEN thenngx.exec("@hello")elseif res.status == ngx.HTTP_OK thenngx.exec("@/")elseif res.status == ngx.HTTP_INTERNAL_SERVER_ERROR thenngx.exec("@servererror")elsengx.exec("@error")end';}
access_by_lua由于要实现权限掌握,以是只能选择access_by_lua,而不能利用content_by_luanginx中的lua全文以单引号' '进行席卷local res, lua中利用local定义一个变量res向一个接口发送参数,有两种方法,一种是利用ngx.location.capture方法,其余一种是httpc:request_uri,httpc.request_uri为openresty的第三方模块。 httpc.request_url的api利用解释 , openresty加载第三方模块解释 , 本例中利用capture方法ngx.location.capture方法不能象httpc:request_uri方法一样直接传入url,而只能是$request_uri,以是此处前辈行一层的/matrix_proxy/的封装,而/matrix_proxy通过pass_proxy,将要求反代至接口的做事器ip 10.58.91.84:8080ngx.location.capture api的利用解释此处向接口url通报两个参数,由于要传入变量,以是要以{args={ }}的形式来完成。如果利用httpc.request_uri方法的话,该当可以利用lua的..拼接符进行变量与uri的拼接,有兴趣的同学可以自行测试lua的http状态码与ngx_lua状态对应表ngx.var.cookie_COOKIE_KEY 获取用户的cookie的value值,上实例中cookie的key为example。ngx.var.request_uri 获取nginx中的$request_uri值从api解释中可以看到ngx.location.capture有4个slots,个中一个是res.status判断res.status的结果与http状态码是否相等,lua中即是判断利用==lua中多种if判断利用elseiflua中if完全的语句为 if..else..end将四种结果均返回进行实行,ngx.exec表示实行后面的location,@hello 中的 @表示nginx内部的通报,不会进行外部的跳转完全的ngx_lua配置实例
[root@vm-10-112-42-12 conf.d]# cat matrix-tu.confupstream matrix.lebi.letv.cn {ip_hash;server 10.112.42.140:8080;server 10.112.42.141:8080;server 10.112.42.142:8080; keepalive 100;}upstream matrix-tu.lebi.letv.cn { server 10.110.150.217; keepalive 100;}server { listen80; server_name matrix-tu.lebi.letv.cn; access_log /letv/nginx/logs/lebitableau.a.log main; error_log /letv/nginx/logs/lebitableau.error.log; location = /favicon.ico { log_not_found off; log_subrequest off; } location / { proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_passhttp://matrix-tu.lebi.letv.cn; proxy_send_timeout18000; proxy_read_timeout18000; proxy_next_upstreamerror timeout invalid_header http_500; proxy_connect_timeout 20; }location = / {return 403;}location ~ /authoring/ {return 403;}location @/ { proxy_passhttp://matrix-tu.lebi.letv.cn;} location ~ /matrix_proxy/(.) { internal; proxy_pass http://matrix.lebi.letv.cn/$1$is_args$args; } location /m/resource/tableauLogin { proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_pass http://matrix.lebi.letv.cn; proxy_send_timeout 18000; proxy_read_timeout 18000; proxy_next_upstream error timeout invalid_header http_500; proxy_connect_timeout 20;add_header Access-Control-Allow-Origin "http://matrix.lebi.letv.cn"; add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"; add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";add_header Access-Control-Allow-Credentials true; }location @error { default_type 'text/plain'; content_by_lua 'ngx.say("lua error")';}location ~ /wenz_img.png$ {root /etc/nginx/forbidden;}location ~ /beierx_img.png$ {root /etc/nginx/forbidden;}location ~ /error_bg.png$ {root /etc/nginx/forbidden;}location @servererror {default_type 'text/plain';content_by_lua 'ngx.say("server error")';}location @forbidden {return 403;}location @nousername {rewrite ^(.)$ http://matrix.lebi.letv.cn/#/index break; ##所有页面跳转到登录页面}#proxy_intercept_errors on;error_page 403 /403.html;location = /403.html {root /etc/nginx/forbidden;#internal;}proxy_intercept_errors on; ##加入将http反代缺点拦截并以nginx的缺点状态码显示error_page 404 /404.html; ##自定义nginx的404缺点显示页面location = /404.html {root /etc/nginx/forbidden;}location @ok {default_type 'text/plain';content_by_lua 'ngx.say("authorized ok, cookie=", ngx.var.cookie_78bdfe11ce353909cb210160a76c330b)';}location ~ ^/views/ {#default_type 'text/plain';access_by_lua '--local cookie_value = ngx.var.cookie_78bdfe11ce353909cb210160a76c330b--ngx.say("cookie= ", cookie_value)local res = ngx.var.cookie_78bdfe11ce353909cb210160a76c330bif not res thenngx.exec("@nousername") ##判断是否能获取到用户的cookie,如果不能获取,则直接实行nousername规则,进行用户登录页面的跳转elselocal res = ngx.location.capture("/matrix_proxy/m/api/permission/getSchedulePermission", {args={username=ngx.var.cookie_78bdfe11ce353909cb210160a76c330b , url=ngx.var.request_uri}})if res.status == ngx.HTTP_FORBIDDEN thenngx.exec("@forbidden")elseif res.status == ngx.HTTP_OK thenngx.exec("@/")elseif res.status == ngx.HTTP_INTERNAL_SERVER_ERROR thenngx.exec("@servererror")elsengx.exec("@error")endend';}}