Nginx 中检测客户端不包含 Cookie:abc 即禁止访问怎么写? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nikoo
V2EX    NGINX

Nginx 中检测客户端不包含 Cookie:abc 即禁止访问怎么写?

  •  
  •   nikoo 2016-12-20 00:10:07 +08:00 5048 次点击
    这是一个创建于 3291 天前的主题,其中的信息可能已经有所发展或是发生改变。
    即所有访问客户端必须包含一个 cookie 名为 abc ,如不包含该 cookie 即禁止访问

    不在服务器端处理,仅在 nginx 中是否可以做到?谢谢!
    10 条回复    2016-12-20 13:32:31 +08:00
    Phant0m
        1
    Phant0m  
       2016-12-20 00:25:26 +08:00 via iPhone
    ngx lua 可以实现
    lhbc
        2
    lhbc  
       2016-12-20 00:54:49 +08:00
    map $COOKIE_abc
    shiniv
        3
    shiniv  
       2016-12-20 01:29:48 +08:00
    试试这样能能否符合你的需求

    set $auth_cookie 0;
    if ($http_cookie ~* "AUTH_COOKIE=([a-z0-9]+)(?:/|$)") {
    set $auth_cookie 1;
    }

    if ($auth_cookie = 0) {
    return 403;
    }
    jarlyyn
        4
    jarlyyn  
       2016-12-20 01:39:47 +08:00   2
    location / {
    if ( $cookie_antiscanpassword != "password") {
    return 403;
    }
    proxy_pass http://127.0.0.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


    http://blog.jarlyyn.com/site/blogi/100-%E7%94%A8nginx%2Bcookie%E9%98%B2%E6%AD%A2%E7%AE%80%E5%8D%95%E7%9A%84%E6%8A%93%E5%8F%96%2F%E7%A0%B4%E8%A7%A3%E9%AA%9A%E6%89%B0

    我线上在用的
    nikoo
        5
    nikoo  
    OP
       2016-12-20 03:13:11 +08:00
    @jarlyyn 谢谢,很有帮助

    如果仅判断 cookie 是否存在怎么处理?实际生产环境中 abc 的 cookie 值每个用户是不同的,没法这样写在配置里判断
    jarlyyn
        6
    jarlyyn  
       2016-12-20 03:37:22 +08:00 via Android
    @nikoo

    就是 if 那一句
    nikoo
        7
    nikoo  
    OP
       2016-12-20 09:20:30 +08:00
    @jarlyyn

    if ( $cookie_antiscanpassword != "password") {
    这句是判断名为 antiscanpassword 的 cookie 值是否为 "password" 吧?而不是判断 antiscanpassword 是否存在
    jarlyyn
        8
    jarlyyn  
       2016-12-20 10:05:46 +08:00
    @nikoo

    应该是直接 if ( $cookie_antiscanpassword),不行的话正则处理 if ( $cookie_antiscanpassword~ .+)

    参考 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

    a variable name; false if the value of a variable is an empty string or “ 0 ”;
    Before version 1.0.1, any string starting with “ 0 ” was considered a false value.
    comparison of a variable with a string using the “=” and “!=” operators;
    matching of a variable against a regular expression using the “~” (for case-sensitive matching) and “~*” (for case-insensitive matching) operators. Regular expressions can contain captures that are made available for later reuse in the $1..$9 variables. Negative operators “!~” and “!~*” are also available. If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.
    checking of a file existence with the “-f ” and “!-f ” operators;
    checking of a directory existence with the “-d ” and “!-d ” operators;
    checking of a file, directory, or symbolic link existence with the “-e ” and “!-e ” operators;
    checking for an executable file with the “-x ” and “!-x ” operators.
    AlexaZhou
        9
    AlexaZhou  
       2016-12-20 11:20:10 +08:00
    不想太麻烦的话,可以试试 VeryNginx
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2971 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 12:16 PVG 20:16 LAX 04:16 JFK 07:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86