文章目录
nginx配置参数
proxy_pass https: //yuntongapi.websaas.cn;
proxy_set_header Host "yuntongapi.websaas.cn";
proxy_ssl_name "yuntongapi.websaas.cn";
proxy_ssl_server_name on;
常用函数总结
参考链接:Nginx API for Lua
1、ngx.req.get_headers()
获取请求头函数
local headers = ngx.req.get_headers()
local hedhost = headers["Hostpre"]
headers["Host"]
headers["Referer"]
2、ngx.req.set_header()
ngx.req.set_header("Content-Type", "text/css")
ngx.req.set_header("Foo", {"a", "abc"})
-------------------------------------------
ngx.req.set_header("X-Foo", nil)
is equivalent to
ngx.req.clear_header("X-Foo")
--------------------------------------------
3、ngx.req.set_uri()
重写url函数,注意: url中存在# 时使用 ngx.redirect()
local url = "/login/test"
ngx.req.set_uri(lgurl)
4、ngx.req.set_uri_args()
设置 请求参数函数
ngx.req.set_uri_args("a=test")
ngx.req.set_uri_args("a=3&b=hello%20world")
ngx.req.set_uri_args({ a = 3, b = "hello world" })
5、ngx.redirect()
301、302 跳转函数
local newUrl = "https://www.baidu.com"
ngx.redirect(newUrl)
6、ngx.log()
打印nginx的日志
ngx.log(ngx.ERR,"log is ", ngx.var.uri)
log-level: ngx.STDERR
ngx.EMERG
ngx.ALERT
ngx.CRIT
ngx.ERR
ngx.WARN
ngx.NOTICE
ngx.INFO
ngx.DEBUG
7、ngx.resp.get_headers()
获取响应头的信息
local resheaders = ngx.resp.get_headers()
local loc = resheaders["Location"]
8、ngx.header.HEADER
设置响应头信息
ngx.header['Set-Cookie'] = {'a=32; path=/', 'b=4; path=/'}
will yield
Set-Cookie: a=32; path=/
Set-Cookie: b=4; path=/
-- equivalent to ngx.header["Content-Type"] = 'text/plain'
ngx.header.content_type = 'text/plain'
ngx.header["X-My-Header"] = 'blah blah'
--------------------------------------------
ngx.header.content_type = {'a', 'b'}
is equivalent to
ngx.header.content_type = 'b'
----------------------------------------------
ngx.header["X-My-Header"] = nil
The same applies to assigning an empty table:
ngx.header["X-My-Header"] = {}
---------------------------------------------