if not cookie set in nginx

5,074

Yes, but in my example you still need IF.

https://nginx.ru/en/docs/http/ngx_http_map_module.html

http {
    ...........
    map $cookie_PHPSESSID $ban {
    default '';
    ''  1;
    ...........
    }
    server {
    server_name example.com;
    if($ban) {return 403;}
        location / {
        ..........
        }
    .........
    }
}
Share:
5,074

Related videos on Youtube

nerkn
Author by

nerkn

Physician, programmer ...

Updated on September 18, 2022

Comments

  • nerkn
    nerkn over 1 year

    I wanted to restrict access to our downloads section. If users/bots wants to download file without logging in they will see 403 error. Logic is wonderful but it turned out very ugly code

    location /downloads/ {
     set $banforcookienotset 1;
     if ($cookie_PHPSESSID) {
        set $banforcookienotset 0;
     }
     if ($banforcookienotset = 1){
        return   403;}
    }
    

    Is there a better way to do this?