Nginx as the most popular open source reverse proxy HTTP Server , which is used to implement resource caching, Web Server load balancing functions, due to its lightweight, high performance, high reliability and other features in InternetThe project has a very common application, the relevant concepts on the internet has a wealth of introduction. Distributed Web Server after cluster deployment, you need to implement Session shared, for Tomcat There are various implementations of the server, such as Tomcat cluster Session Broadcast, Nginx IP Hash policies, nginx Sticky Module and other programs, this paper mainly introduces the use Redis Server to Session shared scenarios for unified storage management.
Related Application Structure reference:
Second, the Environment configuration
Test environment based on Linux CentOS 6.5, please install tomcat,redis,nginx First The relevant environment, without detailed description, the test configuration of this article is as follows:
|
Version |
Ip_port |
Nginx |
1.6.2 |
10.129.221.70:80 |
Tomcat_1 |
7.0.54 |
10.129.221.70:8080 |
Tomcat_2 |
7.0.54 |
10.129.221.70:9090 |
Redis |
2.8.19 |
10.129.221.70:6379 |
Iii. Construction of Tomcat-redis-session-manager-master
1 , because the source code is built based on Gradle , first Configure Gradle environment.
2 , from GitHub Get Tomcat-redis-session-manager-master source code, address as follows:
View Source Print ?
1.https://github.com/jcoleman/tomcat-redis-session-manager
3 , find the source of the Build.gradle file, as the author uses a third-party repository ( Sonatype ), need to register account, too cumbersome, comments directly after use maven central Repository, while annotating the signature-related script and increasing the output script of the dependent package Copyjars ( Dist directory), the modified Build.gradle The file is as follows:
View Source Print ?
001.apply plugin: ' java '
002.apply plugin: ' maven '
003.apply plugin: ' Signing '
004.
005.group = ' com.orangefunction '
006.version = ' 2.0.0 '
007.
008.repositories {
009.mavenCentral ()
010.}
011.
012.compileJava {
013.sourceCompatibility = 1.7
014.targetCompatibility = 1.7
015.}
016.
017.dependencies {
018.compile group: ' Org.apache.tomcat ', Name: ' Tomcat-catalina ', version: ' 7.0.27 '
019.compile group: ' Redis.clients ', Name: ' Jedis ', version: ' 2.5.2 '
020.compile group: ' Org.apache.commons ', Name: ' Commons-pool2 ', Version: ' 2.2 '
021.//compile group: ' Commons-codec ', Name: ' Commons-codec ', version: ' 1.9 '
022.
023.testCompile group: ' JUnit ', Name: ' JUnit ', version: ' 4.+ '
024.testCompile ' org.hamcrest:hamcrest-core:1.3 '
025.testCompile ' org.hamcrest:hamcrest-library:1.3 '
026.testCompile ' org.mockito:mockito-all:1.9.5 '
027.testCompile group: ' Org.apache.tomcat ', Name: ' Tomcat-coyote ', version: ' 7.0.27 '
028.}
029.
030.task Javadocjar (Type:jar, Dependson:javadoc) {
031.classifier = ' Javadoc '
032.from ' Build/docs/javadoc '
033.}
034.
035.task Sourcesjar (Type:jar) {
036.from SourceSets.main.allSource
037.classifier = ' sources '
038.}
039.
040.artifacts {
041.archives jar
042.
043.archives Javadocjar
044.archives Sourcesjar
045.}
046.
047.//signing {
048.//Sign Configurations.archives
049.//}
050.
051.task Copyjars (type:copy) {
052.from Configurations.runtime
053.into ' Dist '
054.}
055.
056.uploadArchives {
057.repositories {
058.mavenDeployer {
059.beforeDeployment {mavendeployment Deployment-signing.signpom (Deployment)}
060.
061.//repository (url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
062.//Authentication (username:sonatypeusername, Password:sonatypepassword)
063.//}
064.//repository (url: 'https://oss.sonatype.org/content/repositories/snapshots') {
065.//Authentication (username:sonatypeusername, Password:sonatypepassword)
066.//}
067.
068.pom.project {
069.name ' Tomcat-redis-session-manager '
070.packaging ' jar '
071.description ' Tomcat Redis Session Manager is a tomcat extension to store sessions in Redis '
072.url 'https://github.com/jcoleman/tomcat-redis-session-manager'
073.
074.issueManagement {
075.url 'https://github.com: Jcoleman/tomcat-redis-session-manager/issues '
076.system ' GitHub issues '
077.}
078.
079.SCM {
080.url 'https://github.com: Jcoleman/tomcat-redis-session-manager '
081.connection ' scm:git:git://github.com/jcoleman/tomcat-redis-session-manager.git'
082.developerConnection ' Scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git '
083.}
084.
085.licenses {
086.license {
087.name ' MIT '
088.url 'Http://opensource.org/licenses/MIT'
089.distribution ' Repo '
090.}
091.}
092.
093.developers {
094.developer {
095.id ' Jcoleman '
096.name ' James Coleman '
097.email ' jtc331@gmail.com '
098.url 'https://github.com/jcoleman'
099.}
100.}
101.}
102.}
103.}
104.}
4 , Execution Gradle command to build the source code, compile the output Tomcat-redis-session-manager-master and Dependence Jar Package
View Source Print ?
1.gradle build-x Test Copyjars
All output list files are as follows:
Four,tomcat configuration
Install two tomcat Web servers, modify the Connector port number to 8080 and 9090 , and make sure that all works, of course, if it's distributed across different hosts, you can use the same port number.
V. Writing test pages
To differentiate 2 Tomcat visits, write the pages separately and package the deployment:
1 , for tomcat_1 Write a test page that shows "Response from Tomcat_1" , while the page provides a button to display the current Session value, packaged and published to tomcat_1 server;
2 , for tomcat_2 Write a test page that shows "Response from Tomcat_2" , while the page provides a button to display the current Session value, packaged and published to tomcat_2 server;
The http://10.129.221.70:8080 and http://10.129.221.70:9090 addresses are accessed separately because the access is different Web server, so each display different page content and session value must be different.
Vi.Tomcat Session Manager configuration
Modifying the configuration using tomcat-redis-session-manager-master as the Tomcat session Manager
1 , the third step is generated separately. Tomcat-redis-session-manager-master and Dependence Jar Package Overlay to Tomcat of the installation directory Lib folder
2 , modify separately 2 Desk Tomcat of the context.xml file to make Tomcat-redis-session-manager-master as a Session manager, specifying both Redis address and Port.
context.xml Add the following configuration:
View Source Print ?
1.
2.
3. < p="">
4.host= ' localhost '
5.port= ' 6379 '
6.database= ' 0 '
7.maxinactiveinterval= '/>
8.
3 , restart separately 2 Desk Tomcat server.
Seven,nginx configuration
1 , modify default.conf configuration file, enabling Upstream Load Balancing Tomcat Cluster , the polling method is used by default.
View Source Print ?
01.upstream Site {
Ip_hash; distribution based on Ip_hash
02.server localhost:8080;
03.server localhost:9090;
04.}
05.
06.server {
07.listen 80;
08.server_name localhost;
09.
#charset Koi8-r;
One. #access_log/var/log/nginx/log/host.access.log main;
12.
13.location/{
#root/usr/share/nginx/html;
#index index.html index.htm;
16.index index_tel.http://www.it165.net/pro/webjsp/"target=" _blank "class=" Keylink ">jsp index.http:// www.it165.net/pro/webjsp/"target=" _blank "class=" Keylink ">jsp index.html index.htm;
17.proxy_redirect off;
18.proxy_set_header Host $host;
19.proxy_set_header X-real-ip $remote _addr;
20.proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
21.client_max_body_size 10m;
22.client_body_buffer_size 128k;
23.proxy_buffers 4k;
24.proxy_connect_timeout 3;
25.proxy_send_timeout 30;
26.proxy_read_timeout 30;
27.proxy_pass Http://site;
28.
29.}
30.
#error_page 404/404.html;
32.
33.# Redirect Server error pages to the static page/50x.html
34.#
35.error_page 502 503 504/50x.html;
36.location =/50x.html {
37.root/usr/share/nginx/html;
38.}
39.
40.# Proxy The PHP scripts to Apache listening on 127.0.0.1:80
41.#
#location ~. php$ {
43.# Proxy_pass http://127.0.0.1;
44.#}
45.
46.# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
47.#
#location ~. php$ {
49.# root html;
50.# Fastcgi_pass 127.0.0.1:9000;
51.# Fastcgi_index index.php;
52.# Fastcgi_param Script_filename/scripts$fastcgi_script_name;
53.# include Fastcgi_params;
54.#}
55.
56.# deny access to. htaccess files, if Apache ' s document Root
57.# concurs with Nginx ' s one
58.#
#location ~/.ht {
60.# deny all;
61.#}
62.}
2 , Nginx Reload Configuration
View Source Print ?
1.nginx-s Reload
Eight, configure Tomcat
The saved session entity is placed in Redis andTomcat can record Session-id The value is compared to Redis ,Session-id is obtained from the cookie , the different the location of the Tomcat storage Sessioncookie is different, so you must modify all of the Tomcat Conf/context.xml, modify the contents as follows:
<>Sessioncookiepath= "/" >
Nine, test results
1 , Access http://10.129.221.70:8080 Direct request to tomcat_1 Server,
Show "Response from Tomcat_1"with session value ' 56e2fae376a47f1c0961d722326b8423 ';
2 , Access http://10.129.221.70:9090 Direct request to tomcat_2 Server,
Show "Response from Tomcat_2"with session value ' 56e2fae376a47f1c0961d722326b8423 ' /c10>;
3 , Access http://10.129.221.70 (Default the port) Request to Nginx reverse proxy to the specified Web server, due to the default use of polling load mode,
Repeatedly refreshing the page displays the switch between "response from Tomcat_1" and "response from Tomcat_2" , but The 6>session value remains ' 56e2fae376a47f1c0961d722326b8423 ' ;
4 , using redis-cli Connection Redis server, the view will display a "56e2fae376a47f1c0961d722326b8423" key of the Session data, value to serialize the data.
At this point, the session consistency of Tomcat cluster is realized based on nginx load Balancing .
Boot order:redis--nginx--Tomcat
Redis startup script =/usr/locat/redis.2.0.1/src/redis-server
Nginx+tomcat+redis Implementing session Sharing
The above describes the Nginx+tomcat+redis implementation session sharing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.