Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of dynamic and static Separation of tomcat by nginx

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Why is it necessary to realize the separation of movement and movement?

1) nginx has a strong ability to deal with static resources

The main reason is that the efficiency of nginx in processing static pages is much higher than that of tomcat. If the number of requests of tomcat is 1000, the number of requests of nginx is 6000, and the throughput of tomcat per second is 0.6m. The throughput of tomcat is 3.6m per second. It can be said that the capacity of nginx to handle static resources is six times that of tomcat, which shows its advantages.

2) dynamic resources and static resources are separated to make the structure of the server clearer.

two。 Principle of static and dynamic separation

Some of the requests received by the server from the client are static resources, such as html,css,js and picture resources, and some are dynamic data requests. Because tomcat is slow to process static resources, we can consider separating all static resources to servers that handle static resources faster, such as nginx, and handing over dynamic requests to tomcat.

As shown in the figure below, we installed both nginx and tomcat on the machine, placed all static resources under the webroot directory of nginx and dynamically requested programs under the webroot directory of tomcat. When the client accesses the server, if it is a request for static resources, it goes directly to the webroot directory of nginx to obtain resources. If it is a request for dynamic resources, nginx uses the principle of reverse proxy. The request is forwarded to the tomcat for processing, so that the static and dynamic separation is realized, and the performance of the server in processing the request is improved.

3. Detailed configuration of static and dynamic separation

1) first familiarize yourself with the important configuration file nginx.conf of nginx

User nginx; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events {worker_connections 1024;} http {include mime.types; default_type application/octet-stream # Log format definition 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 compression function sets 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-javascripttext/css application/xml; gzip_vary on; server {listen 80; server_name www.test.com; location / {# jsp website program root directory, usually nginx and tomcat are in the same directory root / usr/local/tomcat/webapps/ROOT; index index.html index.jsp index.html } location ~. * .jsp$ {index index.jsp; proxy_pass http://127.0.0.1:8080; # from the jsp request to tomcat for processing proxy_redirect off; proxy_set_header Host $host; # the Web server at the back end can obtain the user's real IP proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for through X-Forwarded-For Client_max_body_size 10m; # maximum single file bytes allowed for client requests client_body_buffer_size 128k; # buffer proxy buffer maximum client requests proxy_connect_timeout 90; # nginx and backend server connection timeout (proxy connection timeout) proxy_read_timeout 90 # after a successful connection, the response time of the back-end server (proxy receiving timeout) proxy_buffer_size 4k; # set the buffer size of the proxy server (nginx) to store account information proxy_buffers 632k; # proxy_buffers buffer, if the average web page is less than 32k, set proxy_busy_buffers_size 64k # proxy_buffers*2 proxy_temp_file_write_size 64k under heavy load; # set cache folder size, greater than this value, will be transferred from upstream server} location ~. *\. (gif | jpg | png | bmp | swf) $# static pages {expires 30d are processed by nginx; # using expires cache module, cached to the client for 30 days} location ~. *\. (jsp | js | css)? ${expires 1d } error_page 404 / 404.html; # error page error_page 500502 503504 / 50x.html; location = / 50x.html {root html;}}

2) configure static and dynamic separation

# configure Nginx to separate movement and movement, so that the defined static pages are read directly from the Nginx publishing directory. Location ~. *\. (html | htm | gif | jpg | jpeg | bmp | png | ico | txt | js | css) ${root / webapps/myproject/code/static-resource; # expires defines the caching time of the user's browser as 7 days. If the static page is not updated frequently, you can set it longer, which can save bandwidth and relieve the pressure on the server expires 1d;} location ~ ^ / (WEB-INF) / {# this is very important, otherwise the user can access deny all;}

It should be noted here that the storage path of the external static file should be the same as the path in the request, so that after the nginx splicing path, the file cannot be found because the path does not exist. If js or css is not loaded, you can check the errorlog of nginx to debug and correct it. The log is located in the logs directory under the nginx directory. The error log is intercepted as follows:

The copy code is as follows:

[error] 7195: 0: * 1693 open () "/ home/cms/include/dedeajax2.js" failed (2: No such file or directory), client: 101.226.35.225, server: localhost, request: "GET / cms/include/dedeajax2.js HTTP/1.1"

As you can see, the GET request is "/ cms/include/dedeajax2.js", and nginx will look for the file in this path under / home in the configuration. The full path is:

/ home/cms/include/dedeajax2.js

If you report an error as no such file or directory, you can see the file path problem in the corresponding / home directory.

After the configuration is successful, you can find that static files are processed by nginx, and requests for static files no longer enter the tomcat server, so that when you can package, the directories of static files, such as js, css, and so on, are no longer entered into the war package.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report