Nginx load balancing

Nginx load balancing

Overview of Load Balancing

Early website traffic and business functions were relatively simple, and a single server was enough to meet basic needs. However, with the development of the Internet, business traffic became larger and larger and business logic became more and more complex. The point-of-failure problem is highlighted, so multiple servers are required to scale the performance horizontally and avoid single-point failures. So how to distribute the request traffic of different users to different servers?

Insert picture description here

The principle and processing flow of load balancing

System expansion can be divided into vertical expansion and horizontal expansion.

Vertical expansion is from the stand-alone point of view, increasing the processing capacity of the server by increasing the hardware processing capacity of the system

Horizontal expansion is to meet the processing capacity of large-scale website services by adding machines.

Insert picture description here

There are two important roles involved in this, namely "application cluster" and "load balancer".

Application cluster: Deploy the same application on multiple machines to form a processing cluster, receive requests distributed by load balancing equipment, process them, and return the response data.

Load balancer: According to the corresponding load balancing algorithm, the user's access request is distributed to a server in the cluster for processing.

The role of load balancing

1. Solve the high concurrency pressure of the server and improve the processing performance of the application.

2. Provide failover to achieve high availability.

3. Enhance the scalability of the website by adding or reducing the number of servers.

4. Filtering on the load balancer can improve the security of the system.

Common handling methods for load balancing

Method 1: User manual selection

This method is relatively primitive, as long as the method is implemented is to provide different lines and different server link methods on the website homepage, allowing users to choose the specific server they visit to achieve load balancing.

Insert picture description here

Method 2: DNS polling method

DNS

域名系统(服务)协议(DNS)是一种分布式网络目录服务,主要用于域名与 IP 地址的相互转换。

Most domain registrars support adding multiple A records to the same hostname. This is DNS polling. The DNS server randomly allocates the resolution requests to different IPs in the order of the A records, so that simple load balancing can be completed. The cost of DNS polling is very low, and it is often used on some unimportant servers.

Insert picture description here

The following is the IP address we added for a certain domain name, and two servers are used for load balancing.

Insert picture description here

verification:

ping www.nginx521.cn

Clear the local dns cache

ipconfig/flushdns

We found that using DNS to implement polling does not require too much investment. Although DNS polling costs are low, DNS load balancing has obvious shortcomings.

1. Low reliability

Assuming that a domain name DNS polls multiple servers, if one of the servers fails, then all requests to access the server will not be responded to, even if you remove the server’s IP from the DNS, but due to the major Broadband access providers store a large number of DNS in the cache to save access time, resulting in DNS not being updated in real time. So DNS in turn solves the load balancing problem to a certain extent, but it has the disadvantage of low reliability.

2. Unbalanced load balancing

DNS load balancing uses a simple polling load algorithm, which cannot distinguish between server differences, cannot reflect the current running status of the server, and cannot allocate more requests to servers with good performance. In addition, the local computer will cache the resolved domain name to The mapping of the IP address will also cause the users who use the DNS server to access the same Web server within a certain period of time, which will cause the uneven load reduction of the Web server.

Unbalanced load will result in a low load on some servers, while the load on other servers is really high, and the processing speed of requests is slow. A server with a high configuration allocates fewer requests, while a server with a low configuration allocates more requests. .

Method 3: Layer 4/7 load balancing

Before introducing the four/seven layer load balancing, we first understand a concept, OSI (open system interconnection), called the open system interconnection model, which is designated by the International Organization for Standardization ISO and is not based on specific models, operating systems or companies. Network architecture. This model divides the work of network communication into seven layers.

Insert picture description here

Application layer: Provide network services for applications.

Presentation layer: operations such as formatting, encoding, encrypting, and compressing data.

Session layer: establish, maintain, and manage session connections.

Transport layer: establish, maintain, and manage end-to-end connections, commonly TCP/UDP.

Network layer: IP addressing and routing

Data link layer: Control the communication between the network layer and the physical layer.

