OpenResty 操作cookies
在or中简单的使用cookies 复杂的操作请使用
[lua_resty_cookies](https://github.com/cloudflare/lua-resty-cookie)
基本操作
- 获取一个cookies, cookies的name叫做session
local cookie_name = "cookie_session"
ngx.say(ngx.var[cookie_name])
- 设置 cookies
ngx.header["Set-Cookie"] = "session=af54; Path=/; Expires=" ..
ngx.cookie_time(ngx.time() + 86400)
测试代码
location ~ /header_add {
content_by_lua_block {
ngx.header["Set-Cookie"] = "session=lzz; Path=/; Expires=" ..
ngx.cookie_time(ngx.time() + 86400)
ngx.say("orangleliu")
}
}
看看效果
# curl -i 127.0.0.1:8001/header_add
HTTP/1.1 200 OK
Server: openresty/1.9.7.1
Date: Fri, 16 Dec 2016 07:00:30 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: session=lzz; Path=/; Expires=Sat, 17-Dec-16 07:00:30 GMT
- set cookie 使用的是直接操作 header 的方式,这个时候如果单独添加一个cookie就会覆盖掉其他的值,所以需要多做一些处理。
function get_cookies()
local cookies = ngx.header["Set-Cookie"] or {}
if type(cookies) == "string" then
cookies = {cookies}
end
return cookies
end
function add_cookie(cookie)
local cookies = get_cookies()
table.insert(cookies, cookie)
ngx.header['Set-Cookie'] = cookies
end
add_cookie("session=aff12d; Path=/")
⚠️:上面的代码需要在 header_filter 阶段运行,在这个阶段可以获取 应用或者是后端 proxy 设置的cookies, 如果你想要 添加,删除,替换某一个cookies,注意不要覆盖所有的 cookies了。
- 删除cookies跟上面类似
一个例子,在content阶段设置了多个cookies,然后在 header_filter 阶段删除cookies
location ~ /header_delete {
content_by_lua_block {
ngx.header["Set-Cookie"] = {'Foo=abc; path=/', 'age=18; path=/'}
ngx.say("openresty")
}
header_filter_by_lua_block {
local match = string.match
local function get_cookies()
-- local cookies = ngx.header["Set-Cookie"] or {}
local cookies = ngx.header.set_cookie or {}
if type(cookies) == "string" then
cookies = {cookies}
end
return cookies
end
local function remove_cookie(cookie_name)
local cookies = get_cookies()
ngx.log(ngx.ERR, "source cookies ", table.concat(cookies, " "))
for key, value in ipairs(cookies) do
local name = match(value, "(.-)=")
ngx.log(ngx.ERR, key.."<=>", value)
if name == cookie_name then
table.remove(cookies, key)
end
end
ngx.header['Set-Cookie'] = cookies or {}
ngx.log(ngx.ERR, "new cookies ", table.concat(cookies, " "))
end
remove_cookie("Foo")
}
}
}
测试下
# curl '127.0.0.1:8001/header_delete' -i
HTTP/1.1 200 OK
Server: openresty/1.9.7.1
Date: Fri, 16 Dec 2016 08:07:34 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: age=18; path=/
其他需求
- 在做 proxy_pass 之后想给请求统一修改cookies值这么做呢?反正都很类似了。。
使用 header_filter_by_lua 指令
header_filter_by_lua '
local cookies = ngx.header.set_cookie
if cookies then
if type(cookies) ~= "table" then
cookies = {cookies}
end
local gsub = string.gsub
local changed
for i, cookie in ipairs(cookies) do
local new_cookie = gsub(cookie, "^target%-cookie=[^;]*", "target-cookie=myvalue", 1)
if new_cookie ~= cookie then
cookies[i] = new_cookie
changed = true
end
end
if changed then
ngx.header.set_cookie = cookies
end
end
';
https://github.com/openresty/headers-more-nginx-module/issues/18
ref
OpenResty 操作cookies的更多相关文章
- Openresty 操作Cookie
Openresty 操作cookie共有两种方法: 1.直接操作 1.1 获取Cookie 获取所有cookie: ngx.var.http_cookie, 这里获取的是一个字符串,如果不存在则返回n ...
- JS操作Cookies
JS操作Cookies 获取Cookie function getCookie(c_name) { if (document.cookie.length > 0) { c_start = doc ...
- Selenium常用操作汇总二——如何操作cookies(转)
Web 测试中我们经常会接触到Cookies,一个Cookies主要属性有”所在域.name.value.有效日期和路径",下面来讲一下怎么操作Cookies. import java.ut ...
- JS操作Cookies的小例子
这篇文章介绍了JS操作Cookies的小例子,有需要的朋友可以参考一下. 您可能感兴趣的文章:js 保存与获取cookie的代码javascript cookie操作实例详解javascript co ...
- javascript 操作cookies详解
javascript 操作cookies详解 这段操作cookies的方法我使用很久了,但是一直一来没遇到什么问题,今天在做一个在第一个页面保存了cookies,第二个页面获取或者第三个页面获取的功能 ...
- selenium webdriver学习(九)------------如何操作cookies(转)
selenium webdriver学习(九)------------如何操作cookies 博客分类: Selenium-webdriver Web 测试中我们经常会接触到Cookies,一个C ...
- selenium如何操作cookies实现免登录
执行接口测试或者某些自动化测试时,为了避免每次访问接口都需要登录操作,可以用访问接口时,把cookies信息传过去. 思路是先登录一次页面,获取到cookies信息,把cookies信息保存到本地文件 ...
- 使用jQuery操作Cookies的实现代码
Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是 ...
- java操作cookies
建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下 HttpServletRequest request HttpServletResponse response Co ...
随机推荐
- 基于 MySQL 的数据库实践(准备工作)
背景 本学期在北京大学选修了<数据库概论>的实验班课程,由于 SQL 语法并不是特别理论的内容,因此课上暂时也没有特别展开.出于探索数据库领域的兴趣,使用国内普遍使用的数据库软件 MySQ ...
- 【阿里聚安全·安全周刊】 全美警局已普遍拥有破解 iPhone 的能力 | 女黑客破解任天堂Switch,称硬件漏洞无法修复
本周的七个关键词: 破解 iPhone丨 女黑客破解任天堂丨假的身份证 丨 扫黄打非丨华盛顿特区发现手机间谍设备 丨 Telegram被俄罗斯监管机构告上法庭丨价值5万美金的Firefox浏览器漏洞 ...
- Linux 定时执行shell脚本命令之crontab
crontab可以在指定的时间执行一个shell脚本以及执行一系列Linux命令 例如:服务器管理员定时备份数据库数据.日志等 详解: 常用命令: crontab –e //修改 crontab 文件 ...
- Extensions in UWP Community Toolkit - WebViewExtensions
概述 UWP Community Toolkit Extensions 中有一个为 WebView 提供的扩展 - WebViewExtensions,本篇我们结合代码详细讲解 WebView Ext ...
- jmc远程连接windows环境tomcat
新人报道,先发个小贴赚点人气,本人目前还是小菜鸟,想要飞却怎么也飞不高,哈哈,转到正题,最近发现这个JMC挺好用的,而且也不用像Jprofile需要破解,本地连接挺方便的, 但配置服务器确实挺坑的,按 ...
- NSURLSession使用, 后台下载
现在越来越多的开发习惯于使用各种第三方框架,诚然,第三方框架给我们开发带来了很多便利,但我们不能太依赖于第三方,在使用第三方的同时学习其原理才是硬道理. 所以今天我们就来讲讲AFNetworking所 ...
- PostgreSQL 常用系统自带方法
数据库字符编码问题: -- 查看PostgreSQL数据库服务器端编码: show server_encoding; -- 查看PostgreSQL客户端工具psql编码: s ...
- Hive优化案例
1.Hadoop计算框架的特点 数据量大不是问题,数据倾斜是个问题. jobs数比较多的作业效率相对比较低,比如即使有几百万的表,如果多次关联多次汇总,产生十几个jobs,耗时很长.原因是map re ...
- [LeetCode] Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...