热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Nginx服务器中使用lua获取get或post参数

使用ngx_lua模块(http://wiki.nginx.org/HttpLuaModule):localrequest_methodngx.var.request_methodlocalargsnillocalparamnillocalparam2nil--获取参数的值ifGETrequest_methodthen
使用ngx_lua模块(http://wiki.nginx.org/HttpLuaModule):
local request_method = ngx.var.request_method
local args = nil
local param = nil
local param2 = nil
--获取参数的值
if "GET" == request_method then
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end
param = args["param"]
param2 = args["param2"]
升级版(能处理content-type=multipart/form-data的表单):
local function explode ( _str,seperator )
        local pos, arr = 0, {}
                for st, sp in function() return string.find( _str, seperator, pos, true ) end do
                        table.insert( arr, string.sub( _str, pos, st-1 ) )
                        pos = sp + 1
                end
        table.insert( arr, string.sub( _str, pos ) )
        return arr
end
local args = {}
local file_args = {}
local is_have_file_param = false
local function init_form_args()
        local receive_headers = ngx.req.get_headers()
        local request_method = ngx.var.request_method
        if "GET" == request_method then
                args = ngx.req.get_uri_args()
        elseif "POST" == request_method then
                ngx.req.read_body()
                if string.sub(receive_headers["content-type"],1,20) == "multipart/form-data;" then--判断是否是multipart/form-data类型的表单
                        is_have_file_param = true
                        content_type = receive_headers["content-type"]
                        body_data = ngx.req.get_body_data()--body_data可是符合http协议的请求体,不是普通的字符串
                        --请求体的size大于nginx配置里的client_body_buffer_size,则会导致请求体被缓冲到磁盘临时文件里,client_body_buffer_size默认是8k或者16k
                        if not body_data then
                                local datafile = ngx.req.get_body_file()
                                if not datafile then
                                        error_code = 1
                                        error_msg = "no request body found"
                                else
                                        local fh, err = io.open(datafile, "r")
                                        if not fh then
                                                error_code = 2
                                                error_msg = "failed to open " .. tostring(datafile) .. "for reading: " .. tostring(err)
                                        else
                                                fh:seek("set")
                                                body_data = fh:read("*a")
                                                fh:close()
                                                if body_data == "" then
                                                        error_code = 3
                                                        error_msg = "request body is empty"
                                                end
                                        end
                                end
                        end
                        local new_body_data = {}
                        --确保取到请求体的数据
                        if not error_code then
                                local boundary = "--" .. string.sub(receive_headers["content-type"],31)
                                local body_data_table = explode(tostring(body_data),boundary)
                                local first_string = table.remove(body_data_table,1)
                                local last_string = table.remove(body_data_table)
                                for i,v in ipairs(body_data_table) do
                                        local start_pos,end_pos,capture,capture2 = string.find(v,'Content%-Disposition: form%-data; name="(.+)"; filename="(.*)"')
                                        if not start_pos then--普通参数
                                                local t = explode(v,"\r\n\r\n")
                                                local temp_param_name = string.sub(t[1],41,-2)
                                                local temp_param_value = string.sub(t[2],1,-3)
                                                args[temp_param_name] = temp_param_value
                                        else--文件类型的参数,capture是参数名称,capture2是文件名
                                                file_args[capture] = capture2
                                                table.insert(new_body_data,v)
                                        end
                                end
                                table.insert(new_body_data,1,first_string)
                                table.insert(new_body_data,last_string)
                                --去掉app_key,app_secret等几个参数,把业务级别的参数传给内部的API
                                body_data = table.concat(new_body_data,boundary)--body_data可是符合http协议的请求体,不是普通的字符串
                        end
                else
                        args = ngx.req.get_post_args()
                end
        end
end

推荐阅读
  • 31.项目部署
    目录1一些概念1.1项目部署1.2WSGI1.3uWSGI1.4Nginx2安装环境与迁移项目2.1项目内容2.2项目配置2.2.1DEBUG2.2.2STAT ... [详细]
  • 解决php错误信息不显示在浏览器上的方法
    本文介绍了解决php错误信息不显示在浏览器上的方法。作者发现php中的各种错误信息并不显示在浏览器上,而是需要在日志文件中查看。为了解决这个问题,作者提供了一种解决方式:通过修改php.ini文件中的display_errors参数为On,并重启服务。这样就可以在浏览器上直接显示php错误信息了。 ... [详细]
  • svnWebUI:一款现代化的svn服务端管理软件
    svnWebUI是一款图形化管理服务端Subversion的配置工具,适用于非程序员使用。它解决了svn用户和权限配置繁琐且不便的问题,提供了现代化的web界面,让svn服务端管理变得轻松。演示地址:http://svn.nginxwebui.cn:6060。 ... [详细]
  • 深入解析Linux下的I/O多路转接epoll技术
    本文深入解析了Linux下的I/O多路转接epoll技术,介绍了select和poll函数的问题,以及epoll函数的设计和优点。同时讲解了epoll函数的使用方法,包括epoll_create和epoll_ctl两个系统调用。 ... [详细]
  • LVS实现负载均衡的原理LVS负载均衡负载均衡集群是LoadBalance集群。是一种将网络上的访问流量分布于各个节点,以降低服务器压力,更好的向客户端 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
  • 本文介绍了在无法联网的情况下,通过下载rpm包离线安装zip和unzip的方法。详细介绍了如何搜索并下载合适的rpm包,以及如何使用rpm命令进行安装。 ... [详细]
  • 数据恢复原理实验及工具使用
    本文主要介绍了数据恢复原理实验相关的知识,包括实验目的、实验内容和步骤。通过实验,可以了解数据存储机制,掌握基本的数据灾难备份和恢复工具,并了解信息隐藏与检测相关知识。实验中使用的工具包括Winhex和Final data。同时,还提供了一些分析与思考的问题和心得体会。 ... [详细]
  • Dockerfile构建镜像的指令和说明
    本文介绍了Dockerfile是用来构建镜像的文本文件,其中包含了构建镜像所需的指令和说明。通过创建一个Dockerfile文件并编写内容,可以快速创建自定义的镜像。文章还提供了一个示例,展示了如何使用Dockerfile创建一个本地构建的nginx镜像,并通过docker images命令查看镜像的版本。希望本文对大家的学习有所帮助,并希望大家多多支持编程笔记。 ... [详细]
  • 负载均衡_Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分
    nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • windows用什么nginx,iis不香么 ... [详细]
  • nginx+多个tomcat
    学习nginx的时候遇到的问题:nginx怎么部署两台tomcat?upstream在网上找的资源,我在nginx配置文件(nginx.conf)中添加了两个server。结果只显 ... [详细]
  • ZABBIX 3.0 配置监控NGINX性能【OK】
    1.在agent端查看配置:nginx-V查看编辑时是否加入状态监控模块:--with-http_stub_status_module--with-http_gzip_stat ... [详细]
  • Nginx Buffer 机制引发的下载故障
    Nginx ... [详细]
author-avatar
新闻联播有没有大结局__742
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有