nginx+lua配置

nginx中可以配置
`
-- lua校验配置
location ^~/lua/ {
default_type 'text/html';
access_by_lua_file /usr/local/nginx/conf/access.lua;
}

-- 重定向配置
location ^~/toRedirect/ {
proxy_pass http://目标ip:目标端口/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1000m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

access.lua内容
-- url转义
function encodeURIcomponent(s)
s = ngx.escape_uri(s); -- 转义
-- 按照端口要求替换字符
s = string.gsub(s, "\+", "%20");
s = string.gsub(s, "\%21", "!");
s = string.gsub(s, "\%27", "'");
s = string.gsub(s, "\%29", "(");
s = string.gsub(s, "\%7E", "~");
return s;
end;

local token = ngx.req.get_uri_args()["token"];

if token == nil then
return ngx.exit(403);
end;

local zhttp = require "resty.http";

local httpc = zhttp.new();

-- ip和端口是nginx的,这里需要再通过nginx进行访问
local url = 'http://ip:端口/checkToken?token=' .. token;

local res, err = httpc:request_uri(url, {
keepalive_timeout = 20000 -- 毫秒

});

local starts, ends = string.find(res.body, "200");

if starts == nil then
return ngx.exit(403);
else
-- 转发
local token = encodeURIcomponent(ngx.req.get_uri_args()["token"]);
local targetPath = encodeURIcomponent(ngx.req.get_uri_args()["targetPath"]);
-- ip和端口是nginx的,这里需要再通过nginx进行访问
local tourl = "http://ip:端口/toRedirect?token=".. token.. "&targetPath=".. targetPath;

ngx.redirect(tourl);
end;
`

posted on 2024-09-10 17:03  小七mxs  阅读(6)  评论(0编辑  收藏  举报