In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
The previous blog explained the role of Nginx in the application architecture, as well as the idea of load balancing. This article practices the operations of accessing static and dynamic resources.
First, understand the difference between accessing static resources and accessing dynamic resources
Static resources: data stored in the hard disk, fixed data, data that does not need to be calculated.
Such as: pictures, fonts, js files, css files and so on. When a user accesses static resources, the server returns those resources directly to the user's computer.
Dynamic resources: refers to the data returned by the server after a series of logical calculations according to the data returned by the user and the data stored in the database.
Such as: request for tomorrow's weather information data, request to check the account balance.
2. The necessity of separating the request for dynamic data from the request for static resources
Tomcat application server is used to deal with Servlet container and JSP, although it can also handle a series of static resources such as HTML, but it is not as efficient as Nginx; and has a lot of pressure on the operation of Servlet container and JSP. If not separated, it will lead to a lot of performance waste. In the final analysis, in terms of application services, we should follow the principle that a service only does one thing. If you want to do dynamic requests, you only do dynamic requests, and if you want to do static requests, you do static requests, so as to improve performance.
What we need to do is to let Nginx return the static resources to the user when the user accesses the static resource; when the user accesses the dynamic resource, the access is transferred to the Tomcat application server, and the Tomcat returns the data to Nginx,Nginx and then to the user.
III. Nginx configuration method
Here, there is not much explanation on the parameters in the configuration file of Nginx. If you need to know about the configuration file of Nginx, go here.
An instruction that does not know the location of the configuration file:
Sudo find /-name nginx.conf
Be good at using Linux instructions, so you can't help falling in love with Linux.
Let's start with a full configuration:
# user www www;user root root;worker_processes 2; # setting values are consistent with the number of CPU cores error_log / home/zuoyu/ServerComputer/nginx/logs/nginx_error.log crit; # log location and log level pid / home/zuoyu/ServerComputer/nginx/nginx.pid;worker_rlimit_nofile 65535 http {# use epoll model to improve performance use epoll; # maximum number of connections per process worker_connections 65535;} http {# extension and file type mapping table include mime.types # default type 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"; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; types_hash_max_size 2048; types_hash_bucket_size 128; sendfile on; tcp_nopush on Keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; # decompress transmission 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 # load balancer group # static server group upstream static.zuoyu.com {server localhost:81;} # dynamic server group upstream dynamic.zuoyu.com {server localhost:8080; # server localhost:8081; # server localhost:8082; # server localhost:8083;} # configure proxy parameter proxy_redirect off; proxy_set_header HOST $host; 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; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 16k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; # Cache configuration proxy_cache_key'$host:$server_port$request_uri'; # proxy_temp_file_write_size 64k; proxy_temp_path / home/zuoyu/ServerComputer/nginx/proxy_temp_path Proxy_cache_path / home/zuoyu/ServerComputer/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g; proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; # static resource host server {listen 81; server_name localhost_0; charset utf8; location / {root / home/zuoyu/Public/NginxStaticSource/static;}} # below is the configuration of the server virtual host server {listen 80 listen # listening port server_name localhost_1 # Domain name charset utf8; location / {# root / usr/share/nginx/html; proxy_pass http://dynamic.zuoyu.com; index index.html index.jsp;} location ~. *\. (jsp | do | action) ${index index.jsp; proxy_pass http://dynamic.zuoyu.com;} location ~. *\. (gif | jpg | jpeg | png | bmp | swf | svg) ${# cache 30 days expires 30d; proxy_pass http://static.zuoyu.com; proxy_cache cache_one Proxy_cache_valid 200304 3025d; proxy_cache_valid any 5d; proxy_cache_key'$host:$server_port$request_uri'; add_header X-Cache'$upstream_cache_status from $host';} location ~. *\. (ttf | woff | woff2) ${# cache 30 days expires 30d; proxy_pass http://static.zuoyu.com; proxy_cache cache_one; proxy_cache_valid 200304 3025d; proxy_cache_valid any 5d Proxy_cache_key'$host:$server_port$request_uri'; add_header X-Cache'$upstream_cache_status from $host';} location ~. *\. (js | css) ${# cache 7 days expires 7d; proxy_pass http://static.zuoyu.com; proxy_cache cache_one; proxy_cache_valid 200 304 302 5d; proxy_cache_valid any 5d; proxy_cache_key'$host:$server_port$request_uri'; add_header X-Cache'$upstream_cache_status from $host' } # other pages reverse proxy to tomcat container location ~. * ${index index.jsp index.html; proxy_pass http://dynamic.zuoyu.com;} access_log off; error_page 500502503504 / 50x.htl; location = / 50x.html {root / usr/share/nginx/html;}
In this configuration file, it includes not only the separation of static and dynamic access, but also cache, resource compression and load balancing. Only static and dynamic resources are analyzed here:
Static resource allocation
Take visiting pictures as an example:
Location. *\. (gif | jpg | jpeg | png | bmp | swf | ico | svg) ${root / home/zuoyu/Public/NginxStaticSource/static;}
When you visit the virtual host location:80, when you access the above file type, you will look it up in the root / home/zuoyu/Public/NginxStaticSource/static/ directory. For example, if you want to access the image root / home/zuoyu/Public/NginxStaticSource/static/img/background.png, you only need location:80/img/background.png to access the file.
In my configuration, I set up another host to configure the static resource path, so as to avoid changing the directory of static resources in more than one place, and only need to modify the host path. You can change the configuration of the above picture to
Location ~. *\. (gif | jpg | jpeg | png | bmp | swf | ico | svg) ${proxy_pass http://localhost:81;}
This greatly increases flexibility and is easier to implement when load balancing. Note: the static resource host configuration must be placed on top of the core host to be effective.
Dynamic data configuration
Let's take visiting JSP pages, do requests, and action requests as examples.
Location ~. *\. (jsp | do | action) ${index index.jsp; proxy_pass http://localhost:8080;}
This configuration tells the Nginx server that when there is a request with the suffixes of jsp, do, and action, it will be handed over to the host localhost:8080; for processing. The host's home page is index.jsp, which is called reverse proxy. Here is a concept-proxy and reverse proxy; the proxy usually needs to be configured on the client side to forward the request to be sent to the proxy server, while the reverse proxy should be configured on the server. forward requests that are supposed to be sent to this server to the proxy server.
All the requests that need to be processed by the Tomcat application server are handed over to Tomcat, and the rest is handled by Nginx. If you need other servers, you can configure ok.
In this way, the separation of movement and movement is realized. When the user's browser loads the page, those css files, js files, font styles, pictures, etc., will be directly retrieved from the local hard disk by the Nginx server and returned to the user browser; and the user name and other information will be sent by nginx to Tomcat and returned to Nginx,Nginx to the user's browser.
There is nothing to be afraid of the infinite truth, there is joy to enter an inch.
Summary
The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.