粘性会话 session affinity sticky session requests from the same client to be passed to the same server in a group of servers
Module ngx_http_upstream_module http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky
| Syntax: | sticky sticky sticky |
|---|---|
| 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
cookiemethod 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 eitheraorb.Additional parameters may be as follows:
expires=time- Sets the
timefor which a browser should keep the cookie. The special valuemaxwill 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
domainfor which the cookie is set. Parameter value can contain variables (1.11.5). httponly- Adds the
HttpOnlyattribute to the cookie (1.7.11). secure- Adds the
Secureattribute to the cookie (1.7.11). path=path- Defines the
pathfor which the cookie is set.
If any parameters are omitted, the corresponding cookie fields are not set.
route-
When the
routemethod 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
routemethod 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
learnmethod (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
createandlookupspecify 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
nameandsizeare configured by thezoneparameter. One megabyte zone can store about 4000 sessions on the 64-bit platform. The sessions that are not accessed during the time specified by thetimeoutparameter get removed from the zone. By default,timeoutis set to 10 minutes.The
headerparameter (1.13.1) allows creating a session right after receiving response headers from the upstream server.The
syncparameter (1.13.8) enables synchronization of the shared memory zone.
This directive is available as part of our commercial subscription.
| Syntax: | sticky_cookie_insert |
|---|---|
| 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 cookiename[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的更多相关文章
- Cloud Foundry Session Affinity(Sticky Session)的实现
会话保持(Session Affinity),有时又称粘滞会话(Sticky Sessions), 是负载均衡领域设计需要着力解决的重要问题之一,也是一个相对比较复杂的问题. 会话保持是指在负载均衡器 ...
- Enabling Session Persistence 粘性会话
NGINX Docs | HTTP Load Balancing https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-ba ...
- ip_hash(不推荐使用) 会话粘性问题分析 Cookie 的 Session Sticky
Nignx 连接tomcat时会话粘性问题分析_changyanmanman的专栏-CSDN博客_后端tomcat导致 前端elb中断 https://blog.csdn.net/cymm_liu/a ...
- sticky session 粘性会话
New Elastic Load Balancing Feature: Sticky Sessions | AWS News Blog https://amazonaws-china.com/cn/b ...
- Nginx+Memcached+Tomcat集群配置实践(Sticky Session)
准备工作 创建一个简单的web应用,名为session.其中有两个页面,分别如下所示: 页面login.jsp <%@ page language="java" conten ...
- kubernetes sticky session 配置
kubernetes中经常需要配置sticky session,此时需要根据你使用的ingress controller来设置了. 1.Nginx Ingress apiVersion: extens ...
- 会话数据的管理——Session
cookie的局限性: cookie只能存字符串类型.不能保存对象 只能存非中文 1个cookie的容量不超过4KB(如果要保存非字符串,超过4kb内容,只能使用session技术!!!) Sessi ...
- 会话技术cookie和session详解
什么是会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 会话技术解决了什么问题 每个用户与服务器进行交互的过程中,各自会有一 ...
- 会话管理---Cookie与Session
会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 保存会话数据的两种技术:Cookie,Session Cookie是客户端技术, ...
随机推荐
- struts2内置拦截器和自定义拦截器详解(附源码)
一.Struts2内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特 性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截 ...
- locatin
//location对象 //初始url:http://www.wrox.com/WileyCDA: //将URL修改为:http://www.wrox.com/WileyCDA/#section1 ...
- 用广搜实现的spfa
用广搜实现的spfa,如果是用一般的最短路,会发现构图很麻烦,因为它不是路径带权值,而是自身带权值.写起来只要注意,在点出队列的生活将其标记为0,在要压入队列的时候,判断其标记是否为0,为0表示队列中 ...
- hdu3948(后缀数组)
题意:给一串字符,需要你求不相同的回文子串个数....... 同ural1297,链接:http://www.cnblogs.com/ziyi--caolu/archive/2013/06/09/31 ...
- (转)love2d有用的辅助库--gamework
此文转自朱大仙,感谢他的劳作. 翻译来源地址:https://github.com/Kadoba/gamework gamework是控制LOVE2D游戏进程流的一个项目. ↑ 这个是按原文译的, 当 ...
- 关于Netfilter NF_HOOK宏的outdev參数bug
1.首先指出.NF_HOOK系列宏的outdev參数的传递方式(直接传递一个net_device结构体指针)是不对的 正确的方式要么是不传递.要么是传递指针的地址,即地址的地址. 2.接下来指出,仅仅 ...
- Unix系统编程()brk,sbrk
在堆上分配内存 进程可以通过增加堆的大小来分配内存,所谓堆是一段长度可变的连续虚拟内存,始于进程的未初始化数据段末尾,随着内存的分配和释放而增减.通常将堆的当前内存边界称为"program ...
- VIM打开shell脚本中文乱码解决
1. 查找 .vimrc文件 通常有2个地方保存这个文件的: (1) 在/etc/文件夹下面,是所有用户的vim配置 (2)每个用户的开始登录的文件夹下面,有些不一定有,比如hadoop用户,则在/h ...
- crontab中运行python程序出错,提示ImportError: No module named解决全过程
将一个python脚本放入crontab执行时,提示如下错:ImportError: No module named hashlib但是在shell中直接执行时没有任何问题,google之后,得到线索 ...
- [Java] System.arraycopy 数组复制
函数原型: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) ; s ...