search
HomeBackend DevelopmentPHP TutorialSeveral ways for nginx load balancer to handle session sharing

1) Instead of using session, use cookie

If you can change session to cookie, you can avoid some of the disadvantages of session. A J2EE book I read before also stated that session cannot be used in cluster systems. , otherwise it will be difficult to handle if it causes trouble. If the system is not complex, give priority to whether the session can be removed. If it is very troublesome to change, then use the following method.

2) The application server realizes sharing by itself

It is known that PHP can use a database or memcached to save the session, thereby establishing a session cluster in PHP itself. In this way, the session can be guaranteed to be stable, even if a certain If a node fails, the session will not be lost. It is suitable for more strict situations but low request volume. However, its efficiency is not very high and it is not suitable for occasions that require high efficiency.

The above two methods have nothing to do with nginx. Let’s talk about how to use nginx:

3) ip_hash

The ip_hash technology in nginx can direct the request of a certain IP to the same computer. end, so that a client and a backend under this IP can establish a stable session. ip_hash is defined in the upstream configuration:

upstream backend {
server 127.0.0.1: 8001;
server 127.0.0.1:8002;
ip_hash;
}

ip_hash is easy to understand, but because only the ip factor can be used to allocate backends, ip_hash is defective , cannot be used in some situations:

1/ nginx is not the most front-end server. ip_hash requires that nginx must be the front-end server. Otherwise, nginx cannot get the correct IP and cannot hash based on the IP. For example, if Squid is used as the front end, then nginx can only get the server IP address of Squid when fetching the IP. It is definitely confusing to use this address for distribution.

2/ nginx’s backend also has other methods of load balancing. If there are other load balancing on the nginx backend and the requests are diverted in another way, then a request from a certain client will definitely not be located on the same session application server. Calculated this way, the nginx backend can only point directly to the application server, or build a Squid and then point to the application server. The best way is to use location as a diversion, divert some requests that require session through ip_hash, and go to other backends for the rest.

4) upstream_hash

In order to solve some problems of ip_hash, you can use the third-party module upstream_hash. This module is used as url_hash in most cases, but it does not prevent it from being used for session sharing:

If the front end is squid, it will add the ip to the x_forwarded_for http_header. Using upstream_hash, you can use this header as a factor to direct the request to the specified backend:

See this document:

http:/ /www.oschina.net/discuss/thread/622

In the document, $request_uri is used as the factor. Change it slightly:

hash $http_x_forwarded_for;

This way, we use the x_forwarded_for header As a factor, the new version of nginx can support reading cookie values, so it can also be changed to:

hash $cookie_jsessionid;

If the session configured in php is cookie-less, use nginx's own one The userid_module module can use nginx to generate a cookie. Please refer to the English documentation of the userid module:

http://wiki.nginx.org/NginxHttpUserIdModule

The above introduces several methods for nginx load balancer to handle session sharing, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
SpringBoot Session怎么设置会话超时SpringBoot Session怎么设置会话超时May 15, 2023 pm 02:37 PM

问题发现springboot项目生产session-out超时问题,描述下问题:在测试环境通过改动application.yaml配置session-out,经过设置不同时间验证session-out配置生效,于是就直接设置了过期时间为8小时发布到了生产环境。然而中午接到客户反应项目过期时间设置较短,半小时不操作就会话过期需要反复登陆。解决处理开发环境:springboot项目内置Tomcat,所以项目中application.yaml配置session-out是生效的。生产环境:生产环境发布是

php session刷新后没有了怎么办php session刷新后没有了怎么办Jan 18, 2023 pm 01:39 PM

php session刷新后没有了的解决办法:1、通过“session_start();”开启session;2、把所有的公共配置写在一个php文件内;3、变量名不能和数组下标相同;4、在phpinfo里面查看session数据的存储路径,并查看该文件目录下的sessio是否保存成功即可。

session php默认失效时间是多少session php默认失效时间是多少Nov 01, 2022 am 09:14 AM

session php默认失效时间是1440秒,也就是24分钟,表示客户端超过24分钟没有刷新,当前session就会失效;如果用户关闭了浏览器,会话就会结束,Session就不存在了。

Redis的共享session应用如何实现短信登录Redis的共享session应用如何实现短信登录Jun 03, 2023 pm 03:11 PM

1.基于session实现短信登录1.1短信登录流程图1.2实现发送短信验证码前端请求说明:说明请求方式POST请求路径/user/code请求参数phone(电话号码)返回值无后端接口实现:@Slf4j@ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1.校验手机号if

PHP如何在多个文件中正确地读取和写入Session数据PHP如何在多个文件中正确地读取和写入Session数据Mar 23, 2023 am 11:12 AM

当您在使用PHP会话(Session)时,有时会发现Session在一个文件中可以正常读取,但在另一个文件中却无法读取。这可能会让您感到困惑,因为会话数据应该可以在整个应用程序中共享。本文将解释如何在多个文件中正确地读取和写入PHP会话数据。

JavaScript和PHP的cookie之间有哪些区别?JavaScript和PHP的cookie之间有哪些区别?Sep 02, 2023 pm 12:29 PM

JavaScriptCookie使用JavaScriptcookie是记住和跟踪偏好、购买、佣金和其他信息的最有效方法。更好的访问者体验或网站统计所需的信息。PHPCookieCookie是存储在客户端计算机上的文本文件并保留它们用于跟踪目的。PHP透明地支持HTTPcookie。JavaScriptcookie如何工作?您的服务器将一些数据发送到访问者的浏览器cookie的形式。浏览器可以接受cookie。如果存在,它将作为纯文本记录存储在访问者的硬盘上。现在,当访问者到达站点上的另一个页面时

Springboot2 session设置超时时间无效怎么解决Springboot2 session设置超时时间无效怎么解决May 22, 2023 pm 01:49 PM

问题:今天项目中遇到了一个设置时间超时的问题,按SpringBoot2的application.properties更改一直不生效。解决方案:server.*属性用于控制SpringBoot使用的嵌入式容器。SpringBoot将使用ServletWebServerFactory实例之一创建servlet容器的实例。这些类使用server.*属性来配置受控的servlet容器(tomcat,jetty等)。当应用程序作为war文件部署到Tomcat实例时,server.*属性不适用。它们不适用,

PHP如何处理微信小程序中的session问题PHP如何处理微信小程序中的session问题Jun 02, 2023 pm 03:40 PM

近年来,微信小程序风靡全球,已经成为了许多企业和个人开发者的首选平台。在小程序的开发中,我们经常会遇到session问题,也就是如何在小程序中保存用户登录状态。这个问题对于网站开发者来说并不陌生,但在小程序中却有些不同。本文将介绍如何使用PHP解决微信小程序中的session问题。一、小程序登录过程概述小程序的登录流程与网站的登录流程类似,分为以下几个步骤:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment