Module ngx_http_upstream_module http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky

Syntax: sticky cookie name [expires=time] [domain=domain] [httponly] [secure] [path=path];
sticky route $variable ...;
sticky learn create=$variable lookup=$variable zone=name:size [timeout=time] [header] [sync];
Default:
Context: upstream

This directive appeared in version 1.5.7.

Enables session affinity, which causes requests from the same client to be passed to the same server in a group of servers. Three methods are available:

cookie

When the cookie method is used, information about the designated server is passed in an HTTP cookie generated by nginx:

upstream backend {
server backend1.example.com;
server backend2.example.com; sticky cookie srv_id expires=1h domain=.example.com path=/;
}

A request that comes from a client not yet bound to a particular server is passed to the server selected by the configured balancing method. Further requests with this cookie will be passed to the designated server. If the designated server cannot process a request, the new server is selected as if the client has not been bound yet.

The first parameter sets the name of the cookie to be set or inspected. The cookie value is a hexadecimal representation of the MD5 hash of the IP address and port, or of the UNIX-domain socket path. However, if the “route” parameter of the server directive is specified, the cookie value will be the value of the “route” parameter:

upstream backend {
server backend1.example.com route=a;
server backend2.example.com route=b; sticky cookie srv_id expires=1h domain=.example.com path=/;
}

In this case, the value of the “srv_id” cookie will be either a or b.

Additional parameters may be as follows:

expires=time
Sets the time for which a browser should keep the cookie. The special value max will cause the cookie to expire on “31 Dec 2037 23:55:55 GMT”. If the parameter is not specified, it will cause the cookie to expire at the end of a browser session.
domain=domain
Defines the domain for which the cookie is set. Parameter value can contain variables (1.11.5).
httponly
Adds the HttpOnly attribute to the cookie (1.7.11).
secure
Adds the Secure attribute to the cookie (1.7.11).
path=path
Defines the path for which the cookie is set.

If any parameters are omitted, the corresponding cookie fields are not set.

route

When the route method is used, proxied server assigns client a route on receipt of the first request. All subsequent requests from this client will carry routing information in a cookie or URI. This information is compared with the “route” parameter of the server directive to identify the server to which the request should be proxied. If the “route” parameter is not specified, the route name will be a hexadecimal representation of the MD5 hash of the IP address and port, or of the UNIX-domain socket path. If the designated server cannot process a request, the new server is selected by the configured balancing method as if there is no routing information in the request.

The parameters of the route method specify variables that may contain routing information. The first non-empty variable is used to find the matching server.

Example:

map $cookie_jsessionid $route_cookie {
~.+\.(?P<route>\w+)$ $route;
} map $request_uri $route_uri {
~jsessionid=.+\.(?P<route>\w+)$ $route;
} upstream backend {
server backend1.example.com route=a;
server backend2.example.com route=b; sticky route $route_cookie $route_uri;
}

Here, the route is taken from the “JSESSIONID” cookie if present in a request. Otherwise, the route from the URI is used.

learn

When the learn method (1.7.1) is used, nginx analyzes upstream server responses and learns server-initiated sessions usually passed in an HTTP cookie.

upstream backend {
server backend1.example.com:8080;
server backend2.example.com:8081; sticky learn
create=$upstream_cookie_examplecookie
lookup=$cookie_examplecookie
zone=client_sessions:1m;
}

In the example, the upstream server creates a session by setting the cookie “EXAMPLECOOKIE” in the response. Further requests with this cookie will be passed to the same server. If the server cannot process the request, the new server is selected as if the client has not been bound yet.

The parameters create and lookup specify variables that indicate how new sessions are created and existing sessions are searched, respectively. Both parameters may be specified more than once, in which case the first non-empty variable is used.

Sessions are stored in a shared memory zone, whose name and size are configured by the zoneparameter. One megabyte zone can store about 4000 sessions on the 64-bit platform.  The sessions that are not accessed during the time specified by the timeout parameter get removed from the zone. By default, timeout is set to 10 minutes.

The header parameter (1.13.1) allows creating a session right after receiving response headers from the upstream server.

The sync parameter (1.13.8) enables synchronization of the shared memory zone.

This directive is available as part of our commercial subscription.

Syntax: sticky_cookie_insert name [expires=time] [domain=domain] [path=path];
Default:
Context: upstream

This directive is obsolete since version 1.5.7. An equivalent sticky directive with a new syntax should be used instead:

sticky cookie name [expires=time] [domain=domain] [path=path];

存储位置:Sessions are stored in a shared memory zone;

