search
HomeOperation and MaintenanceNginxHow Nginx and Tomcat achieve dynamic and static separation and load balancing

one. Introduction to nginx:

nginx is a high-performance http and reverse proxy server with high stability and supports hot deployment and easy module expansion. When encountering a peak of access, or someone maliciously initiates a slow connection, it is also likely to cause the server's physical memory to be exhausted and frequently exchanged, resulting in loss of response. The server can only be restarted. nginx adopts a phased resource allocation technology to process static files and Cache-free reverse proxy acceleration achieves load balancing and fault tolerance, and can withstand high concurrency processing in such high-concurrency access situations.

two. nginx installation and configuration

The first step: download the nginx installation package

The second step: install nginx on linux

#tar zxvf nginx-1.7.8.tar.gz //解压

#cd nginx-1.7.8

#./configure --with-http_stub_status_module --with-http_ssl_module//启动server状态页和https模块

will report a missing pcre library error, As shown in the picture:

How Nginx and Tomcat achieve dynamic and static separation and load balancing

At this time, first perform the third step to install pcre, and then execute it in 3, and that’s it

4 .make && make install //Compile and install

5. Test whether the installation configuration is correct. nginx is installed in /usr/local/nginx

#/usr/local/nginx/sbin/ nginx -t, as shown in the picture:

How Nginx and Tomcat achieve dynamic and static separation and load balancing

Step 3: Install pcre on linux

#tar zxvf pcre-8.10.tar.gz //解压

cd pcre-8.10

./configure

make && make install//编译并安装

Three. nginx tomcat implements dynamic and static separation

Dynamic and static separation means that nginx processes the static pages (html pages) or pictures requested by the client, and tomcat processes the dynamic pages (jsp pages) requested by the client, because nginx processes The efficiency of static pages is higher than tomcat.

Step one: We need to configure the nginx file

#vi /usr/local/nginx/conf/nginx.conf

 #user nobody; 
worker_processes 1; 
error_log logs/error.log; 
pid    logs/nginx.pid; 
 
events { 
  use epoll; 
  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; 
keepalive_timeout 65; 
gzip on;  
gzip_min_length 1k;  
gzip_buffers   4 16k;  
gzip_http_version 1.0;  
gzip_comp_level 2;  
gzip_types text/plain application/x-javascript text/css application/xml;  
gzip_vary on;  
  server { 
    listen    80 default; 
    server_name localhost; 
    <span style="color:#ff0000;"> location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ //由nginx处理静态页面</span> 
       {  
         root  /usr/tomcat/apache-tomcat-8081/webapps/root;  
          expires   30d; //缓存到客户端30天 
        }  
    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; 
    } 
     <span style="color:#ff0000;"> location ~ \.(jsp|do)$ {//所有jsp的动态请求都交给tomcat处理 </span> 
      <span style="color:#ff0000;"> proxy_pass http://192.168.74.129:8081; //来自jsp或者do的后缀的请求交给tomcat处理</span> 
      proxy_redirect off; 
      proxy_set_header host $host;  //后端的web服务器可以通过x-forwarded-for获取用户真实ip 
      proxy_set_header x-real-ip $remote_addr; 
      proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
      client_max_body_size 10m;  //允许客户端请求的最大单文件字节数 
      client_body_buffer_size 128k; //缓冲区代理缓冲用户端请求的最大字节数 
       proxy_connect_timeout 90;  //nginx跟后端服务器连接超时时间 
       proxy_read_timeout 90;   //连接成功后,后端服务器响应时间 
       proxy_buffer_size 4k;   //设置代理服务器(nginx)保存用户头信息的缓冲区大小 
       proxy_buffers 6 32k;    //proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 
      proxy_busy_buffers_size 64k;//高负荷下缓冲大小(proxy_buffers*2) 
      proxy_temp_file_write_size 64k; //设定缓存文件夹大小,大于这个值,将从upstream服务器传 
    } 
    
  }  
 
}

Step two: In Create a new index.html static page under webapps/root under tomcat, as shown in the figure:

How Nginx and Tomcat achieve dynamic and static separation and load balancing

Step 3: Start the nginx service

#sbin/nginx As shown in the picture:

How Nginx and Tomcat achieve dynamic and static separation and load balancing

