如何基于 http cookie 重定向 NGINX

How to redirect NGINX based on http cookie

我有这样的 NGINX 配置

server {
    listen       80;
    server_name  localhost;

    #any request without the http cookie has to be redirected to login
    location / {
        if ($http_cookie ~* "user_tokens") {
            return 302 http://127.0.0.0:5000/;
            #break;
        } 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

想法是像

那样配置路由规则

但这并没有像预期的那样工作,它总是从根目录提供页面。

假设 ~* 是否定条件是错误的,实际上是 !~*

if ($http_cookie !~* "user_tokens")