-- 获取请求路径
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. LaTeX 插入代码

    LaTeX 插入代码可以使用的宏包有 verbatim.fancyvrb.listings 以及 minted.个人最推荐使用 minted. verbatim verbatim 没有语法高亮功能,只 ...

  2. LLM论文研读: MindSearch

    1. 背景 近日中科大与上海人工智能实验室联合推出的MindSearch思索,引起了不小的关注,github上的星标,短短几周时间,已经飙到了4.2K.看来确实有些内容,因此本qiang~研读了论文及 ...

  3. Unable to tunnel through proxy. Proxy returns "HTTP/1.1 503 Service Unavailable"

    背景: 某日,一正常项目迁移到新的服务器 新的服务器,需要使用代理来访问之前能直接访问的接口,加完代理之后,发现无法获取数据了 报错: org.springframework.web.client.R ...

  4. 搜索组件优化 - Command ⌘K

    前言: DevNow 项目中我们使用了 DocSearch 来实现搜索功能,但是由于有以下的限制: 您的网站必须是技术文档或技术博客. 您必须是网站的所有者,或者至少具有更新其内容的权限 您的网站必须 ...

  5. SQL 求中位值

    题目A median is defined as a number separating the higher half of a data set from the lower half. Quer ...

  6. echarts实现pie自定义标签

    echarts实现pie自定义标签 一.环境 vue + echarts 实现饼图的自定义标签 二.实现效果 三.实现方式 import * as echarts from 'echarts'; ex ...

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

    前言 以前就写过几篇关于反射和表达式树的学习笔记, 但是写的很乱. 最近常用到反射和表达式树, 所以特别写一篇做一个整理吧. 泛型和反射 表达式树 学习笔记 c# 常用反射和表达式树整理 反射在项目中 ...

  8. WPF中的ListBox怎么添加删除按钮并删除所在行

    直接上代码: 第一步:创建测试类 public class BeautifulGirl { public string Name { get; set; } } 第二步:创建viewmodel和数据源 ...

  9. UEFI原理与编程(四)(dec dsc inf文件)

    1 .inf文件 以下面 .inf文件为例 [Defines] # 块用于定义模块的属性和其他变量,块内定义的变量可被其他块引用 INF_VERSION = 0x00010006 #INF 标准的版本 ...

  10. slot原理

    vue提供组件插槽能力, 允许开发者在封装组件时,把不确定的部分定义为插槽 : 作用:再不确定的内容位置占位,在子组件内使用 slot 标签写占位的内容 : 插槽的分类 : 默认插槽: 具名插槽:使用 ...