Step 4: Our page access can display normal content, as shown in the picture:

How Nginx and Tomcat achieve dynamic and static separation and load balancing

Step 5: Test the performance of nginx and tomcat in processing static pages under high concurrency?

Used the linux ab website stress test command to test the performance

1. Test the performance of nginx in processing static pages

ab -c 100 -n 1000

This means processing 100 requests at the same time and running the index.html file 1000 times, as shown in the figure:

How Nginx and Tomcat achieve dynamic and static separation and load balancing

2. Test tomcat processing static pages Performance

ab -c 100 -n 1000

This means processing 100 requests at the same time and running the index.html file 1000 times, as shown in the figure:

How Nginx and Tomcat achieve dynamic and static separation and load balancing

For the same processing of static files, the static performance of nginx processing is better than that of tomcat. nginx can request 5388 times per second, while tomcat only requests 2609 times.

Summary: In the nginx configuration file, we hand over static configuration to nginx for processing, and hand over dynamic requests to tomcat, which improves performance.

Four. nginx tomcat load balancing and fault tolerance

In the case of high concurrency, in order to improve the performance of the server and reduce the concurrency pressure on a single server, we adopt cluster deployment, which can also solve the problem of avoiding single When a server hangs up and the service cannot be accessed, fault tolerance issues are dealt with.

The first step: We deployed the tomcat server here for two days, 192.168.74.129:8081 and 192.168.74.129:8082

The second step: nginx serves as the proxy server, and the client requests On the server side, load balancing is used to handle it, so that the customer service requests can be evenly distributed to the servers every day, thus reducing the pressure on the server side. Configure the nginx.conf file under nginx.

#vi /usr/local/nginx/conf/nginx.conf

 #user nobody; 
worker_processes 1; 
error_log logs/error.log; 
pid    logs/nginx.pid; 
 
events { 
  use epoll; 
  worker_connections 1024; 
} 
 
 
http { 
  include    mime.types; 
  default_type application/octet-stream; 
  log_format main &#39;$remote_addr - $remote_user [$time_local] "$request" &#39; 
           &#39;$status $body_bytes_sent "$http_referer" &#39; 
           &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;; 
 
  access_log logs/access.log main; 
  sendfile    on; 
keepalive_timeout 65; 
gzip on;  
gzip_min_length 1k;  
gzip_buffers   4 16k;  
gzip_http_version 1.0;  
gzip_comp_level 2;  
gzip_types text/plain application/x-javascript text/css application/xml;  
gzip_vary on;  
<span style="color:#ff0000;">upstream localhost_server { 
    ip_hash; 
    server 192.168.74.129:8081; 
    server 192.168.74.129:8082; 
  }</span> 
 
  server { 
    listen    80 default; 
    server_name localhost; 
    <span style="color:#ff0000;"> location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ //由nginx处理静态页面</span> 
       {  
         root  /usr/tomcat/apache-tomcat-8081/webapps/root;  
          expires   30d; //缓存到客户端30天 
        }  
    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; 
    } 
     <span style="color:#ff0000;">location ~ \.(jsp|do)$ {//所有jsp的动态请求都交给tomcat处理 </span> 
      <span style="color:#ff0000;">proxy_pass http://localhost_server; //来自jsp或者do的后缀的请求交给tomcat处理</span> 
      proxy_redirect off; 
      proxy_set_header host $host;  //后端的web服务器可以通过x-forwarded-for获取用户真实ip 
      proxy_set_header x-real-ip $remote_addr; 
      proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
      client_max_body_size 10m;  //允许客户端请求的最大单文件字节数 
      client_body_buffer_size 128k; //缓冲区代理缓冲用户端请求的最大字节数 
       proxy_connect_timeout 90;  //nginx跟后端服务器连接超时时间 
       proxy_read_timeout 90;   //连接成功后,后端服务器响应时间 
       proxy_buffer_size 4k;   //设置代理服务器(nginx)保存用户头信息的缓冲区大小 
       proxy_buffers 6 32k;    //proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 
      proxy_busy_buffers_size 64k;//高负荷下缓冲大小(proxy_buffers*2) 
      proxy_temp_file_write_size 64k; //设定缓存文件夹大小,大于这个值,将从upstream服务器传 
    } 
    
  }  
 
}

