aimerforreimu
V2EX  ›  问与答

求 Nginx 实现 cookie 判断的方法

By aimerforreimu at 2017 年 6 月 27 日 · 6560 次点击

是这样的,想要用 Nginx 对某一个特定的 cookie 进行判断,如果存在这个 cookie,当用户访问某一个页面的时候返回 403,当不存在这 cookie 的时候访问该页面是正常的 应该如何实现? THX~

6 条回复  •  2017-06-28 15:01:35 +08:00
wanderer
   1
wanderer  
   2017 年 6 月 28 日 via iPhone
这个还是比较容易的吧。
用 ngx_lua 几行代码就搞定了,在 access 阶段读取下 $cookie_你需要检测的 cookie 名字,如果为空,则………如果存在则……

或者你可以试下 cloudflare 的 https://github.com/cloudflare/lua-resty-cookie/blob/master/README.md 这个库子中的 get 方式。
wanderer
   2
wanderer  
   2017 年 6 月 28 日 via iPhone
或者直接用 rewrite 模块的 if 指令也能完成你想要的需求
xiaopc
   3
xiaopc  
   2017 年 6 月 28 日 via Android
if ($http_cookie ~* "example=test"){
return 403;
}
aimerforreimu
   4
aimerforreimu  
OP
   2017 年 6 月 28 日
@xiaopc
感谢,这个我是知道的
但是,这个对于指定页面如何实现呢
我是卡在了对于指定页面上,比如访问 https://example.com/example.html 时进行判断
aimerforreimu
   5
aimerforreimu  
OP
   2017 年 6 月 28 日
@wanderer 感谢,lua 模块我确实不了解……
让我看一下,如果使用 rewrite 模块应该如何写呢
xiaopc
   6
xiaopc  
   2017 年 6 月 28 日 via Android
location 块啊.....
© 2026 V2EX · 31ms · 3.9.8.5