【问题标题】:How to redirect NGINX based on http cookie如何基于 http cookie 重定向 NGINX
【发布时间】:2021-09-21 05:04:02
【问题描述】:

我有这样的 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;
    }
}

想法是像配置路由规则

  • 任何没有 cookie 的请求都会重定向到登录服务器 (5000)。
  • 否则它应该服务于根目录中的页面。

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

【问题讨论】:

  • $http_cookie包含user_tokens的情况下使用!~*

标签: http nginx server routes


【解决方案1】:

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

if ($http_cookie !~* "user_tokens")

【讨论】:

    猜你喜欢
    • 2015-05-30
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2022-08-14
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多