Physical layer: bit stream transmission.

The so-called four-layer load balancing refers to the transport layer in the OSI seven-layer model, mainly based on IP+PORT load balancing

实现四层负载均衡的方式:
硬件:F5 BIG-IP、Radware等
软件:LVS、Nginx、Hayproxy等

The so-called seven-layer load balancing refers to the application layer, mainly based on virtual URL or host IP load balancing

实现七层负载均衡的方式:
软件:Nginx、Hayproxy等

The difference between Layer 4 and Layer 7 load balancing

四层负载均衡数据包是在底层就进行了分发,而七层负载均衡数据包则在最顶端进行分发,所以四层负载均衡的效率比七层负载均衡的要高。
四层负载均衡不识别域名,而七层负载均衡识别域名。

Dealing with Layer 4 and Layer 7 load thinking that there are actually Layer 2 and Layer 3 load balancing. The second layer is based on the mac address at the data link layer to achieve load balancing, and the third layer is generally using virtual IP addresses to achieve load at the network layer. balanced.

Patterns used in the actual environment

四层负载(LVS)+七层负载(Nginx)

Nginx seven-layer load balancing

Nginx needs to use proxy_pass proxy module configuration to achieve seven-layer load balancing. Nginx is installed by default to support this module, and we don't need to do any further processing. Nginx's load balancing is based on Nginx's reverse proxy to distribute user requests to a group of [upstream virtual service pools] according to a specified algorithm.

Nginx seven-layer load balancing instructions

upstream directive

This command is used to define a group of servers, which can be servers that listen on different ports, and they can also be servers that listen on TCP and Unix sockets at the same time. The server can specify different weights, the default is 1.

grammarupstream name {…}
Defaults
positionhttp
server instruction

This command is used to specify the name of the back-end server and some parameters, you can use domain name, IP, port or unix socket

grammarserver name [paramerters]
Defaults
positionupstream

Nginx seven-layer load balancing implementation process

Insert picture description here

Server settings

server {
    listen   9001;
    server_name localhost;
    default_type text/html;
    location /{
    	return 200 '<h1>192.168.200.146:9001</h1>';
    }
}
server {
    listen   9002;
    server_name localhost;
    default_type text/html;
    location /{
    	return 200 '<h1>192.168.200.146:9002</h1>';
    }
}
server {
    listen   9003;
    server_name localhost;
    default_type text/html;
    location /{
    	return 200 '<h1>192.168.200.146:9003</h1>';
    }
}

Load balancer settings

