Nginx+tomcat to achieve load balancing, using Redis session sharing _nginx

Source: Internet
Author: User
Tags install openssl session id openssl prepare svn nginx server server port install redis


Environmental preparedness



1, prepare a nginx server ip192.168.1.133 port 81



Installation process:


#First install dependencies:
yum -y install gcc-c ++
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl—devel
#Note: To install nginx, you must install as root
#Create a nginx directory
mkdir / usr / local / src / nginx
#Enter the nginx directory
cd / usr / local / src / nginx
#Download or upload the installation package
wget http://nginx.org/download/nginx.tar.gz or rz upload
#Unzip the installation package
tar -xvf nginx.tar.gz
#Enter the unzipped directory
cd nginx
# Below is the official installation
#Install nginx to the directory of the specified user
mkdir -p / ucenter / soft / nginx
#Installation configuration prefix is the installation directory user is the user group is the group
./configure --prefix = / ucenter / soft / nginx --user = ucenter --group = ucenter
#Compile
make
#installation
make install
#In the linux system, non-root users cannot occupy port 80, so ordinary users need to start nginx as root.
cd / ucenter / soft / nginx / sbin
#Modify all file owners under soft files to ucener -R for recursion
chown ucenter: ucenter ./soft/ -R
#Modify that ./nginx belongs to root
chown root nginx
#Enable ordinary users to use port 80 and enable nginx with root permissions
chmod u + s nginx
#Modify the configuration file Before modifying the configuration file, you must back up the file
cd conf /
# Pay attention to the working process of nginx, generally modify it according to the number of CPU cores
vim nginx.conf
#Close the firewall and open port 80
service iptables stop
#Start nginx
./nginx
#Restart nginx
./nginx -s reload
#Close nginx
./nginx -s stop
To prepare a tomcat server, prepare the java environment first, the steps of installing jdk are omitted

Then install three tomcat server IP addresses: 192.168.1.143, tomcat1 port 8080, tomcat2 port 8081, tomcat3 port 8082.



apache-tomcat-7.0.64 / conf / server.xml configuration file modify these three places so that the ports will not conflict
<Server port = "8005" shutdown = "SHUTDOWN">
<Connector port = "8080" protocol = "HTTP / 1.1"
connectionTimeout = "20000"
redirectPort = "8443" />
<Connector port = "8009" protocol = "AJP / 1.3" redirectPort = "8443" />
Modify the index.jsp in the tomcat ROOT directory, add the ID of each tomcat, and display the session ID on the page
<%-
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
<! DOCTYPE html>
<% @ page session = "true"%>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ("yyyy");
request.setAttribute ("year", sdf.format (new java.util.Date ()));
request.setAttribute ("tomcatUrl", "http://tomcat.apache.org/");
request.setAttribute ("tomcatDocUrl", "/ docs /");
request.setAttribute ("tomcatExamplesUrl", "/ examples /");
%>
<html lang = "en">
 <head>
  <title> <% = request.getServletContext (). getServerInfo ()%> </ title>
  <link href = "favicon.ico" rel = "icon" type = "image / x-icon" />
  <link href = "favicon.ico" rel = "shortcut icon" type = "image / x-icon" />
  <link href = "tomcat.css" rel = "stylesheet" type = "text / css" />
 </ head>

 <body>
  <div id = "wrapper">
   <div id = "navigation" class = "curved container">
    <span id = "nav-home"> <a href="${tomcatUrl}"> Home-<% = request.getSession (). getId ()%> </a> </ span>
    <span id = "nav-hosts"> <a href="${tomcatDocUrl}"> Documentation </a> </ span>
    <span id = "nav-config"> <a href="${tomcatDocUrl}config/"> Configuration </a> </ span>
    <span id = "nav-examples"> <a href="${tomcatExamplesUrl}"> Examples </a> </ span>
    <span id = "nav-wiki"> <a href="http://wiki.apache.org/tomcat/FrontPage"> Wiki </a> </ span>
    <span id = "nav-lists"> <a href="${tomcatUrl}lists.html"> Mailing Lists </a> </ span>
    <span id = "nav-help"> <a href="${tomcatUrl}findhelp.html"> Find Help </a> </ span>
    <br class="separator" />
   </ div>
   <div id = "asf-box">
    <h1> $ {pageContext.servletContext.serverInfo}-8080 </ h1>
   </ div>
   <div id = "upper" class = "curved container">
    <div id = "congrats" class = "curved container">
     <h2> If you're seeing this, you've successfully installed Tomcat. Congratulations! </ h2>
    </ div>
    <div id = "notice">
     <img src = "tomcat.png" alt = "[tomcat logo]" />
     <div id = "tasks">
      <h3> Recommended Reading: </ h3>
      <h4> <a href="${tomcatDocUrl}security-howto.html"> Security Considerations HOW-TO </a> </ h4>
      <h4> <a href="${tomcatDocUrl}manager-howto.html"> Manager Application HOW-TO </a> </ h4>
      <h4> <a href="${tomcatDocUrl}cluster-howto.html"> Clustering / Session Replication HOW-TO </a> </ h4>
     </ div>
    </ div>
    <div id = "actions">
     <div class = "button">
      <a class="container shadow" href="/manager/status"> <span> Server Status </ span> </a>
     </ div>
     <div class = "button">
      <a class="container shadow" href="/manager/html"> <span> Manager App </ span> </a>
     </ div>
     <div class = "button">
      <a class="container shadow" href="/host-manager/html"> <span> Host Manager </ span> </a>
     </ div>
    </ div>
    <!-
    <br class="separator" />
    ->
    <br class="separator" />
   </ div>
   <div id = "middle" class = "curved container">
    <h3> Developer Quick Start </ h3>
    <div class = "col25">
     <div class = "container">
      <p> <a href="${tomcatDocUrl}setup.html"> Tomcat Setup </a> </ p>
      <p> <a href="${tomcatDocUrl}appdev/"> First Web Apreplication </a> </ p>
     </ div>
    </ div>
    <div class = "col25">
     <div class = "container">
      <p> <a href="${tomcatDocUrl}realm-howto.html"> Realms & AAA </a> </ p>
      <p> <a href="${tomcatDocUrl}jndi-datasource-examples-howto.html"> JDBC DataSources </a> </ p>
     </ div>
    </ div>
    <div class = "col25">
     <div class = "container">
      <p> <a href="${tomcatExamplesUrl}"> Examples </a> </ p>
     </ div>
    </ div>
    <div class = "col25">
     <div class = "container">
      <p> <a href="http://wiki.apache.org/tomcat/Specifications"> Servlet Specifications </a> </ p>
      <p> <a href="http://wiki.apache.org/tomcat/TomcatVersions"> Tomcat Versions </a> </ p>
     </ div>
    </ div>
    <br class="separator" />
   </ div>
   <div id = "lower">
    <div id = "low-manage" class = "">
     <div class = "curved container">
      <h3> Managing Tomcat </ h3>
      <p> For security, access to the <a href="/manager/html"> manager webapp </a> is restricted.
      Users are defined in: </ p>
      <pre> $ CATALINA_HOME / conf / tomcat-users.xml </ pre>
      <p> In Tomcat 7.0 access to the manager application is split between
       different users. <a href="${tomcatDocUrl}manager-howto.html"> Read more ... </a> </ p>
      <br />
      <h4> <a href="${tomcatDocUrl}RELEASE-NOTES.txt"> Release Notes </a> </ h4>
      <h4> <a href="${tomcatDocUrl}changelog.html"> Changelog </a> </ h4>
      <h4> <a href="${tomcatUrl}migration.html"> Migration Guide </a> </ h4>
      <h4> <a href="${tomcatUrl}security.html"> Security Notices </a> </ h4>
     </ div>
    </ div>
    <div id = "low-docs" class = "">
     <div class = "curved container">
      <h3> Documentation </ h3>
      <h4> <a href="${tomcatDocUrl}"> Tomcat 7.0 Documentation </a> </ h4>
      <h4> <a href="${tomcatDocUrl}config/"> Tomcat 7.0 Configuration </a> </ h4>
      <h4> <a href="http://wiki.apache.org/tomcat/FrontPage"> Tomcat Wiki </a> </ h4>
      <p> Find additional important configuration information in: </ p>
      <pre> $ CATALINA_HOME / RUNNING.txt </ pre>
      <p> Developers may be interested in: </ p>
      <ul>
       <li> <a href="http://tomcat.apache.org/bugreport.html"> Tomcat 7.0 Bug Database </a> </ li>
       <li> <a href="${tomcatDocUrl}api/index.html"> Tomcat 7.0 JavaDocs </a> </ li>
       <li> <a href="http://svn.apache.org/repos/asf/tomcat/tc7.0.x/"> Tomcat 7.0 SVN Repository </a> </ li>
      </ ul>
     </ div>
    </ div>
    <div id = "low-help" class = "">
     <div class = "curved container">
      <h3> Getting Help </ h3>
      <h4> <a href="${tomcatUrl}faq/"> FAQ </a> and <a href="${tomcatUrl}lists.html"> Mailing Lists </a> </ h4>
      <p> The following mailing lists are available: </ p>
      <ul>
       <li id = "list-announce"> <strong> <a href="${tomcatUrl}lists.html#tomcat-announce"> tomcat-announce </a> <br />
        Important announcements, releases, security vulnerability notifications. (Low volume). </ Strong>
       </ li>
       <li> <a href="${tomcatUrl}lists.html#tomcat-users"> tomcat-users </a> <br />
        User support and discussion
       </ li>
       <li> <a href="${tomcatUrl}lists.html#taglibs-user"> taglibs-user </a> <br />
        User support and discussion for <a href="${tomcatUrl}taglibs/"> Apache Taglibs </a>
       </ li>
       <li> <a href="${tomcatUrl}lists.html#tomcat-dev"> tomcat-dev </a> <br />
        Development mailing list, including commit messages
       </ li>
      </ ul>
     </ div>
    </ div>
    <br class="separator" />
   </ div>
   <div id = "footer" class = "curved container">
    <div class = "col20">
     <div class = "container">
      <h4> Other Downloads </ h4>
      <ul>
       <li> <a href="${tomcatUrl}download-connectors.cgi"> Tomcat Connectors </a> </ li>
       <li> <a href="${tomcatUrl}download-native.cgi"> Tomcat Native </a> </ li>
       <li> <a href="${tomcatUrl}taglibs/"> Taglibs </a> </ li>
       <li> <a href="${tomcatDocUrl}deployer-howto.html"> Deployer </a> </ li>
      </ ul>
     </ div>
    </ div>
    <div class = "col20">
     <div class = "container">
      <h4> Other Documentation </ h4>
      <ul>
       <li> <a href="${tomcatUrl}connectors-doc/"> Tomcat Connectors </a> </ li>
       <li> <a href="${tomcatUrl}connectors-doc/"> mod_jk Documentation </a> </ li>
       <li> <a href="${tomcatUrl}native-doc/"> Tomcat Native </a> </ li>
       <li> <a href="${tomcatDocUrl}deployer-howto.html"> Deployer </a> </ li>
      </ ul>
     </ div>
    </ div>
    <div class = "col20">
     <div class = "container">
      <h4> Get Involved </ h4>
      <ul>
       <li> <a href="${tomcatUrl}getinvolved.html"> Overview </a> </ li>
       <li> <a href="${tomcatUrl}svn.html"> SVN Repositories </a> </ li>
       <li> <a href="${tomcatUrl}lists.html"> Mailing Lists </a> </ li>
       <li> <a href="http://wiki.apache.org/tomcat/FrontPage"> Wiki </a> </ li>
      </ ul>
     </ div>
    </ div>
    <div class = "col20">
     <div class = "container">
      <h4> Miscellaneous </ h4>
      <ul>
       <li> <a href="${tomcatUrl}contact.html"> Contact </a> </ li>
       <li> <a href="${tomcatUrl}legal.html"> Legal </a> </ li>
       <li> <a href="http://www.apache.org/foundation/sponsorship.html"> Sponsorship </a> </ li>
       <li> <a href = "http: //www.apache.org / foundation / thanks.html "> Thanks </a> </ li>
      </ ul>
     </ div>
    </ div>
    <div class = "col20">
     <div class = "container">
      <h4> Apache Software Foundation </ h4>
      <ul>
       <li> <a href="${tomcatUrl}whoweare.html"> Who We Are </a> </ li>
       <li> <a href="${tomcatUrl}heritage.html"> Heritage </a> </ li>
       <li> <a href="http://www.apache.org"> Apache Home </a> </ li>
       <li> <a href="${tomcatUrl}resources.html"> Resources </a> </ li>
      </ ul>
     </ div>
    </ div>
    <br class="separator" />
   </ div>
   <p class = "copyright"> Copyright © 1999-$ {year} Apache Software Foundation. All Rights Reserved </ p>
  </ div>
 </ body>

