


Nginx 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.
2、sessionExists 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.
3、sessionexistmemcacheorRedis中
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.
4、nginx#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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