upstream backend{
	server 192.168.200.146:9091;
	server 192.168.200.146:9092;
	server 192.168.200.146:9093;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

Load balancing status

The state of the proxy server in the balance scheduling is as follows:

statusOverview
downThe current server temporarily does not participate in load balancing
backupReserved backup server
max_failsAllow the number of failed requests
fail_timeoutAfter max_fails fails, the service pause time
max_connsLimit the maximum number of receive connections
down

down: Mark the server as permanently unavailable, then the proxy server will not participate in load balancing.

upstream backend{
	server 192.168.200.146:9001 down;
	server 192.168.200.146:9002
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

This state will generally be set for servers that need to be shut down for maintenance.

backup

backup: Mark the server as a backup server, and will be used to pass requests when the primary server is unavailable.

upstream backend{
	server 192.168.200.146:9001 down;
	server 192.168.200.146:9002 backup;
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

At this time, you need to prohibit access to port 9094 to simulate the downtime of the only service that can provide external access, and the backup server will start to provide services to the outside world. At this time, in order to test and verify, we need to use a firewall to intercept it.

Introduce a tool firewall-cmd, which is provided by Linux specifically to operate firewall.

Query whether the specified port in the firewall is open

firewall-cmd --query-port=9001/tcp

How to open a specified port

firewall-cmd --permanent --add-port=9002/tcp

Add development ports in batches

firewall-cmd --permanent --add-port=9001-9003/tcp

How to remove a specified port

firewall-cmd --permanent --remove-port=9003/tcp

Reload

firewall-cmd --reload

among them

​ --permanent means set to be persistent

​ --add-port means to add the specified port

​ --remove-port means to remove the specified port

max_conns

max_conns=number: Used to set the maximum number of simultaneous active links of the proxy server. The default is 0, which means no limit. This configuration can be set according to the concurrent amount of requests processed by the back-end server to prevent the back-end server from being overwhelmed.

max_fails and fail_timeout

max_fails=number: Set the number of failed requests to the proxy server, the default is 1.

fail_timeout=time: Set the time for the service to pause after max_fails fails, the default is 10 seconds.

upstream backend{
	server 192.168.200.133:9001 down;
	server 192.168.200.133:9002 backup;
	server 192.168.200.133:9003 max_fails=3 fail_timeout=15;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

Load balancing strategy

After introducing the relevant instructions for Nginx load balancing, we have been able to distribute user requests to different servers. In addition to the default distribution method, what kind of load algorithm can we use?

Nginx upstream supports the following six allocation algorithms, which are:

Algorithm nameDescription
pollingDefault way
weightWeight method
ip_hashAccording to the ip distribution method
least_connAccording to the least connection method
url_hashAccording to URL allocation method
fairBased on response time
polling

It is the default strategy of upstream module load balancing. Each request will be assigned to different back-end servers one by one in chronological order. No additional configuration is required for polling.

upstream backend{
	server 192.168.200.146:9001 weight=1;
	server 192.168.200.146:9002;
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}
weight weighting [weighted polling]

weight=number: used to set the weight of the server, the default is 1, the larger the weight data, the greater the probability of being assigned to the request; the weight value is mainly adjusted for different back-end server hardware configurations in the actual working environment , All this strategy is more suitable for the situation where the hardware configuration of the server is quite different.

upstream backend{
	server 192.168.200.146:9001 weight=10;
	server 192.168.200.146:9002 weight=5;
	server 192.168.200.146:9003 weight=3;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}
ip_hash

When performing load balancing on multiple dynamic application servers at the back end, the ip_hash instruction can locate the request of a certain client IP to the same back-end server through a hash algorithm. In this way, when a user from a certain IP logs in on the back-end web server A, when accessing other URLs of the site, it can be ensured that they are accessing the back-end web server A.

grammarip_hash;
Defaults
positionupstream
upstream backend{
	ip_hash;
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

What needs to be said more is that the use of the ip_hash command cannot guarantee the load balance of the back-end servers, which may cause some back-end servers to receive more requests, and some back-end servers to receive fewer requests, and methods such as setting the back-end server weight will not be used. kick in.

Insert picture description here
least_conn

Minimal connections, forward the request to the back-end server with fewer connections. The polling algorithm forwards the requests to each backend evenly, so that their load is roughly the same; however, some requests take a long time, which will lead to a higher load on the backend. In this case, least_conn can achieve a better load balancing effect.

upstream backend{
	least_conn;
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

This load balancing strategy is suitable for situations where the request processing time varies and the server is overloaded.

Insert picture description here
url_hash

Distribute requests according to the hash results of the visited URLs, so that each URL is directed to the same back-end server, which must be used in conjunction with cache hits. Multiple requests for the same resource may arrive on different servers, resulting in unnecessary multiple downloads, low cache hit rate, and some waste of resource time. Using url_hash can make the same url (that is, the same resource request) reach the same server. Once the resource is cached and the request is received, it can be read from the cache.

upstream backend{
	hash &request_uri;
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

Visit the following address:

http://192.168.200.133:8083/a
http://192.168.200.133:8083/b
http://192.168.200.133:8083/c
Insert picture description here
fair

What fair uses is not the rotation balancing algorithm used by the built-in load balancing, but can intelligently balance the load according to the page size and load time. So how to use the fair load balancing strategy of a third-party module.

upstream backend{
	fair;
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

But how to use it directly will report an error, because fair belongs to the load balancing implemented by a third-party module. Need to add nginx-upstream-fair, how to add the corresponding module:

  1. Download the nginx-upstream-fair module
下载地址为:
	https://github.com/gnosek/nginx-upstream-fair
  1. Upload the downloaded file to the server and decompress it
unzip nginx-upstream-fair-master.zip
  1. Rename resource
mv nginx-upstream-fair-master fair
  1. Use the ./configure command to add resources to the Nginx module
./configure --add-module=/root/fair
  1. Compile
make

The following errors may occur during compilation, the default_port is missing in the ngx_http_upstream_srv_conf_t structure

Insert picture description here

solution:

Find src/http/ngx_http_upstream.h in the source code of Nginx, ngx_http_upstream_srv_conf_sadd the default_port attribute to the module

in_port_t	   default_port
Insert picture description here

Then perform make.

  1. Update Nginx

​ 6.1 Back up nginx in the sbin directory

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold

​ 6.2 Copy nginx in objs in the installation directory to the sbin directory

cd objs
cp nginx /usr/local/nginx/sbin

​ 6.3 Update Nginx

cd ../
make upgrade
  1. Compile and test using Nginx

The above introduced Nginx's commonly used load balancing strategies. Some people say that there are 5 types, which are polling and weighted polling as one type, and some people say that there are 6 types. So which one to use in our future development needs to be decided according to the application scenario of the actual project.

Load balancing case

Case 1: Load balancing of general polling rules is implemented for all requests
upstream backend{
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
	server 192.168.200.146:9003;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}
Case 2: Load balancing of weighted polling rules is implemented for all requests
upstream backend{
	server 192.168.200.146:9001 weight=7;
	server 192.168.200.146:9002 weight=5;
	server 192.168.200.146:9003 weight=3;
}
server {
	listen 8083;
	server_name localhost;
	location /{
		proxy_pass http://backend;
	}
}

Case 3: Load balancing for specific resources
upstream videobackend{
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
}
upstream filebackend{
	server 192.168.200.146:9003;
	server 192.168.200.146:9004;
}
server {
	listen 8084;
	server_name localhost;
	location /video/ {
		proxy_pass http://videobackend;
	}
	location /file/ {
		proxy_pass http://filebackend;
	}
}
Case 4: Load balancing for different domain names
upstream itcastbackend{
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
}
upstream itheimabackend{
	server 192.168.200.146:9003;
	server 192.168.200.146:9004;
}
server {
	listen	8085;
	server_name www.itcast.cn;
	location / {
		proxy_pass http://itcastbackend;
	}
}
server {
	listen	8086;
	server_name www.itheima.cn;
	location / {
		proxy_pass http://itheimabackend;
	}
}
Case 5: Implement load balancing with URL rewriting
upstream backend{
	server 192.168.200.146:9001;
	server 192.168.200.146:9002;
	server 192.168.200.146:9003;
}
server {
	listen	80;
	server_name localhost;
	location /file/ {
		rewrite ^(/file/.*) /server/$1 last;
	}
	location / {
		proxy_pass http://backend;
	}
}

Nginx four-layer load balancing

After Nginx 1.9, a stream module was added to implement forwarding, proxying, and load balancing of the four-layer protocol. The usage of the stream module is similar to the usage of http, allowing us to configure a set of TCP or UDP protocol monitoring, and then forward our requests through proxy_pass, and add multiple back-end services through upstream to achieve load balancing.

The implementation of four-layer protocol load balancing generally uses LVS, HAProxy, F5, etc., which are either expensive or troublesome to configure. Nginx is relatively simple to configure and can complete the work more quickly.

Add support for stream module

Nginx does not compile this module by default. If you need to use the stream module, you need to add it when compiling --with-stream.

Complete the added --with-streamimplementation steps:

》将原有/usr/local/nginx/sbin/nginx进行备份
》拷贝nginx之前的配置信息
》在nginx的安装源码进行配置指定对应模块  ./configure --with-stream
》通过make模板进行编译
》将objs下面的nginx移动到/usr/local/nginx/sbin下
》在源码目录下执行  make upgrade进行升级,这个可以实现不停机添加新模块的功能

Nginx four-layer load balancing instructions

stream instruction

This directive provides the configuration file context in which the streaming server directive is specified. Same level as http command.

grammarstream {…}
Defaults
positionmain
upstream directive

This instruction is similar to the upstream instruction of http.

Four-layer load balancing case

demand analysis
Insert picture description here

Implementation steps

(1) Prepare Redis server, prepare three Redis on one server, the ports are 6379, 6378 respectively

1. Upload the redis installation package,redis-4.0.14.tar.gz

2. Unzip the installation package

tar -zxf redis-4.0.14.tar.gz

3. Enter the redis installation package

cd redis-4.0.14

4. Use make and install to compile and install

make PREFIX=/usr/local/redis/redis01 install

5. Copy the redis configuration file redis.confto the /usr/local/redis/redis01/bin directory

cp redis.conf	/usr/local/redis/redis01/bin

6. Modify the redis.conf configuration file

port  6379      #redis的端口
daemonize yes   #后台启动redis

7. Copy redis01 to redis02

cd /usr/local/redis
cp -r redis01 redis02

8. Modify the redis.conf in the redis02 file folder

port  6378      #redis的端口
daemonize yes   #后台启动redis

9. Start separately, you can get two Redis. And view

ps -ef | grep redis

Use Nginx to distribute requests to different Redis servers.

(2) Prepare Tomcat server.

1. Upload the tomcat installation package,apache-tomcat-8.5.56.tar.gz

2. Unzip the installation package

tar -zxf apache-tomcat-8.5.56.tar.gz

3. Enter the bin directory of tomcat

cd apache-tomcat-8.5.56/bin
./startup

nginx.conf configuration

stream {
        upstream redisbackend {
                server 192.168.200.146:6379;
                server 192.168.200.146:6378;
        }
        upstream tomcatbackend {
        		server 192.168.200.146:8080;
        }
        server {
                listen  81;
                proxy_pass redisbackend;
        }
        server {
        		listen	82;
        		proxy_pass tomcatbackend;
        }
}

Visit test.

Nginx cache integration

The concept of caching

Cache is the buffer for data exchange (called: Cache). When users want to get data, they will first query and get data from the cache. If there is in the cache, it will be directly returned to the user. If there is not in the cache, it will be returned to the user. Send a request to query the data again from the server, and when the data is returned to the user, the data is put into the cache, and the user will get the data directly from the cache next time.

Insert picture description here

Caching is actually used in many scenarios, such as:

Sceneseffect
Operating system disk cacheReduce disk mechanical operations
Database cacheReduce file system IO operations
Application cacheReduce queries to the database
Web server cacheReduce the number of requests to the application server
Browser cacheReduce the number of interactions with the background

Advantages of caching

​ 1. Reduce data transmission, save network traffic, accelerate response speed, and improve user experience;

​ 2. Reduce server pressure;

​ 3. Provide high availability of the server;

Disadvantages of caching

​ 1. Inconsistent data

​ 2. Increase costs

Insert picture description here

The course notes explain Nginx. Nginx is used as a web server, and Nginx is used as a web cache server. It is between the client and the application server. When a user accesses a URL through a browser, the web cache server will go to the application server to get the information Show the content to the user and cache the content on its own server. When the next request comes, if the same URL is accessed, the web cache server will directly return the previously cached content to the client instead of the application server Send the request again. Web caching reduces the load of application servers and databases, reduces network latency, improves the response speed of user access, and enhances user experience.

Nginx web caching service

Nginx provides caching function starting from version 0.7.48. Nginx is implemented based on the Proxy Store. Its principle is to use the URL and related combinations as the Key, and use the MD5 algorithm to hash the Key to obtain the corresponding hash directory path on the hard disk, so as to save the cached content in the directory. It can support any URL connection, and also supports non-200 status codes such as 404/301/302. Nginx can either support setting the expiration time for the specified URL or status code, or use the purge command to manually clear the cache of the specified URL.

Insert picture description here

Nginx's web caching service is mainly completed by using the ngx_http_proxy_modulemodule-related instruction set. Next, we will introduce the commonly used instructions.

proxy_cache_path

This designation is used to set the storage path of the cache file

grammarproxy_cache_path path [levels=number]
keys_zone=zone_name:zone_size [inactive=time][max_size=size];
Defaults
positionhttp

path: cache path address, such as:

/usr/local/proxy_cache

levels: Specify the directory corresponding to the cache space, up to 3 levels can be set, and the value of each level is 1|2, such as:

levels=1:2   缓存空间有两层目录,第一次是1个字母,第二次是2个字母
举例说明:
itheima[key]通过MD5加密以后的值为 43c8233266edce38c2c9af0694e2107d
levels=1:2   最终的存储路径为/usr/local/proxy_cache/d/07
levels=2:1:2 最终的存储路径为/usr/local/proxy_cache/7d/0/21
levels=2:2:2 最终的存储路径为??/usr/local/proxy_cache/7d/10/e2

keys_zone: used to set the name and specified size for this cache area, such as:

keys_zone=itcast:200m  缓存区的名称是itcast,大小为200M,1M大概能存储8000个keys

inactive: Specifies that the cached data will be deleted if it has not been accessed multiple times, such as:

inactive=1d   缓存数据在1天内没有被访问就会被删除

max_size: Set the maximum cache space. If the cache space is full, the resource with the longest cache time will be overwritten by default, such as:

max_size=20g

Configuration example:

http{
	proxy_cache_path /usr/local/proxy_cache keys_zone=itcast:200m  levels=1:2:1 inactive=1d max_size=20g;
}

proxy_cache

This command is used to enable or disable the proxy cache. If it is enabled, you can customize which cache area to use for caching.

grammarproxy_cache zone_name|off;
Defaultsproxy_cache off;
positionhttp, server, location

zone_name: Specify the name of the buffer zone

proxy_cache_key

This command is used to set the key value of the web cache, and Nginx will store the cache according to the MD5 hash of the key value.

grammarproxy_cache_key key;
Defaultsproxy_cache_key $scheme$proxy_host$request_uri;
positionhttp, server, location

proxy_cache_valid

This command is used to set different cache times for different URLs that return status codes

grammarproxy_cache_valid [code …] time;
Defaults
positionhttp, server, location

Such as:

proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
为200和302的响应URL设置10分钟缓存,为404的响应URL设置1分钟缓存
proxy_cache_valid any 1m;
对所有响应状态码的URL都设置1分钟缓存

proxy_cache_min_uses

This instruction is used to set the resource to be cached after how many times it has been accessed

grammarproxy_cache_min_uses number;
Defaultsproxy_cache_min_uses 1;
positionhttp, server, location

proxy_cache_methods

This instruction user sets which HTTP methods are cached

grammarproxy_cache_methods GET|HEAD|POST;
Defaultsproxy_cache_methods GET HEAD;
positionhttp, server, location

The GET and HEAD methods of HTTP are cached by default, and the POST method is not cached.

Nginx cache setting case

demand analysis

Insert picture description here

Steps to achieve

1. Environmental preparation

Environment preparation of the application server

(1) Add a js directory under tomcat webapps on the 192.168.200.146 server, and add a jquery.js file in the js directory

(2) Start tomcat

(3) Access test

http://192.168.200.146:8080/js/jquery.js

Nginx environment preparation

(1) Complete Nginx reverse proxy configuration

http{
	upstream backend{
		server 192.168.200.146:8080;
	}
	server {
		listen       8080;
        server_name  localhost;
        location / {
        	proxy_pass http://backend/js/;
        }
	}
}

(2) Complete Nginx cache configuration

4. Add cache configuration

http{
	proxy_cache_path /usr/local/proxy_cache levels=2:1 keys_zone=itcast:200m inactive=1d max_size=20g;
	upstream backend{
		server 192.168.200.146:8080;
	}
	server {
		listen       8080;
        server_name  localhost;
        location / {
        	proxy_cache itcast;
            proxy_cache_key itheima;
            proxy_cache_min_uses 5;
            proxy_cache_valid 200 5d;
            proxy_cache_valid 404 30s;
            proxy_cache_valid any 1m;
            add_header nginx-cache "$upstream_cache_status";
        	proxy_pass http://backend/js/;
        }
	}
}

Nginx cache clearing

Method 1: Delete the corresponding cache directory

rm -rf /usr/local/proxy_cache/......

Method 2: Use third-party extension modules

ngx_cache_purge

(1) Download the resource package corresponding to the ngx_cache_purge module and upload it to the server.

ngx_cache_purge-2.3.tar.gz

(2) Decompress the resource file

tar -zxf ngx_cache_purge-2.3.tar.gz

(3) Modify the folder name to facilitate later configuration

mv ngx_cache_purge-2.3 purge

(4) Query Nginx configuration parameters

nginx -V

(5) Enter the Nginx installation directory and use ./configure for parameter configuration

./configure --add-module=/root/nginx/module/purge

(6) Use make to compile

make

(7) Back up the nginx secondary executable file in the nginx installation directory

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold

(8) Copy nginx in the compiled objs to the sbin directory of nginx

cp objs/nginx /usr/local/nginx/sbin

(9) Use make to upgrade

make upgrade

(10) Perform the following configuration in the nginx configuration file

server{
	location ~/purge(/.*) {
		proxy_cache_purge itcast itheima;
	}
}

Nginx sets resources not to be cached

Earlier we have completed the use of Nginx as a web cache server. But one problem we have to think about is that not all data is suitable for caching. For example, for some data that changes frequently. If caching is used, it is easy to appear that the data accessed by the user is not the real data of the server. Therefore, we need to filter these resources in the process of caching, not caching.

Nginx also provides this function setting, you need to use the following two instructions

proxy_no_cache

This instruction is used to define the condition of not caching the data.

grammarproxy_no_cache string …;
Defaults
positionhttp, server, location

Configuration example

proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;

proxy_cache_bypass

This instruction is used to set the condition of not getting data from the cache.

grammarproxy_cache_bypass string …;
Defaults
positionhttp, server, location

Configuration example

proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;

The above two instructions have a specified condition, this condition can be multiple, and at least one of the multiple conditions is not empty and not equal to "0", then the condition is satisfied. The configuration example given above is obtained from the official website, and three variables are used in it, namely $cookie_nocache, $arg_nocache, and $arg_comment

$cookie_nocache, $arg_nocache, $arg_comment

The meanings of these three parameters are:

$cookie_nocache
指的是当前请求的cookie中键的名称为nocache对应的值
$arg_nocache和$arg_comment
指的是当前请求的参数中属性名为nocache和comment对应的属性值

Under the case demonstration:

log_format params $cookie_nocache | $arg_nocache | $arg_comment;
server{
	listen	8081;
	server_name localhost;
	location /{
		access_log logs/access_params.log params;
		add_header Set-Cookie 'nocache=999';
		root html;
		index index.html;
	}
}

Case realization

Set the configuration scheme for not caching resources

server{
	listen	8080;
	server_name localhost;
	location / {
		if ($request_uri ~ /.*\.js$){
           set $nocache 1;
        }
		proxy_no_cache $nocache $cookie_nocache $arg_nocache $arg_comment;
        proxy_cache_bypass $nocache $cookie_nocache $arg_nocache $arg_comment;
	}
}