nginxlua文件服务器权限,通过lua进行nginx的权限控制
nginx_lua的安裝
nginx使用luajit進行編譯安裝
使用openresty進行yum安裝
openresty中將lua和nginx進行封裝,詳情可查看openresty官網
openresty相關啟動命令service openresty start
ngx_lua的相關api使用說明及相關使用
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 Forbidden
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: 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 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: 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 Error
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Date: Thu, 01 Mar 2018 10:07:03 GMT
Connection: 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 then
ngx.exec("@hello")
elseif res.status == ngx.HTTP_OK then
ngx.exec("@/")
elseif res.status == ngx.HTTP_INTERNAL_SERVER_ERROR then
ngx.exec("@servererror")
else
ngx.exec("@error")
end
‘;}
access_by_lua因為要實現權限控制,所以只能選擇access_by_lua,而不能使用content_by_lua
nginx中的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:8080
此處向接口url傳遞兩個參數,因為要傳入變量,所以要以{args={ }}的形式來完成。如果使用httpc.request_uri方法的話,應該可以使用lua的..拼接符進行變量與uri的拼接,有興趣的同學可以自行測試
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判斷使用elseif
lua中if完整的語句為 if..else..end
將四種結果均返回進行執行,ngx.exec表示執行后面的location,@hello 中的 @表示nginx內部的傳遞,不會進行外部的跳轉
完整的ngx_lua配制實例
[root@10-110-157-48 conf.d]# cat tableau.conf
upstream backend_lebiTableau {
server 10.110.150.217;
keepalive 100;
}
upstream matrix_proxy_backend {
server 10.58.91.84:8080;
keepalive 100;
}
server {
listen 82;
server_name lebi.letv.cn;
resolver 10.110.220.231;
access_log /tmp/lebitableau.a.log main;
error_log /tmp/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_pass http://backend_lebiTableau;
proxy_send_timeout 18000;
proxy_read_timeout 18000;
proxy_next_upstream error timeout invalid_header http_500;
proxy_connect_timeout 20;
}
location ~ /matrix_proxy/(.*) {
internal;
proxy_pass http://matrix_proxy_backend/$1$is_args$args;
}
location @/ {
proxy_pass http://backend_lebiTableau;
}
location @error {
return 403;
}
location @servererror {
default_type ‘text/plain‘;
content_by_lua ‘ngx.say("server error")‘;
}
location @hello {
rewrite ^(.*)$ http://matrix.lebi.letv.cn/#/error break; ##沒有權限的用戶全部rewrite至系統錯誤頁面
}
location @ok {
default_type ‘text/plain‘;
content_by_lua ‘ngx.say("authorized ok, cookie=", ngx.var.cookie_78bdfe11ce353909cb210160a76c330b)‘;
}
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 then
ngx.exec("@hello")
elseif res.status == ngx.HTTP_OK then
ngx.exec("@/")
elseif res.status == ngx.HTTP_INTERNAL_SERVER_ERROR then
ngx.exec("@servererror")
else
ngx.exec("@error")
end
‘;}
}
總結
以上是生活随笔為你收集整理的nginxlua文件服务器权限,通过lua进行nginx的权限控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机支付需厘清六大关键问题
- 下一篇: 360网络修复工具_为什么大家都在骂36