</ html>



At this time, modify the nginx configuration file nginx.conf, add the IP addresses and port numbers of three tomcats, and use nginx as a proxy
#user nobody;
worker_processes 1;

#error_log logs / error.log;
#error_log logs / error.log notice;
#error_log logs / error.log info;

#pid logs / nginx.pid;


events {
 worker_connections 1024;
}


http {
 include mime.types;
 default_type application / octet-stream;

 #log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request"'
 # '$ status $ body_bytes_sent "$ http_referer"'
 # '"$ http_user_agent" "$ http_x_forwarded_for"';

 #access_log logs / access.log main;

 sendfile on;
 #tcp_nopush on;

 #keepalive_timeout 0;
 keepalive_timeout 65;

 #gzip on;

 upstream localhost1 {
  
   #ip_hash;
   server 192.168.1.143:8080;
   server 192.168.1.143:8081;
   server 192.168.1.143:8082;
 }

 server {
  listen 81;
  server_name localhost;

  #charset koi8-r;

  #access_log logs / host.access.log main;

  location / {
    proxy_connect_timeout 3;
    proxy_send_timeout 30;
    proxy_read_timeout 30;
    proxy_pass http: // localhost1;
  }

  #error_page 404 /404.html;

  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \ .php $ {
  # proxy_pass http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \ .php $ {
  # root html;
  # fastcgi_pass 127.0.0.1:9000;
  # fastcgi_index index.php;
  # fastcgi_param SCRIPT_FILENAME / scripts $ fastcgi_script_name;
  # include fastcgi_params;
  #}

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  # deny all;
  #}
 }


 # another virtual host using mix of IP-, name-, and port-based configuration
 #
 #server {
 # listen 8000;
 # listen somename: 8080;
 # server_name somename alias another.alias;

 # location / {
 # root html;
 # index index.html index.htm;
 #}
 #}


 # HTTPS server
 #
 #server {
 # listen 443;
 # server_name localhost;

 # ssl on;
 # ssl_certificate cert.pem;
 # ssl_certificate_key cert.key;

 # ssl_session_timeout 5m;

 # ssl_protocols SSLv2 SSLv3 TLSv1;
 # ssl_ciphers HIGH:! aNULL:! MD5;
 # ssl_prefer_server_ciphers on;

 # location / {
 # root html;
 # index index.html index.htm;
 #}
 #}

}

At this time, start three tomcats and nginx respectively and visit http://192.168.1.133:81. At this time, every time you refresh the page, you will randomly visit 8080 or 8081 or 8082, and the session ids on the page are different Yes, how should we let these three tomcat share sessions, we use redis to do it.

At this time, install redis on the server 192.168.1.143 that has three tomcats. The installation steps are as follows:
$ wget http://download.redis.io/releases/redis-3.2.3.tar.gz
$ tar xzf redis-3.2.3.tar.gz
$ cd redis-3.2.3
$ make MALLOC = libc
#Start redis src in front of the installation path
$ src / redis-server &

#Close redis
src / redis-cli shutdown
#Use redis to put key value pairs key value
$ src / redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
$

After installing redis, upload the five dependent jars in the three tomcat lib folders, respectively.

commons-pool2-2.0.jar

jedis-2.5.2.jar

tomcat-redis-session-manager1.2.jar

tomcat-juli.jar

tomcat-juli-adapters.jar

All jars are here, download address

Then fix the three tomcat context.xml files, and add the following configuration

<? xml version = '1.0' encoding = 'utf-8'?>

<Context>

 <!-Default set of monitored resources->
 <WatchedResource> WEB-INF / web.xml </ WatchedResource>

 
 <!-Add redis session shared configuration here 6379 is the port of redis->
 <Valve className = "com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
 <Manager className = "com.orangefunction.tomcat.redissessions.RedisSessionManager"
 host = "127.0.0.1"
 port = "6379"
 database = "0"
 maxInactiveInterval = "60" />
</ Context>

At this time, restart the three tomcats and nginx respectively. After checking the tomcat log, it is found that there are no abnormal errors, indicating that we have succeeded, and then start testing.

We visit the nginx server address: http://192.168.1.133:81/

You get tomcat1 on port 8080, and the session id is 1A0625767F27BA95EF4D5F061FE0568D


At this time, press F5 to refresh the page and get tomcat2 on port 8081. The session id is still 1A0625767F27BA95EF4D5F061FE0568D


Refresh the page again and get tomcat3 on port 8082, and the session id is still 1A0625767F27BA95EF4D5F061FE0568D. ,



At this time, it means that we set up tomcat + nginx load balancing + redis session synchronization successfully!

nginx helps to evenly distribute our requests to three tomcats-"tomcat1, tomcat2 and tomcat3

 Redis helps us to synchronize sessions. In this way, our server performance will improve a lot. After any one tomcat fails, it will not affect the overall service.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope everyone supports the Yunqi community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.