-- 获取请求路径
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中使用的更多相关文章

  1. 学习SpringMVC——如何获取请求参数

    @RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@CookieValue)!她(@ModelAndView) ...

  2. springMvc源码学习之:spirngMVC获取请求参数的方法2

    @RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他 (@CookieValue)!她(@ModelAndView ...

  3. request对象常用API 获取请求参数的值 request应用 MVC设计模式

    1 request对象常用API   1)表示web浏览器向web服务端的请求   2)url表示访问web应用的完整路径:http://localhost:8080/day06/Demo1     ...

  4. struts2获取请求参数的三种方式及传递给JSP参数的方式

    接上一篇文章 package test; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; impo ...

  5. 学习SpirngMVC之如何获取请求参数

    学习SpringMVC——如何获取请求参数   @RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@Cooki ...

  6. springMVC(spring)+WebSocket案例(获取请求参数)

    开发环境(最低版本):spring 4.0+java7+tomcat7.0.47+sockjs 前端页面要引入: <script src="http://cdn.jsdelivr.ne ...

  7. Structs2 中拦截器获取请求参数

    前言 环境:window 10,JDK 1.7,Tomcat 7 测试代码 package com.szxy.interceptor; import java.util.Map; import jav ...

  8. ServletRequest HttpServletRequest 请求方法 获取请求参数 请求转发 请求包含 请求转发与重定向区别 获取请求头字段

      ServletRequest 基本概念 JavaWeb中的 "Request"对象  实际为   HttpServletRequest  或者  ServletRequest, ...

  9. spring(spring mvc)整合WebSocket案例(获取请求参数)

    开发环境(最低版本):spring 4.0+java7+tomcat7.0.47+sockjs 前端页面要引入: <script src="http://cdn.jsdelivr.ne ...

  10. JavaScript获取请求参数

    <script type="text/javascript"> //获取请求参数 function paramsMap() { var url = window.loc ...

随机推荐

  1. Web 国际化:新增越南语语系(vue i18n)

    前提: 1. 在src/locales文件夹中,新增vi.json文件 背景: 1. vue 步骤: 1. 在main.js中, import VueI18n from 'vue-i18n' Vue. ...

  2. 【YashanDB知识库】EXP导致主机卡死问题

    问题现象 问题单:exp导出全库1主2备主节点执行,DMP文件30G左右系统卡死,发生主备切换 现象: exp sys/Cod-2022 file=bim20240402.dmp full=y 服务器 ...

  3. 小tips:xml文件转为html表格展示示例

    books.xml文件格式如下: <?xml version="1.0" encoding="UTF-8"?> <xbrl xmlns=&qu ...

  4. ASP.NET Core C# 反射 & 表达式树 (第三篇)

    前言 前一篇讲完了反射, 这一篇来讲一下和反射息息相关的表达式树. 首先搞清楚 Delegate, Action, Func, Anonymous Method, Lambda, Expression ...

  5. 初步认识uboot

    1. uboot下载地址 ftp://ftp.denx.de/pub/u-boot/

  6. 用C#写个PDF批量合并工具简化日常工作

    一. 前言 由于项目需要编写大量的材料,以及各种签字表格.文书等,最后以PDF作为材料交付的文档格式,过程文档时有变化或补充,故此处理PDF文档已经成为日常工作的一部分. 网上有各种PDF处理工具,总 ...

  7. [Tkey] [IOI 2018] werewolf

    注意看,我耗时五个小时 AK 了 IOI 题意 给你一个图,每次给定若干询问 \((s,t,l,r)\),请你完成下述要求: 定义 \(S\) 为到 \(s\) 的最短路径不小于 \(l\) 的点构成 ...

  8. 在Vue3中如何实现四种全局状态数据的统一管理?

    四种全局状态数据 在实际开发当中,会遇到四种全局状态数据:异步数据(一般来自服务端).同步数据.同步数据又分为三种:localstorage.cookie.内存.在传统的 Vue3 当中,分别采用不同 ...

  9. element表单校验 【登录】

    async btnOK () { // 1. 表单校验 (调用表单的校验方法获取校验结果) // this.$refs.formRef.validate(function (valid) { // c ...

  10. 多校A层冲刺NOIP2024模拟赛04

    T1.02表示法 竟然有出题人敢出高精度(其实只是一个把string转成01串),开场看出记搜后十分犹豫到底要不要写高精,徘徊很久还是写了个小高精. 码( /* GGrun */ #include&l ...