lua获取请求参数以及在nginx.conf中使用
-- 获取请求路径
local request_uri = ngx.var.request_uri
-- 从 header中取值
local token = ngx.req.get_headers()["token"]
-- 获取cookie中的值
local user_id = ngx.var.cookie_userId
-- 获取args中的值
local user_id = ngx.var.args_参数名
--[[
如:
#尝试访问 /nginx_var?a=hello,world
content_by_lua_block {
ngx.say(ngx.var.arg_a)
}
]]--
-- 获取body参数,ngx.req.get_post_args() 获取到的可能会是这种格式: 注:整体是table类型
{"{\"content\": {\"loginUrl\": \"http:\/\/10.2.3.62:10001\/ia\/oss\/token\"}}":true}
ngx.req.read_body()
local post_args_tab = ngx.req.get_post_args()
local args
for k, _ in pairs(post_args_tab) do
args = core.json.decode(k)
end
下面是一个示例
# 设置纯lua外部函数库的搜索路径(';;'代表默认的路径)
lua_package_path '/foo/bar/?.lua;/blah/?.lua;;';
# 设置用C语言编写的lua外部函数库的搜索路径(也可以使用';;')
lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';
server {
location /lua_content{
#使用default_type确定MIME的类型
default_type 'text/plain';
content_by_lua_block{
ngx.say('Hello,world!')
}
}
location /nginx_var {
#使用default_type确定MIME的类型
default_type 'text/plain';
#尝试访问 /nginx_var?a=hello,world
content_by_lua_block {
ngx.say(ngx.var.arg_a)
}
}
location = /request_body {
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua_blick {
ngx.req.read_body() -- 明确要读取请求的body
local data = ngx.req.get_body_data()
if data then
ngx.say("body data")
ngx.print(data)
return
end
-- body有可能缓存到一个临时文件中
local file = ngx.req.get_body_file()
if file then
ngx.say("body is in file ",file)
else
ngx.say("no body found")
end
}
}
# 在lua中非阻塞IO的子请求
# (当然,一个更好的方式是使用cosockets)
location = /lua {
# 使用default_type来确定MIME的类型
default_type 'text/plain';
content_by_lua_block {
local res = ngx.location.capture("/some_other_location")
if res then
ngx.say("status:",res.status)
ngx.say("body:")
ngx.print(res.body)
end
}
}
location = /foo {
rewrite_by_lua_block {
res = ngx.location.capture("/memc",
{ args = { cmd = "incr",key = ngx.var.uri }}
)
}
proxy_pass http://blah.blah.com
}
location = /mixed {
rewrite_by_lua_file /path/to/rewrite.lua;
access_by_lua_file /path/to/access.lua;
content_by_lua_file /path/to/content.lua;
}
# 在代码路径中使用nginx变量
# 警告:nginx变量中的内容必须被小心的过滤出来
# 否则这里会有严重的安全风险
location ~ ^/app/([-_a-zA-Z0-9/]+) {
set $path $1;
content_by_lua_file /path/to/lua/app/root/$path.lua;
}
location / {
client_max_body_size 100k;
client_body_buffer_size 100k;
access_by_lua_block {
-- 检查客户端的IP地址是否在我们的黑名单中
if ngx.var.remote_addr == "132.5.72.3" then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
-- 检查URI中是否含有不良的词语
if ngx.var.uri and
string.match(nax.var.request_body,"evil")
then
return ngx.redirect("/terms_of_use.html")
end
-- 测试通过
}
# proxy_pass/fastcgi_pass/etc settings
}
}
lua获取请求参数以及在nginx.conf中使用的更多相关文章
- 学习SpringMVC——如何获取请求参数
@RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@CookieValue)!她(@ModelAndView) ...
- springMvc源码学习之:spirngMVC获取请求参数的方法2
@RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他 (@CookieValue)!她(@ModelAndView ...
- request对象常用API 获取请求参数的值 request应用 MVC设计模式
1 request对象常用API 1)表示web浏览器向web服务端的请求 2)url表示访问web应用的完整路径:http://localhost:8080/day06/Demo1 ...
- struts2获取请求参数的三种方式及传递给JSP参数的方式
接上一篇文章 package test; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; impo ...
- 学习SpirngMVC之如何获取请求参数
学习SpringMVC——如何获取请求参数 @RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@Cooki ...
- springMVC(spring)+WebSocket案例(获取请求参数)
开发环境(最低版本):spring 4.0+java7+tomcat7.0.47+sockjs 前端页面要引入: <script src="http://cdn.jsdelivr.ne ...
- Structs2 中拦截器获取请求参数
前言 环境:window 10,JDK 1.7,Tomcat 7 测试代码 package com.szxy.interceptor; import java.util.Map; import jav ...
- ServletRequest HttpServletRequest 请求方法 获取请求参数 请求转发 请求包含 请求转发与重定向区别 获取请求头字段
ServletRequest 基本概念 JavaWeb中的 "Request"对象 实际为 HttpServletRequest 或者 ServletRequest, ...
- spring(spring mvc)整合WebSocket案例(获取请求参数)
开发环境(最低版本):spring 4.0+java7+tomcat7.0.47+sockjs 前端页面要引入: <script src="http://cdn.jsdelivr.ne ...
- JavaScript获取请求参数
<script type="text/javascript"> //获取请求参数 function paramsMap() { var url = window.loc ...
随机推荐
- 【YashanDB知识库】自关联外键插入数据时报错:YAS-02033 foreign key constraint violated parent key not found
问题现象 使用如下的sql语句创建自关联外键表: drop table self_f_key; create table self_f_key(t1 number primary key not nu ...
- Java是值传递还是引用传递,又是怎么体现的
关于Java是值传递还是引用传递,可以从代码层面来实现一下拿到结果 执行下面的代码: public static void main(String[] args) { int num = 10; St ...
- C++: 虚函数,一些可能被忽视的细节
C++: 虚函数,一些可能被忽视的细节 引言:关于C++虚函数,对某些细节的理解不深入,可能导致我们的程序无法按预期结果运行,或是表明我们对其基本原理理解不够透彻.本文详细解答以下几个问题:实现多态, ...
- TS中简单实现一下依赖注入
依赖注入(Dependency Injection,DI)是一种设计模式,主要用于实现控制反转(Inversion of Control,IoC).它通过将对象的依赖关系从内部管理转移到外部容器来解耦 ...
- webpack高版本抽离css样式报错[已解决]
全局安装的webpack版本是5.51.1,webpack-cli是4.9的版本; 本来想用 extract-text-webpack-plugin 的,但是报错了,查了一下文档 发现,已经不支持新版 ...
- echarts的x轴显示不全的解决办法
echarts的x轴显示不全的解决办法 一.背景 当x轴类目较多时,label显示时会自动间隔显示,也就是会隐藏掉中间的label,如下图: 二.解决办法 通过设置 xAxis.axisLabel.i ...
- SQL Server – 树结构 (二叉树, 红黑树, B-树, B+树)
前言 很久以前有学习过各种树结构, 但后来真的没有在实际项目中运用到. 毕竟我主要负责的都是写业务代码. 太上层了 但是忘光光还是很可惜的. 所以久久可以复习一下. 记得概念也好, 帮助思考. 参考: ...
- ASP.NET Core – Filter
介绍 Filter 类似 Middleware,只是它集中在处理 request 的前后, 站 MVC 角度看就是 before 和 after action, 站 Razor Pages 角度就是 ...
- Figma 学习笔记 – Prototype
挺简单的, 只要知道它有什么, 基本上就会用了 监听 Event Type 监听 Callback Action 过度 Animation Frame Scrolling
- 2How To Use Python On A Web Page With Jinja2 - Fla 21:30
# localhost:5000/user/John @app.route("/user/<name>") def user(name): return render_ ...