V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
aimerforreimu
V2EX  ›  问与答

求 Nginx 实现 cookie 判断的方法

  •  
  •   aimerforreimu · 2017-06-27 22:28:33 +08:00 · 5842 次点击
    这是一个创建于 2513 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    或者你可以试下 cloudflare 的 https://github.com/cloudflare/lua-resty-cookie/blob/master/README.md 这个库子中的 get 方式。
    wanderer
        2
    wanderer  
       2017-06-28 01:34:20 +08:00 via iPhone
    或者直接用 rewrite 模块的 if 指令也能完成你想要的需求
    xiaopc
        3
    xiaopc  
       2017-06-28 06:58:13 +08:00 via Android
    if ($http_cookie ~* "example=test"){
    return 403;
    }
    aimerforreimu
        4
    aimerforreimu  
    OP
       2017-06-28 14:43:05 +08:00
    @xiaopc
    感谢,这个我是知道的
    但是,这个对于指定页面如何实现呢
    我是卡在了对于指定页面上,比如访问 https://example.com/example.html 时进行判断
    aimerforreimu
        5
    aimerforreimu  
    OP
       2017-06-28 14:47:20 +08:00
    @wanderer 感谢,lua 模块我确实不了解……
    让我看一下,如果使用 rewrite 模块应该如何写呢
    xiaopc
        6
    xiaopc  
       2017-06-28 15:01:35 +08:00 via Android
    location 块啊.....
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2928 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:28 · PVG 23:28 · LAX 08:28 · JFK 11:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.