cookie/session值:目的是导向哪个服务器,故值为服务器节点标识;

粘性会话 session affinity sticky session requests from the same client to be passed to the same server in a group of servers的更多相关文章

  1. Cloud Foundry Session Affinity(Sticky Session)的实现

    会话保持(Session Affinity),有时又称粘滞会话(Sticky Sessions), 是负载均衡领域设计需要着力解决的重要问题之一,也是一个相对比较复杂的问题. 会话保持是指在负载均衡器 ...

  2. Enabling Session Persistence 粘性会话

    NGINX Docs | HTTP Load Balancing https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-ba ...

  3. ip_hash(不推荐使用) 会话粘性问题分析 Cookie 的 Session Sticky

    Nignx 连接tomcat时会话粘性问题分析_changyanmanman的专栏-CSDN博客_后端tomcat导致 前端elb中断 https://blog.csdn.net/cymm_liu/a ...

  4. sticky session 粘性会话

    New Elastic Load Balancing Feature: Sticky Sessions | AWS News Blog https://amazonaws-china.com/cn/b ...

  5. Nginx+Memcached+Tomcat集群配置实践(Sticky Session)

    准备工作 创建一个简单的web应用,名为session.其中有两个页面,分别如下所示: 页面login.jsp <%@ page language="java" conten ...

  6. kubernetes sticky session 配置

    kubernetes中经常需要配置sticky session,此时需要根据你使用的ingress controller来设置了. 1.Nginx Ingress apiVersion: extens ...

  7. 会话数据的管理——Session

    cookie的局限性: cookie只能存字符串类型.不能保存对象 只能存非中文 1个cookie的容量不超过4KB(如果要保存非字符串,超过4kb内容,只能使用session技术!!!) Sessi ...

  8. 会话技术cookie和session详解

    什么是会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 会话技术解决了什么问题 每个用户与服务器进行交互的过程中,各自会有一 ...

  9. 会话管理---Cookie与Session

    会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 保存会话数据的两种技术:Cookie,Session Cookie是客户端技术, ...

随机推荐

  1. mysqlbinlog 导出某时间段的是二进制日志

    mysqlbinlog --no-defaults --start-datetime="2016-07-26 00:00:00" --stop-datetime="201 ...

  2. CConfig类

    #ifndef __CONFIG_H__ #define __CONFIG_H__ #include "GameFrameHead.h" //图片信息 struct ImageIn ...

  3. HTTP协议 - 协议格式

    HTTP 是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和 扩展.目前在WWW中使用的是HTTP/1.0 ...

  4. (1)FluidMoveBehavior 之 ListBox 中详细内容项飞出来

    在 Blend 中集成了很多行为,首先需要了解一下Behaviors(行为)的几个关键点: (1)Behaviors(行为)是可复用代码集合,可以被任何对象附加使用: (2)设计人员和开发人员可以使用 ...

  5. js 内置对象常用方法

    1 内容概述 js包含一些内置对象,如Array,Function,String等,这些是基本的,常用的js类,所以了解它们十分重要:把他们的方法,用例子和文字简要的记录下来,方便今后参看. 2 Ar ...

  6. mysql学习笔记1---mysql ERROR 1045 (28000): 错误解决办法(续:深入分析)

    在命令行输入mysql -u root –p,输入密码,或通过工具连接数据库时,经常出现下面的错误信息,详细该错误信息很多人在使用MySQL时都遇到过. ERROR 1045 (28000): Acc ...

  7. Android——问题解决之adb not responding;adb不是内部或外部命令;path变量的默认值为多少

    adb not responding 恩,这是出现的问题.我们开始来解决它吧! 出现这种问题大多是因为adb端口被占用导致这个问题,所以只要找到占用端口号程序,结束即可!就是这么简单(adb运行端口号 ...

  8. C++基本功之Operator

    废话不多说,这次讲的是 Operator overload. 关于operator, 在 < The C++ Programing Language > 里的描述,可以用做overload ...

  9. Java的多线程 简单入门

    Java的多线程 简单入门 首先能够先搞清楚什么是程序.进程.线程,以及它们之间的关系: 定义: 一 程序仅仅是一组指令的有序集合.它是静态的 二 进程是具有一定独立功能的程序关于某个数据集合上的一次 ...

  10. noip2014滚粗记

    滚粗了..伤心. day0:和baba一起去,但是整天都是下雨啊好不爽,鞋子都湿了啊好不爽,注定是要滚粗?在火车站等了1h后上动车走人...在此期间我还天真的认为火车站的wifi可以被我给破解然后上网 ...