「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-develbzr libtool2.安装opensslwget https://www.openssl.org/source/openssl-1.0.2s.tar.gztar -zxvf openssl-1.0.2s.tar.gzcd /usr/local/src/openssl-1.0.2s./config --prefix=/usr/local/openssl-1.0.2smakemake install3.安装pcrewget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gztar -zxvf pcre-8.43.tar.gzcd /usr/local/src/pcre-8.43./configure --prefix=/usr/local/pcre-8.43makemake install4.安装zlibwget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gztar -zxvf zlib-1.2.11.tar.gzcd /usr/local/src/zlib-1.2.11./configure --prefix=/usr/local/zlib-1.2.11makemake5.安装Nginxwget http://nginx.org/download/nginx-1.17.2.tar.gztar -zxvf nginx-1.17.2.tar.gzcd /usr/local/src/nginx-1.17.2./configure --prefix=/usr/local/nginx-1.17.2 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_ssl_modulemakemake install这里需要注意的是:安装Nginx时,指定的是openssl、pcre和zlib的源码解压目录,安装完成后Nginx配置文件的完整路径为:
/usr/local/nginx-1.17.2/conf/nginx.conf 。

「Nginx」实现负载均衡、限流、缓存、黑白名单和灰度发布

文章插图
 
Nginx负载均衡配置1.负载均衡配置http {……upstream real_server {server 192.168.103.100:2001 weight=1;#轮询服务器和访问权重server 192.168.103.100:2002 weight=2;}server {listen80;location / {proxy_pass http://real_server;}}}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」实现负载均衡、限流、缓存、黑白名单和灰度发布

文章插图
 
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 http://real_server;}burst排队大小,nodelay不限制单个请求间的时间 。
3.不限流白名单geo $limit {default1;192.168.2.0/240;}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 http://real_server;}上述配置中,192.168.2.0/24网段的IP访问是不限流的,其他限流 。
IP后面的数字含义:
  • 24表示子网掩码:255.255.255.0
  • 16表示子网掩码:255.255.0.0
  • 8表示子网掩码:255.0.0.0

「Nginx」实现负载均衡、限流、缓存、黑白名单和灰度发布

文章插图
 
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接口
【「Nginx」实现负载均衡、限流、缓存、黑白名单和灰度发布】不需要缓存
  • 用户隐私等敏感数据
  • 经常改变的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_passhttp://real_server;proxy_redirectoff;}location ~ .*.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {root /data/webApps/edc;expires3d;add_header Static Nginx-Proxy;}}


推荐阅读