Nginx and Tomcat integration

Nginx and tomcat integration

A project to import

1.nginx advantages and features

nginx is a high-performance HTTP server and reverse proxy, but also a IMAP / POP3 / SMTP proxy server. It mainly has the following advantages

- 高并发连接: 
官方测试能够支撑5万并发连接,在实际生产环境中跑到2~3万并发连接数。
- 内存消耗少: 
在3万并发连接下,开启的10个Nginx 进程才消耗150M内(15M*10=150M)。
- 配置文件非常简单: 
风格跟程序一样通俗易懂。
- 成本低廉: 
Nginx为开源软件,可以免费使用。而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则需要十多万至几十万人民币。
- 支持Rewrite重写规则: 
能够根据域名、URL的不同,将 HTTP 请求分到不同的后端服务器群组。
- 内置的健康检查功能: 
如果 Nginx Proxy 后端的某台 Web 服务器宕机了,不会影响前端访问。
- 节省带宽: 
支持 GZIP 压缩,可以添加浏览器本地缓存的 Header 头。
- 稳定性高: 
用于反向代理,宕机的概率微乎其微

Due to good performance nginx, so many large domestic companies are in use, the main reason is nginx is an open source free. In addition to a series of functions described above, the project is mainly used nginx to achieve the following three functions:

- 动静分离
- 反向代理
- 负载均衡
- 网页、图片缓存

1.2 nginx load balancing There are five main strategies

- 轮询(默认) 
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
- weight 
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
- ip_hash 
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
- fair(第三方) 
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
- url_hash(第三方) 
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

Second, install Nginx

yum -y install gcc gcc-c++ pcre-devel openssl-devel openssl wget
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx
make
make install

Third, install Tomcat

3.1 jdk install and test environment

[root@ c6s02 ~]# tail -5 /etc/profile
####java_env####
export JAVA_HOME=/usr/local/jdk1.8.0_60
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

source /etc/profile

[root@ c6s02 ~]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Installation Tomcat 3.2 (two nodes)

tar -zxvf apache-tomcat-7.0.47.tar.gz

#新建tomcat01项目
mkdir -p /opt/tomcat01
cp -a apache-tomcat-7.0.47/* /opt/tomcat01/

#新建tomcat02项目,并修改三处端口为18005,18080,18009
mkdir -p /opt/tomcat021
cp -a apache-tomcat-7.0.47/* /opt/tomcat02/

Manually creating a test page and test

echo 'this is tomcat01' >/opt/tomcat01/webapps/ROOT/index.jsp
echo 'this is tomcat02' >/opt/tomcat02/webapps/ROOT/index.jsp
curl  10.0.0.22:8080/index.jsp
curl  10.0.0.22:18080/index.jsp

Fourth, through the combination of Tomcat and Nginx

Install nginx configuration file and modify Nignx

vim /usr/local/nginx/conf/nginx.conf

Add the HTTP module:

upstream tomcat {     #定义服务器组tomcat
    server 10.0.0.22:8080;    #定义后Tomcat端服务器
    server 10.0.0.22:18080;
}

In the server module to add:

location ~ \.jsp$ {   #URL正则匹配,匹配jsp结尾的所有URL
	proxy_pass   http://tomcat;   #proxy_pass反向代理参数,将匹配到的请求反向代理到tomcat服务器组!
}

Here Insert Picture Description

Start nginx and test

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx

Here Insert Picture Description

5, Tomcat Performance Optimization

5.1 tomcat memory optimization

linux modify TOMCAT_HOME / bin / catalina.sh, added in front of the

JAVA_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=128mM -XX:MaxPermSize=1024m"

-server: Be sure to as the first parameter, good performance when a plurality of the CPU
-Xms: Heap initial heap size, the minimum memory usage, cpu high performance value should be set to larger
-Xmx: java heap maximum, the above two values maximum memory allocation used by the JVM is the minimum and maximum memory, hardware, depending on the size of physical memory, recommendations are set to half of physical memory.
-XX: PermSize: setting memory permanent preservation areas
-XX: MaxPermSize: setting the maximum memory of the permanent preservation area

5.2 tomcat thread optimization

Objective: To improve concurrency

Modify the configuration file server.xml tomcat

<Connector port="80" protocol="HTTP/1.1" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700"
connectionTimeout="20000" redirectPort="8443" />

maxThreads = "600" # maximum number of threads
minSpareThreads = number of threads created when "100" # Initialization
maxSpareThreads = "500" # Once you have created a thread more than this value, Tomcat will close the socket threads are no longer needed.
acceptCount = "700" # Specify When all requests can be processed using the number of threads are used, the number of requests may be placed in the queue process, more than the number of the request will not be processed

5.3 Setting the session expiration time

conf \ web.xml specified by the parameters:

<session-config>   
    <session-timeout>180</session-timeout>     
</session-config> 

In minutes.

Published 13 original articles · won praise 0 · Views 224

Guess you like

Origin blog.csdn.net/liuhuanboke/article/details/104893782