aimerforreimu
V2EX  ›  问与答

求 Nginx 实现 cookie 判断的方法

  •  
  •   aimerforreimu · Jun 27, 2017 · 6664 views
    This topic created in 3420 days ago, the information mentioned may be changed or developed.

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

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

    或者你可以试下 cloudflare 的 https://github.com/cloudflare/lua-resty-cookie/blob/master/README.md 这个库子中的 get 方式。
    wanderer
    wanderer
    Jun 28, 2017 via iPhone
    2
    或者直接用 rewrite 模块的 if 指令也能完成你想要的需求
    xiaopc
    xiaopc
    Jun 28, 2017 via Android
    3
    if ($http_cookie ~* "example=test"){
    return 403;
    }
    aimerforreimu
    aimerforreimu
    OP
    Jun 28, 2017
    4
    @xiaopc
    感谢,这个我是知道的
    但是,这个对于指定页面如何实现呢
    我是卡在了对于指定页面上,比如访问 https://example.com/example.html 时进行判断
    aimerforreimu
    aimerforreimu
    OP
    Jun 28, 2017
    5
    @wanderer 感谢,lua 模块我确实不了解……
    让我看一下,如果使用 rewrite 模块应该如何写呢
    xiaopc
    xiaopc
    Jun 28, 2017 via Android
    6
    location 块啊.....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1475 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:49 · PVG 17:49 · LAX 01:49 · JFK 04:49
    ♥ Do have faith in what you're doing.