nginx + Tomcat load balancing -- Part 2

1, In general, a Tomcat site can not cope with the complex and diverse requests of too many customers due to possible single point of failure. It cannot be used in production environment alone. So we need a more reliable solution to improve the web site architecture. At present, many large enterprises use nginx server as direction agent and load balancer of back-end web applications. To improve the load concurrency of the entire site.

2, The previous article showed you how to install Tomcat. In this article, there are not too many narratives! (Tomcat2 is the same as the previous installation step. Just modify the test file of index.jsp)

3, nginx server installation and configuration

yum -y install pcre-devel zlib-devel openssl-devel  //nginx related packages
groupadd nginxgroup
useradd -g nginxgroup -s /bin/false nginx
tar zxf nginx-1.6.....gz
cd nginx-1.6.0
./configure --prefix=/usr/local/nginx --user=nginx --group=nginxgroup --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module  && make && make install
//Note: - with file AIO / / support for modifying startup files
    --with-http_stub_status_module   //Enable status statistics
    --with-http_gzip_static_module   //Enable gzip static compression
    --with-http-flv-module        //Enable flv module to provide time-based offset file for memory usage
    --with-http-ssl-module        //Enable ssl module

2)vim  /usr/local/nginx/conf/nginx.conf

1.//    Add the following code to http {....}. Set the list of servers for load balancing. The weight parameter represents the weight. The higher the weight, the greater the probability of being assigned.
upstream tomcat_server {
                    server weight=1 max_fails=2 fail_timeout=30s;
                    server weight=1 max_fails=2 fail_timeout=30s;

2.//   Add a line to http {...} - server {...} - location / {...}
proxy_pass        http://tomcat_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;

 location = / {
            #Judge whether it is mobile terminal
            if ($http_user_agent ~* '(iPhone|ipod|iPad|Android|Windows Phone|Mobile|Nokia)') {
                rewrite . break;
            rewrite . break;

     //The above configuration is to proxy the default site to the set Tomcat? Server load balancing server group through the proxy? Pass method.

4) If you want to do dynamic static separation, just add the following configuration in nginx.

#Configure Nginx dynamic and static separation   
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$   
root /data/www;   
#expires defines the cache time of the user's browser as 3 days. If the static page is not updated frequently, it can be set longer, which can save bandwidth and relieve the pressure of the server   
expires      3d;   

3) detection

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ps aux | grep nginx
netstat -antpu |grep nginx

4) Just visit

Tags: Nginx Tomcat SSL Mobile

Posted on Wed, 08 Jan 2020 07:30:34 -0800 by irving