Nginx reverse httpd, the implementation of three kinds of Tomcat proxy model to the back end of the Tomcat server, session binding of three ways

Source: Internet
Author: User
Tags session id tomcat server

Build the Tomcat cluster, the front-end of a nginx, to the back of the Apache server, the Apache is responsible for the back-end of the Tomcat server for resource scheduling, such a model than directly with Nginx back to the back-end host, the Tomcat server will receive less pressure, The service will be more stable, and such a model is tested in practice. If Nginx is dispatched directly to the back-end Tomcat server, only HTTP and HTTPS are supported, and the Ajp,http and HTTPS mode settings are not supported, allowing foreign clients to access the Tomcat server directly without having to go through our set of front-end nginx ports. This is a very unsafe practice, so the commonly used mode is AJP and MOD_JK, this mode can prevent foreign users directly access to the Tomcat server, security has a certain protection.

There are three types of httpd proxy modes, Proxy_http_module,proxy_ajp_module, and MOD_JK are implemented here. and load balancing the backend Tomcat host, session sticky,session Cluster,session server operation.

IP configuration:

Front-end Nginx:

eno16777736 192.168.72.3

eno33554976 172.16.25.2

HTTPD Scheduler:

eno16777736 172.16.25.1

Tomcata:

eno16777736 172.16.25.101

TOMCATB:

eno16777736 172.16.25.102


1. Load Balancer +session Sticky

①proxy_http_moudle

Nginx Anti-generation:

Only need to set Proxy_pass anti-generation can;

HTTPD configuration:

Set the cookie as a token at the header of the message, and when the Env route changes, it is triggered by Proxyset Stickysession, saving the session

Proxy Balancer://tcsrvs is the group of multiple servers that we set up in httpd, which is equivalent to the upstream in Nginx,

Haproxy in the backend. The domain name of both hosts needs to be written to the/etc/hosts in the HTTPD scheduler, otherwise the domain name cannot be resolved properly.

Route=tomcata

Route=tomcatb

Requires that we specify in the engine in the server.xml of the back-end Tomcat server, such as

Tomcat Server Configuration

Installing the Tomcat Service

There is a Tomcat installation package on the local CD

#yum install-y Tomcat Tomcat-webapps tomcat-lib Tomcat-admin-webapps Tomcat-docs-webapps

Customizing the Tomcat App

Such as:

#mkdir-pv/testapp2/webapps/root/{web-inf,lib,classes}

#vim/testapp2/webapps/root/index.jsp (set JSP page to test)

Modify the Tomcat Master profile Server.xml to allow access to the app we set by specifying a domain name

Visit 192.168.72.3

Because of the session sticky, I have always visited the same

When we put down the TOMCATB service,

This enables the Proxy_http_module-based HTTP mode

② based on Proxy_ajp_module

The AJP configuration differs from HTTP in that it is different for ports and protocols.

The configuration of the HTTPD scheduler needs to be modified to:

It is best to comment out the connector of the backend Tomcat about HTTP;

and modify the Defaulthost in the engine to point to the domain we set up

The access results are as above.


③ based on MOD_JK

First, you need to compile the mod_jk on the HTTPD scheduler because it doesn't exist by default and is downloaded from the Web mirroring site

Tomcat-connectors-version

#yun install-y "Development Tools" "Server Platform Develop"

Install the compilation environment.

#yum install-y Httpd-devel

#tar-XF tomcat-connectors-version.tar.gz

#cd tomcat-connectors-version/native

#./configure--with-apxs

#make-j 4 && make install

The MOD_JK is compiled and can be viewed through/USR/LIB64/HTTPD/MODULES/MOD_JK

Writing the MOD_JK configuration file

In the/etc/httpd/conf.d/jk_proxy_tomcat.conf

LoadModule Loading Module

Jkworkersfile for setting task configuration information

Jklogfile for setting up log files

Jkloglevel to set the log level

Jkmount to access any data, the group is processed by Tcsrvs, which is set in the Workers.properties task file;


/etc/httpd/conf.d/workers.properties

In the task file, you need to set up information for all Tomcat servers in the group, such as host,type,port,lbfactor weights, how many

How many groups are set by the Tomcat server.

Worker.tcsrvs.type is set as the polling method.

WORKER.TCSRVS.BALANCE_WORKERS=TOMCATA,TOMCATB list of servers used to set up load balancing

Worker.tcsrvs.sticky_session=0 indicates that session binding is not supported, note that the row cannot be omitted, otherwise it is bound by default, although it is not an error;

0 1 is required if session binding is required;


2. Load Balancer +session Server

The Tomcat server load balancing operation, using the HTTPD scheduler mod_jk form, about the front-end Nginx and HTTPD, configured with the front indistinguishable

Here is not to elaborate, mainly on the back end of two Tomcat server implementation of Seession server, the session is all placed on a server, when Tomcat

The server is taken from session server when needed, so the session server must be set to be highly available, or it will not function properly when the session server is hung.

Provide services.

To import the jar package required by the session server implementation, download the following jar file to the Lib directory in the Tomcat installation directory of each tomcat node, where ${version}

To replace the version number you need, tc${6,7,8} will be replaced with the same version number as the Tomcat version. Place these jar packages in the/usr/share/java/tomcat.

Set the context alias in the Tomcat Master Profile Server.xml and add the session server configuration segment in the host context

Context Context settings Access path path= "/test", its application is saved in/testapp2/webapps, so we need to manually configure

#mkdir-pv/testapp2/webapps/{lib,classes,web-inf}

#vim index.jsp (Create JSP file)

Copy the/etc/tomcat/web.xml to the web-inf that you created the app manually, and add <distributable/>

Memcachednodes is the node of Session server, several nodes are set up, Failovernodes is the backup node, when the N2 node is hung off

Top up. To say here, session server is implemented based on memcached, so the memcached cache must be installed first.

Set Jvmroute, the label should be the same as the previous httpd indistinguishable;

When accessed using the 8009 Port AJP protocol, the connector context of the 8080 port needs to be commented out.

To this one tomcat configuration, you only need to make the same settings on another host

Access results:

The session ID of the access content and the creation session time are the same.

3. Load Balancer +session Cluster

Session cluster implementation, for multiple Tomcat servers, backup each other's session, so

Each TOMCCAT server has all the sessions, so that even one tomcat or multiple tomcat

Once a server is down, the session will remain, but if all Tomcat

If the service is down, the session will not work properly.

Session cluster is implemented in the same way as session server, except for the cluster set in Server.xml

The context configuration of the group.

Such as:

The first half of the configuration that needs to be modified, the membership context is used to set the multicast communication address between the cluster, the port,

and duration, time-out, etc.;

The receiver context is used to set the IP address, port, time, and so on, for sending session data to and from each other cluster IP address

is the IP address available in the current server;

The second half of the configuration does not need to change.

Comment out the previous session server-related configuration.

Nginx reverse httpd, the implementation of three kinds of Tomcat proxy model to the back end of the Tomcat server, session binding of three ways

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.