token一般用于身份驗證流程中。而實現nginx的token驗證通常可通過nginx的模塊化開發或通過基于nginx+lua的openresty來實現。本文將介紹兩種方式來實現基于nginx的靜態token驗證。
一、基于nginx+lua的openresty來實現
1、安裝lua-nginx-module模塊
wget //github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
?
tar zxvf v0.10.13.tar.gz
注意安裝lua-nginx-module模塊后不需要編譯,解壓即可
2、安裝LuaJIT
wget //luajit.org/download/LuaJIT-2.0.5.tar.gz
?
tar -zxvf LuaJIT-2.0.5.tar.gz
?
cd LuaJIT-2.0.5
?
make install perfix=/usr/local/LuaJIT
若安裝成功,則提示
==== Successfully installed LuaJIT 2.0.5 to /usr/local/LuaJIT ====
在安裝成功后,需要在解壓文件中的/etc/profile路徑中修改profile文件
在文件最后加上環境變量即可:
export LUAJIT_LIB=/usr/local/LuaJIT/lib
?
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
修改環境變量,并保存后執行命令:
source /etc/profile
3、安裝ngx_devel_kit
與安裝lua-nginx-module模塊相同,下載后解壓即可,無需安裝:
cd /opt/
?
wget //github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
?
tar zxvf v0.3.0.tar.gz
4、直接安裝openresty
在終端執行下面 3 條命令把 OpenResty 安裝到 /usr/local/openresty
sudo yum install yum-utils
?
sudo yum-config-manager --add-repo //openresty.org/package/centos/openresty.repo
?
sudo yum install openresty
-
Nginx 的配置文件位于 /usr/local/openresty/nginx/conf/nginx.conf (openresty -V 中沒有指定)
-
啟動 Nginx,2 種啟動方式都可以
-
sudo openresty
-
sudo nginx
-
查看是否啟動了 nginx: ps -ef | grep nginx
-
-
測試是否支持 Lua: 參考上面的方法
5、編譯nginx
本文默認已經安裝了Nginx,所以在編譯時(./configure),只追加以下新參數:
--with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-module=/root/test_for_token/ngx_devel_kit-0.3.0 --add-module=/root/test_for_token/lua-nginx-module-0.10.13
編譯nginx的同時,分別將已下載的lua-nginx-module模塊、LuaJIT模塊、ngx_devel_kit模塊增加進去。隨后進行編譯安裝:
$ make -j2
$ make install
6、測試lua-nginx-module
在nginx.conf文件中的server代碼塊里加入如下代碼:
location /hello_lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua!")';
}
重啟nginx后,通過curl進行測試:
curl //127.0.0.1/hello_lua
若結果為hello, lua! 則安裝成功。
7、在nginx.conf文件中配置靜態token
location /a/b/c {
access_by_lua '
local args = ngx.req.get_headers(); --獲取請求中 header 頭的列表.
local token1 = args["token"]; --取出 header 頭中key為 token 的值.
local token2 = "123456"; --定義一個局部變量.
local errs = "Only Authorized Request will be Processe" --定義錯誤提示消息
local errs2 = "The token value is incorrect."
if not token1 then
ngx.status = ngx.HTTP_FORBIDDEN --返回錯誤碼
ngx.say(errs) --返回錯誤消息
ngx.exit(200) -- 跟以上兩個連用,固定寫法
end
if token1 ~= token2 then
ngx.status = ngx.HTTP_FORBIDDEN
ngx.say(errs2)
ngx.exit(200)
else
return
end
';
proxy_pass XXXX;
}
重啟nginx后,在發送http請求時,需要URI中添加token=123456,進行校驗后nginx才可進行請求處理。
二、基于nginx模塊化開發實現token驗證
基于nginx模塊化開發實現靜態token驗證的基本思路是通過在nginx.conf配置文件中的server模塊添加參數,如:
server {
listen 80;
server_name test;
token_key token_value;
}
并通過ngx_command_t中的ngx_http_set_token_key(根據不同需求提供讀取配置函數),對配置文件nginx.conf中的靜態的token值進行讀取,如下所示:
{ ngx_string("token_key"),
NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_http_set_token_key,
NGX_HTTP_SRV_CONF_OFFSET,
0,
NULL },
每當獲得http請求時,通過對ngx_http_core_loc_conf_t模塊化開發的handler中,對比請求參數token_key的值。若無該參數,或者該參數的值與nginx.conf文件中的值不匹配,則返回 NGX_HTTP_FORBIDDEN,即返回403,提示禁止訪問,具體實現如下:
if(NGX_OK != ngx_http_arg(r,(u_char *)"token_key", 9, &token_key)){
ngx_log_error(NGX_LOG_ERR,/*log*/, 0,
"Only token Request will be Processe");
return NGX_HTTP_FORBIDDEN;
}else if (ngx_strncmp(token_key.data,/*配置文件中的token_value*/, /*token_value長度*/) != 0)
{
ngx_log_error(NGX_LOG_ERR, /*log*/, 0,"token check failed");
return NGX_HTTP_FORBIDDEN;
}