V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
aimerforreimu
V2EX  ›  问与答

求 Nginx 实现 cookie 判断的方法

  •  
  •   aimerforreimu · Jun 27, 2017 · 6279 views
    This topic created in 3236 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
        1
    wanderer  
       Jun 28, 2017 via iPhone
    这个还是比较容易的吧。
    用 ngx_lua 几行代码就搞定了,在 access 阶段读取下 $cookie_你需要检测的 cookie 名字,如果为空,则………如果存在则……

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