Home> Database> Redis> body text

How nginx+redis realizes session sharing

WBOY
Release: 2023-05-26 12:49:06
forward
1480 people have browsed it

1. The first step is to install redis. My server is Windows. I downloaded the installation-free version. Just decompress it. The directory is as follows. At the beginning, redis does not require a password by default. If you want to set a password, you can go to the redis.windows.conf file and find requirepass, delete the # sign in front, and set the password after it.

How nginx+redis realizes session sharing

2. Enter the root directory of redis from cmd and type the following command: redis-server.exeredis.windows.conf. In this way, redis can be started. If the startup is successful, the following screen will appear. Of course, you can also modify the conf file and add a password. requirepass xxxxx

How nginx+redis realizes session sharing

#3. Next we can do some configuration work to achieve global caching of session data.

1) First, add the jar package. If you are a maven project, you need to add the following code to pom.xml

  org.springframework.session spring-session-data-redis 1.3.1.release pom 
Copy after login

If it is not a maven project, you need to add the following jar packages.

How nginx+redis realizes session sharing

2) Write redis.properties, the code is as follows

redis_isopen:yes #主机地址 redis_hostname=xxx.xxx.xxx.xxx #端口 redis_port=6379 #密码 redis_password=xxxxxxxx #连接超时时间 redis_timeout=200000 redis_maxidle:300 redis_maxactive:600 redis_maxwait:100000 redis_testonborrow:true
Copy after login

is basically similar to the connection statement for our configuration database.

3) Write the spring-redis.xml configuration file. This file configures some basic information about redis.

                   
Copy after login

4) In application.xml (spring’s main configuration file), you need to add scanning of the redis.properties configuration file, as follows.

    /web-inf/classes/redis.properties   
Copy after login

5) Introduce spring-redis.xml into the main configuration file, as follows.

Copy after login

6) In web.xml, add a filter about the session. Only in this way will the session be manipulated by redis.

 springsessionrepositoryfilter org.springframework.web.filter.delegatingfilterproxy   springsessionrepositoryfilter /* 
Copy after login

After this, we will realize the management of session by redis.

7) We can install a redis client to view the data inside, called redis desktop manager. As shown below, it is very useful and you can see the data in the redis database.

How nginx+redis realizes session sharing

#ps. When you exit again, you need to write like this to avoid errors. (ssh project)

public string yipinexit(){ iteratorkeys=session.keyset().iterator(); while(keys.hasnext()){ string key=keys.next(); session.remove(key); } return "yipinexit"; }
Copy after login

The above is the detailed content of How nginx+redis realizes session sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
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!