AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 728791
Accepted
neezer
neezer
Asked: 2015-10-14 20:47:02 +0800 CST2015-10-14 20:47:02 +0800 CST 2015-10-14 20:47:02 +0800 CST

基于cookie在proxy_pass和uwsgi_pass之间切换?

  • 772

我的 nginx 服务器当前设置为通过以下方式服务所有请求uwsgi_pass:

location / {
  uwsgi_pass unix:///tmp/uwsgi-zerocater.sock;
  include uwsgi_params;
  uwsgi_read_timeout 7200;
  client_max_body_size 20M;
}

在我的应用程序代码中,我设置了一个名为new_fe“True”或“False”的cookie。

当值为“True”时,我想改为proxy_pass另一个外部服务器,但仅限于特定路由。类似于以下伪代码:

location ~ "^/my/complicated/regex$" {
  if ($cookie_new_fe = "True") {
    # pass the request to my other server
    # at something like http://other.server.com
  } else {
    # do the usual uwsgi stuff
  }
}

我怎样才能做到这一点?我还是有点新nginx,所以如果我错过了一些简单的问题,请原谅这个问题。

nginx
  • 2 2 个回答
  • 1104 Views

2 个回答

  • Voted
  1. Best Answer
    Alexey Ten
    2015-10-14T22:41:38+08:002015-10-14T22:41:38+08:00

    作为临时解决方案,这应该有效:

    location ~ "^/my/complicated/regex$" {
      if ($cookie_new_fe = "True") {
        error_page 418 = @proxy;
        return 418;
      }
    
      # do the usual uwsgi stuff
    }
    
    location @proxy {
        # do proxy stuff
    }
    
    • 0
  2. neezer
    2015-10-15T09:46:19+08:002015-10-15T09:46:19+08:00

    我进一步研究了地图并提出了这个解决方案,它似乎有效(使用@womble 和@alexey-ten 的建议):

    map $cookie_new_fe $serve_request_from {
      default @uwsgi;
      "True" @proxy;
    }
    
    server {
    
      # ...
    
      # BEGIN Routes to switch based on value of $cookie_new_fe
      location ~ "^/my/complicated/regex$" {
        error_page 418 = $serve_request_from;
        return 418;
      }
      # END
    
      location / {
        error_page 418 = @uwsgi;
        return 418;
      }
    
      location @uwsgi {
        uwsgi_pass unix:///tmp/uwsgi-origserver.sock;
        include uwsgi_params;
        uwsgi_read_timeout 7200;
        client_max_body_size 20M;
      }
    
      location @proxy {
        proxy_pass https://other.server.com;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
      }
    }
    

    这样,在此迁移完成之前,我需要在此处添加的许多路由中的重复最少。

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve