Nginx and PHP installation and configuration eight nginx session sharing

不言
Release: 2023-03-23 19:38:02
Original
1313 people have browsed it

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.   }
Copy after login


##

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!