Nginx+tomcat for load balancing and session sharing (Linux CENTOS7 environment)

Source: Internet
Author: User

One, Nginx installation

1. prepare three copies of Tomcat

TOMCAT1 Setting port 8080

TOMCAT2 Setting Port 8081

TOMCAT3 Setting Port 8082

2.

Download Nginx

3.

Unzip to/home directory and rename to Nginx

4.

Cd/home/nginx into Nginx directory

5.

./configure--with-http_stub_status_module

To initialize the configuration.

If prompted Pcre error, need to manually install Pcre, see http://www.linuxidc.com/Linux/2015-03/114986.htm

After installing Pcre, run again

./configure--with-http_stub_status_module

Initialize to

6.

make install to compile.

7.

Completing the sixth step will appear as the following tips

  ......  Nginx path prefix: "/usr/local/nginx"  nginx binary file: "/usr/local/nginx/sbin/nginx"  nginx Modules path: "/ Usr/local/nginx/modules "  nginx configuration prefix:"/usr/local/nginx/conf "  nginx configuration file:"/usr /local/nginx/conf/nginx.conf "  nginx pid file:"/usr/local/nginx/logs/nginx.pid "  nginx error log file:"/usr/ Local/nginx/logs/error.log "  nginx http Access log file:"/usr/local/nginx/logs/access.log "  nginx HTTP Client Request body Temporary files: "client_body_temp"  nginx http proxy temporary files: "proxy_temp"  nginx http Fast CGI Temporary files: "fastcgi_temp"  nginx http Uwsgi temporary files: "uwsgi_temp"  nginx http scgi temporary fil ES: "scgi_temp" ...  

8.

./usr/local/nginx/sbin/nginx Start Nginx

If you see the error described below, you need to configure the Pcre shared library in the case where the Pcre library is already installed. See here Http://www.linuxidc.com/Linux/2015-03/114985.htm the concrete operation.

./usr/local/nginx/sbin/nginx:error while loading shared libraries:libpcre.so.1:cannot open shared object File:no such File or directory

9.

Enter IP:80 in the browser and appear as shown to indicate that the installation was successful.

10.

Nginx Operation:

Start
./usr/local/nginx/sbin/nginx

Restart
./usr/local/nginx/sbin/nginx-s Reload

Close Nginx
./usr/local/nginx/sbin/nginx-s Stop

11.

Vi/usr/local/nginx/conf/nginx.conf modifying Nginx configuration

As follows:

Monitoring Access Path Ip:80/status

Configure the contents of the nginx.conf file later:

#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 Tomcat {server 127.0.0.1:8080 weight=1;          Server 127.0.0.1:8081 weight=1;    Server 127.0.0.1:8082 weight=1;        } server {Listen 80;        server_name localhost;        #charset Koi8-r;        #access_log Logs/host.access.log Main;            Location/{#root html; #index INDEx.html index.htm;              Proxy_pass Http://tomcat;                     Proxy_redirect default;             } location/status {stub_status on;         Access_log off;        } #error_page 404/404.html;        # REDIRECT Server error pages to the static page/50x.html # Error_page 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$ {# ProX        Y_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 {# lis    Ten 8000;    # Listen somename:8080;    # server_name somename alias Another.alias;    # location/{# root HTML;    # index index.html index.htm;    #} #} # HTTPS Server # #server {# listen 443 SSL;    # server_name localhost;    # ssl_certificate Cert.pem;    # Ssl_certificate_key Cert.key;    # Ssl_session_cache shared:ssl:1m;    # ssl_session_timeout 5m; # ssl_ciphers high:!anull:!    MD5;    # ssl_prefer_server_ciphers on;    # location/{# root HTML;    # index index.html index.htm; #    }    #}}

Second, set up the Tomcat cluster session sharing

1.

Modify the Conf/server.xml port in three tomcat directories

and Tomcat 123 for JVM 123, respectively.

2.

Launched Tomcat 123 and Nginx, respectively, to access the IP:80 entry project is successful

Session whether the shared Success Test JSP page code is: Refresh session unchanged description success.

<%@ Page ContentType="text/html; CHARSET=GBK" %><%@ Page Import="java.util.*" %><HTML><Head><title>Cluster App Test</title></Head><Body>Server Info:<%out.println (request.getlocaladdr ()+ " : " +Request.getlocalport ()+"<br>");%><%Out.println ("<br> ID" +Session.getid ()+"<br>"); //If there is a new Session property setStringDataname=Request.getparameter ("Dataname"); if(dataname! )= NULL &&dataname.length ()> 0) {    StringDataValue=Request.getparameter ("DataValue");  Session.setattribute (Dataname, DataValue); } out.println ("<b>session list </b><br>"); System.out.println ("============================"); Enumeration e=Session.getattributenames ();  while(E.hasmoreelements ()) {Stringname= (String) e.nextelement (); Stringvalue=Session.getattribute (name). toString (); OUT.PRINTLN (Name+ " = " +value+"<br>"); SYSTEM.OUT.PRINTLN (Name+ " = " +value); }%><formAction= "index.jsp"Method= "POST">Name:<inputtype=textsize=20name= "Dataname">  <BR>values:<inputtype=textsize=20name= "DataValue">  <BR>  <inputtype=submit></form></Body></HTML>

Nginx+tomcat for load balancing and session sharing (Linux CENTOS7 environment)

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.