


How 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:
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:
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:
Step 3: Start the nginx service
#sbin/nginx As shown in the picture:
Step 4: Our page access can display normal content, as shown in the picture:
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:
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:
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 '$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; <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!

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment
