search
HomeBackend DevelopmentPHP TutorialNginx and PHP installation and configuration eight nginx session sharing

The content shared with you in this article is about the sharing of Nginx and PHP installation and configuration eight nginx sessions. It has a certain reference value. Friends in need can refer to it

Check Some information and reading some documents written by others are summarized as follows to achieve the sharing of nginx session

PHP There are multiple servers, use nginx for load balancing, so that the same IP will access the same page. are assigned to different servers. If session is out of sync, many problems will occur, such as the most common login status. Here are several ways to solve itsessionShared problem:

1, not using session , use cookie

session is stored on the server side, cookie is stored on the server side On the client side, we can put the session generated by the user's access to the page into cookie, that is, cookie is a transfer station. You access web serverA, generate session and then put it to # Inside ##cookie, when your request is assigned to the B server, the server B first Determine whether the server has this session. If not, check whether the client's cookie contains this session, if not, it means session really does not exist. If there is one in cookie, then Synchronize the sessoin in cookie to the serverB, so that you can achievesession is synchronized.

Note: This method is simple and convenient to implement, and will not increase the burden on the database, But if the client disables cookie, then session will not be synchronized, which will cause losses to the website; cookie is not very secure. Although it has been encrypted, it can still be forged.

2sessionExists in the database (MySQL etc.) in

php can be configured to save session in the database. This method is to store # The tables of ##session are placed together with other database tables. If mysql is also clustered, each mysqlNodes must have this table, and the data table of this session table must be synchronized in real time.

Instructions: Use the database to synchronize session, which will add big dataIO of the library increases the burden on the database. Moreover, the database reading and writing speed is slow, which is not conducive to timely synchronization of session.

3sessionexistmemcacheorRedis

memcachecan be distributed,phpSet the storage method in the configuration file to memcache, so php will create a session cluster, stores session data in memcache.

Note: Synchronizing session in this way will not increase the burden on the database and is safer than using cookie is greatly improved. Putting session into the memory is much faster than reading from the file. But memcache divides the memory into storage blocks of many specifications. Each block has a size, which is determined by this method. memcacheThe memory cannot be fully utilized, memory fragmentation will occur, and if there are insufficient storage blocks, memory overflow will occur.

4nginx#ip_hash technology can Direct the request of a certain ip to the same backend, so that a certain client under this ip# and a certain A stable session can be established with just one backend, ip_hash is in upstreamDefined in the configuration:

[html] view plain copy


1.   upstream nginx.example.com  
2.       {   
3.                server 192.168.74.235:80;   
4.                server 192.168.74.236:80;  
5.                ip_hash;  
6.       }  
7.       server  
8.       {  
9.                listen 80;  
10.               location /  
11.               {  
12.                       proxy_pass  
13.                      http://nginx.example.com;  
14.               }  
15.   }


##

ip_hash is easy to understand, but because only the factor ip can be used to allocate the backend, ip_hash is defective and cannot be used in some situations:
1.nginx
is not the front-end server.

ip_hashRequirementsnginx must be the front-end server, otherwisenginxIf you cannot get the correct ip, you cannot make hash# based on ip ##. For example, if squid is used as the front end, then nginx takes ip We can only get the server#ip address of squid. It is definitely confusing to use this address for distribution. 2. The backend of nginx
also has other methods of load balancing.

If nginx has other load balancing in the backend and diverts the request through another method, then the request of a certain client It definitely cannot be located on the same session application server. Calculating this, the nginx backend can only point directly to the application server, or build another squid, and then point to the application server. The best way is to use location to make a diversion, and pass some of the requests that require session through ip_hash Divert the traffic and go to other backends for the rest.

Related recommendations:

Nginx and PHP installation and configuration 5 strategies for nginx load balancing

Nginx and php installation and configuration 6-Nginx reverse proxy and load balancing deployment guide

Nginx and php installation and configuration 5-LINUX using PHPIZE Install PHP GD extension

The above is the detailed content of Nginx and PHP installation and configuration eight nginx session sharing. For more information, please follow other related articles on the PHP Chinese website!

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是生效的。生产环境:生产环境发布是

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

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

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

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

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
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor