【Nginx】实现负载均衡、限流、缓存、黑白名单和灰度发布,这是最全的一篇了!

在《》一文中,我们主要介绍了如何使用Nginx进行限流,以避免系统被大流量压垮。除此之外,Nginx还有很多强大的功能,例如:负载均衡、缓存黑白名单、灰度发布等。今天,我们就来一起探讨Nginx支持的这些强大的功能!

Nginx安装

注意:这里以CentOS 6.8服务器为例,以root用户身份来安装Nginx。

1.安装依赖环境 yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel* libaio libaio-devel bzr libtool 2.安装openssl wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz tar -zxvf openssl-1.0.2s.tar.gz cd /usr/local/src/openssl-1.0.2s ./config --prefix=http://www.likecs.com/usr/local/openssl-1.0.2s make make install 3.安装pcre wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz tar -zxvf pcre-8.43.tar.gz cd /usr/local/src/pcre-8.43 ./configure --prefix=http://www.likecs.com/usr/local/pcre-8.43 make make install 4.安装zlib wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd /usr/local/src/zlib-1.2.11 ./configure --prefix=http://www.likecs.com/usr/local/zlib-1.2.11 make make 5.安装Nginx wget tar -zxvf nginx-1.17.2.tar.gz cd /usr/local/src/nginx-1.17.2 ./configure --prefix=http://www.likecs.com/usr/local/nginx-1.17.2 --with-openssl=http://www.likecs.com/usr/local/src/openssl-1.0.2s --with-pcre=http://www.likecs.com/usr/local/src/pcre-8.43 --with-zlib=http://www.likecs.com/usr/local/src/zlib-1.2.11 --with-http_ssl_module make make install

这里需要注意的是:安装Nginx时,指定的是openssl、pcre和zlib的源码解压目录,安装完成后Nginx配置文件的完整路径为:/usr/local/nginx-1.17.2/conf/nginx.conf。

Nginx负载均衡配置 1.负载均衡配置 http { …… upstream real_server { server 192.168.103.100:2001 weight=1; #轮询服务器和访问权重 server 192.168.103.100:2002 weight=2; } server { listen 80; location / { proxy_pass ; } } } 2.失败重试配置 upstream real_server { server 192.168.103.100:2001 weight=1 max_fails=2 fail_timeout=60s; server 192.168.103.100:2002 weight=2 max_fails=2 fail_timeout=60s; }

意思是在fail_timeout时间内失败了max_fails次请求后,则认为该上游服务器不可用,然后将该服务地址踢除掉。fail_timeout时间后会再次将该服务器加入存活列表,进行重试。

三、Nginx限流配置 1.配置参数

limit_req_zone指令设置参数

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;

limit_req_zone定义在http块中,$binary_remote_addr表示保存客户端IP地址的二进制形式。

Zone定义IP状态及URL访问频率的共享内存区域。zone=keyword标识区域的名字,以及冒号后面跟区域大小。16000个IP地址的状态信息约1MB,所以示例中区域可以存储160000个IP地址。

Rate定义最大请求速率。示例中速率不能超过每秒10个请求。

2.设置限流 location / { limit_req zone=mylimit burst=20 nodelay; proxy_pass ; }

burst排队大小,nodelay不限制单个请求间的时间。

3.不限流白名单 geo $limit { default 1; 192.168.2.0/24 0; } map $limit $limit_key { 1 $binary_remote_addr; 0 ""; } limit_req_zone $limit_key zone=mylimit:10m rate=1r/s; location / { limit_req zone=mylimit burst=1 nodelay; proxy_pass ; }

上述配置中,192.168.2.0/24网段的IP访问是不限流的,其他限流。

IP后面的数字含义:

24表示子网掩码:255.255.255.0

16表示子网掩码:255.255.0.0

8表示子网掩码:255.0.0.0

Nginx缓存配置 1.浏览器缓存

静态资源缓存用expire

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { expires 2d; }

Response Header中添加了Expires和Cache-Control,

静态资源包括(一般缓存)

普通不变的图像,如logo,图标等

js、css静态文件

可下载的内容,媒体文件

协商缓存(add_header ETag/Last-Modified value)

HTML文件

经常替换的图片

经常修改的js、css文件

基本不变的API接口

不需要缓存

用户隐私等敏感数据

经常改变的api数据接口

2.代理层缓存 //缓存路径,inactive表示缓存的时间,到期之后将会把缓存清理 proxy_cache_path /data/cache/nginx/ levels=1:2 keys_zone=cache:512m inactive = 1d max_size=8g; location / { location ~ \.(htm|html)?$ { proxy_cache cache; proxy_cache_key $uri$is_args$args; //以此变量值做HASH,作为KEY //HTTP响应首部可以看到X-Cache字段,内容可以有HIT,MISS,EXPIRES等等 add_header X-Cache $upstream_cache_status; proxy_cache_valid 200 10m; proxy_cache_valid any 1m; proxy_pass ; proxy_redirect off; } location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /data/webapps/edc; expires 3d; add_header Static Nginx-Proxy; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpggyj.html