Explanation:

1. The server in upstream points to the server IP (domain name) and port, followed by parameters

1)weight: Set the forwarding weight of the server to a default value of 1.

2)max_fails: It is used in conjunction with fail_timeout. It means that within the fail_timeout time period, if the number of server forwarding failures exceeds the value set by max_fails, the server will not be available. The default value of max_fails is 1

3)fail_timeout: Indicates how many times the forwarding fails within this time period before the server is considered unavailable.

4)down: Indicates that this server cannot be used.

5) backup: Indicates that the ip_hash setting is invalid for this server. The request will be forwarded to the server only after all non-backup servers have failed.

2. The ip_hash setting is in the cluster server. If the same client request is forwarded to multiple servers, each server may cache the same information, which will cause a waste of resources. The ip_hash setting is used When the same client requests the same information for the second time, it will be forwarded to the server that requested the first time. But ip_hash cannot be used together with weight.

The above is the detailed content of How Nginx and Tomcat achieve dynamic and static separation and load balancing. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
内存飙升!记一次nginx拦截爬虫内存飙升!记一次nginx拦截爬虫Mar 30, 2023 pm 04:35 PM

本篇文章给大家带来了关于nginx的相关知识,其中主要介绍了nginx拦截爬虫相关的,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

nginx限流模块源码分析nginx限流模块源码分析May 11, 2023 pm 06:16 PM

高并发系统有三把利器:缓存、降级和限流;限流的目的是通过对并发访问/请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务(定向到错误页)、排队等待(秒杀)、降级(返回兜底数据或默认数据);高并发系统常见的限流有:限制总并发数(数据库连接池)、限制瞬时并发数(如nginx的limit_conn模块,用来限制瞬时并发连接数)、限制时间窗口内的平均速率(nginx的limit_req模块,用来限制每秒的平均速率);另外还可以根据网络连接数、网络流量、cpu或内存负载等来限流。1.限流算法最简单粗暴的

nginx+rsync+inotify怎么配置实现负载均衡nginx+rsync+inotify怎么配置实现负载均衡May 11, 2023 pm 03:37 PM

实验环境前端nginx:ip192.168.6.242,对后端的wordpress网站做反向代理实现复杂均衡后端nginx:ip192.168.6.36,192.168.6.205都部署wordpress,并使用相同的数据库1、在后端的两个wordpress上配置rsync+inotify,两服务器都开启rsync服务,并且通过inotify分别向对方同步数据下面配置192.168.6.205这台服务器vim/etc/rsyncd.confuid=nginxgid=nginxport=873ho

nginx php403错误怎么解决nginx php403错误怎么解决Nov 23, 2022 am 09:59 AM

nginx php403错误的解决办法:1、修改文件权限或开启selinux;2、修改php-fpm.conf,加入需要的文件扩展名;3、修改php.ini内容为“cgi.fix_pathinfo = 0”;4、重启php-fpm即可。

如何解决跨域?常见解决方案浅析如何解决跨域?常见解决方案浅析Apr 25, 2023 pm 07:57 PM

跨域是开发中经常会遇到的一个场景,也是面试中经常会讨论的一个问题。掌握常见的跨域解决方案及其背后的原理,不仅可以提高我们的开发效率,还能在面试中表现的更加

nginx部署react刷新404怎么办nginx部署react刷新404怎么办Jan 03, 2023 pm 01:41 PM

nginx部署react刷新404的解决办法:1、修改Nginx配置为“server {listen 80;server_name https://www.xxx.com;location / {root xxx;index index.html index.htm;...}”;2、刷新路由,按当前路径去nginx加载页面即可。

Linux系统下如何为Nginx安装多版本PHPLinux系统下如何为Nginx安装多版本PHPMay 11, 2023 pm 07:34 PM

linux版本:64位centos6.4nginx版本:nginx1.8.0php版本:php5.5.28&php5.4.44注意假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不同安装目录即可。安装php#wgethttp://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror#tarzxvfphp-5.4.44.tar.gz#cdphp-5.4.44#./configure--pr

nginx怎么禁止访问phpnginx怎么禁止访问phpNov 22, 2022 am 09:52 AM

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools