-- 获取请求路径
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. 专业级语义搜索优化:利用 Cohere AI、BGE Re-Ranker 及 Jina Reranker 实现精准结果重排

    专业级语义搜索优化:利用 Cohere AI.BGE Re-Ranker 及 Jina Reranker 实现精准结果重排 1. 简介 1.1 RAG 在说重排工具之前,我们要先了解一下 RAG. 检 ...

  2. 【YashanDB数据库】YAS-02024 lock wait timeout, wait time 0 milliseconds

    [标题]错误码处理 [问题分类]锁等待超时 [关键字]YAS-02024 [问题描述]执行语句时候,因锁等待超时执行语句失败 [问题原因分析]数据库默认锁等待时间为0秒,如果执行语句存在锁等待过长会执 ...

  3. pyinstaller 打包成 exe

    生成 spec 和可执行文件: pyinstaller --onefile your_file.py 如果需要修改生成的spec文件,则后续可以这样运行: pyinstaller -F your_fi ...

  4. 动态规划——详解leetcode518 零钱兑换 II

    动态规划 零钱兑换 II 参考书目:<程序员代码面试指南:IT名企算法与数据结构题目最优解> 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币 ...

  5. Failed to convert value of type 'java.lang.String' to required type

    DEBUG 微信小程序Java后台 Failed to convert value of type 'java.lang.String' to required type 产生这种条件的原因一般是使用 ...

  6. 合合信息参编“生成式人工智能个人信息保护技术要求系列标准”,助力AI行业可信发展

    生成式人工智能作为新一轮的技术革命成果,在赋能千行百业,给经济社会发展带来新机遇的同时,也产生了个人信息泄露.数据安全风险等问题.在此背景下,中国信息通信研究院(简称"中国信通院" ...

  7. Nodejs+npm详细安装

    Nodejs详细安装步骤1.去官网下载nodejs,下载地址:https://nodejs.org/en/download/ 根据自己要求下载,我这里下载的是windows installer的. 2 ...

  8. idea运行java项目main方法报build failure错误的解决方法

    当在使用 IntelliJ IDEA 运行 Java 项目的 main 方法时遇到 "Build Failure" 错误,这通常意味着在项目的构建过程中遇到了问题.解决这类问题通常 ...

  9. 各位Oracle DBA们,你们期待的在线实训环境终于来了

    各位Oracle DBA,你们是否曾在学习.工作时遇到不得不上正式库测试的情况,如果没事发生那一切岁月静好,但若一不小心删了用户.删了表甚至删了库,可就是悲剧了--从做出开库测试这个决定起,胆战心惊就 ...

  10. C#获取环境变量的值

    Environment.GetEnvironmentVariable("Path"); 修改环境变量之后,不能立即生效,需要重启一下VStudio 或重启电脑 : 在 vstudi ...