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 / 问题 / 697530
Accepted
slm
slm
Asked: 2015-06-09 18:21:41 +0800 CST2015-06-09 18:21:41 +0800 CST 2015-06-09 18:21:41 +0800 CST

Nginx 上游从 serverA 故障转移到 serverB,然后回到 serverA,为什么?

  • 772

背景

我有以下 Nginx 配置,它负载平衡在 2 个不同服务器上运行的 2 个 Tomcat 服务器:

   ### http://nginx.org/en/docs/http/ngx_http_upstream_module.html#hash
   upstream backend_w_affinity { 
     hash $request_uri consistent;
     server 192.168.110.11:8080;
     server 192.168.110.12:8080; 
   }

   server {
            listen 80 default_server;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            location ~ ^/path/to/rest/endpoint$ { proxy_pass http://backend_w_affinity; }
  }

当我使用以下curl命令在 Nginx 驱动流量时,它似乎很好。

$ watch "curl -iI http://10.128.13.153/path/to/rest/endpoint"
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Tue, 09 Jun 2015 01:31:13 GMT
Content-Type: application/xml;charset=ISO-8859-1
Content-Length: 231
Connection: keep-alive

注意:我意识到我得到了上面的 401。肯定有 Tomcat 启动了,如果我更改路径,使其指向 Tomcat 中的静态页面,这样我得到 200 个,它的行为仍然相同,所以目前这似乎不是问题。

最初看起来不错

运行上述curl命令后,我的所有流量都被定向到第一个 Tomcat 服务器。如果我停止 Tomcat,那么所有流量都会故障转移到第二个 Tomcat 服务器。

我的问题

当我恢复第一台 Tomcat 服务器时,我希望所有流量现在都保留在第二台服务器上。但是,一旦我将其恢复,流量就会被引导回第一个 Tomcat 服务器。

我怎样才能获得 Nginx,将流量留在第二台服务器上,而不是将其重定向回来?

nginx
  • 1 1 个回答
  • 1267 Views

1 个回答

  • Voted
  1. Best Answer
    rmalayter
    2015-06-12T06:57:07+08:002015-06-12T06:57:07+08:00

    如果您希望流量即使在上游组成员资格发生变化的情况下仍坚持到服务器,则需要使用“粘性”会话。只要可用,一致的哈希就会选择相同的服务器,这似乎不是您想要的。

    在您的情况下,您正在对 URI 进行哈希处理,如果您在 nginx 中使用 proxy_cache,这对“缓存位置”很有用。然而,一致的哈希意味着对于给定的 URI,一个服务器将始终是“首选”,因此一旦它重新上线,nginx 就会切换回它。

    有来自 3rd 的开源 nginx 可用的粘性会话模块,或者在商业“NGINX Plus”中有一个本机粘性会话选项。

    无需附加模块或商业 nginx 订阅也可以进行粘性会话,但这有点难看。您可以将 map、proxy_pass 与目标中的变量主机名和备用位置结合使用。您还需要在每个后端服务器中设置一个唯一的标头来标识它们(因此它们发送了一个“X-Loadbalace-ID”标头。这可能是会话 cookie 的一部分,例如 Tomcat JVM 路由。我们实际上仍然运行它生产中的配置,因为它早于 nginx 上粘性会话的可用性。

    像这样的东西可能对你有用(我们所做的未经测试的简化):

    upstream backend_w_affinity { 
      #use round-robin
      server 192.168.110.11:8080;
      server 192.168.110.12:8080; 
      }
    
    server {
      listen 80 default_server;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
      map $http_cookie_lb $mybackend {
        server1 192.168.110.11:8080;
        server2 192.168.110.12:8080;
        #default is something that will fail quickly
        #when no correct load balancing cookie is set
        #putting us into the named location that uses
        #the upstream block
        default 127.0.0.1:65432;
        }
    
      location ~ ^/path/to/rest/endpoint$ { 
          proxy_pass http://$mybackend;
          #if the backend selected is down, fallback to upstream logic
          error_page 503 = @backend_down;
          }
      location @backend_down { 
          proxy_pass http://backend_w_affinity;
          add_header "Set-Cookie" "lb=$upstream_http_X_Loadbalance_ID";
          }
    }
    
    • 1

相关问题

  • 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