赣州新闻网

首页 / 科技

Nginx 居然支持流量拷贝,一个牛X的功能!

2020-04-07 · 赣州新闻网 · 科技

1、需求

将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如可以验证功能是否正常,以及服务的性能;用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问;这跟灰度发布还不太一样,镜像流量不会影响真实流量;可以用来排查线上问题;重构,假如R B d t N服务做了重构,这也是一种测试方式p l y;为V ! % O P c $了实现流量拷贝,Nginx提供了ngx_http_mirroR e # ! / Kr_modu0 T D s = +le模块

2、1 l q 6 Q j = $ :安装Ngin: u w M O 4 k Wx

首h 1 f Z X E h g页,设置yum仓库。为此,创建一个文件/etc/yum.l , [ F | Lrepos.d/nginx.repo

将以下内容写入文件


name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://ngL + ` 0 Sinx.org/keys/nginx_signing.key
module_hotfixe# ` ` r Z c 1 :s=true


name=nginx mainline repo
baK n O j Z q J ?seurl=http://nginx.org/packages/mainline/centos/$releasever// 1 d A 5$basearch/
gpg z , Ggcc ^ U d E E Vraceful shutdown
nginx -s quit
# reloading the configuration file
nginx -s reloadA o ( R V 9 `
# reopening the log files
nginx -s reopen
# list of all running nginx processes
ps -ax | grep nginx

一旦master进程接收到重新加载配置的信号,它将检查新配置文件的语法是否正确,并尝试应用其中提供的配置。如果成功,master进程将启动新的worker进程,并发送消息给旧的worker进程,要求他们shutdow@ M 0 S Zn。

否则,master进程将回滚所做的更改,并继续使用y h 3 f旧配置。旧的worker进程在接收到关闭命令后,停止接受新的连接,直到所有之前已经接受的连接全部处理完为止。之后,旧的worker进程退出。

ngi4 = 7 n @ =nx的master进程的进程ID,默认情/ | 1况下,放在nginx.pid文件中,该文` h K F ! { f d 8件所在的[ - X 2 b } P目录一般是/usr/local/nginx/logs 或者 /var/run

还可以这样停止nginx

killg O L W & ` ~ -s QU* 4 0 ! x s =IT 3997

初始配置文件长这样:

user  nginx;
worker_proceh & _ s [sses 1;

error_log /var/log/nginx/error.log warM { k nn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main \'$remote_addr - $remote_user \"$requ_ { H ~ C 2 l 7 ?estD 5 ] , ~\" \'
\B P - #'$. ) y 8 [status $body_bytes_sent \"$http_refererm ` y = q v\" \'
\'\"$http_user_agent\" \"$http_x_forwarded_for\"\';

access_log /var/log/nginxk ; m ? ; , { B/access.log main;

sendfj ^ %ile on;
#tcp_nopush on;

keepalive_timeout 65 d y L ( Y R ; ^5;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

3、ng| p d ! ^ - T e zx_http_mirror_module

The ngx_http_mirror_module module (1.13.4) implements mirroring of an original request by creating backgr& ` K : B . N Jound mirror subrequests. Responses tW [ A oo mirror subrequests are igz 5 0nored.

我是这样理解的,这里,mirror本意是镜子、镜像,这里可以理解就像一个镜像站点一样,将所有的请求都收集起来,这个镜像就代表了所有真实有效的: q ` Z + !原始请求。有了这个镜像,后续我们才可能用这个镜像去做一些事情n { 4 n,比如重现一下所有的请求,这就实现了把线上的流程复制到别的地方。扩展:SpringBoot 项目构建 Docker 镜像调优实践

官网给出的示例倒是很简单,如下:

location / {% 7 e Q [
mirror /mirror;
proxy_pas8 L ( u g p Ps http://backend;
}

location = /mirror {
internal;
proxy_pass http://test_backend$rei U S m o [quest_uri;
}

如果请求体被镜像,那么在创建子请求之前会先读取请求体

location / {
mirror /mirror;
mirror_request_body off;
proxy_pass http://bp B 5ackend;
}

location = /mirror {
internal;
proxy_p! X 7ass http://log_backend;
proxy_pass_request_body off;
proxy_set_header Content-Length \"v z d F m Q & U m * E/nginx.org/en/download.html

接下来,编译安装,例如:

./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-pav ( C !th=/usr/local/nginx/nginx.pid
--with-http_ssl_module
--without-http_lid { Emit_req_module
--without-http_mirroO f H z U c K ~ `r_module
--witg L N #h-pcre=../pcre-8.43
--with-zlib=../zlib-1.2.11
--add-module=/path/to/ngx_devel_kit
--add-module=/pE 5 a Qath/to/Z 1 $ :lua-nginx-module

make & mc . g Uake install

配置

upstream api.abc.com {
server 127.0.0.1:85 k P E s080;
}

upstream tapi.abc.com {
server 127.0.B - u 00.1:8081;
}

server {
liV - - _ u * ! 7sten 80;
   # 源站点
location /api {
proxy_pass http://api.cjs.com;
pn d m KroQ & C /xy_set_header Host $host2 R l 7 5;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# 流量复制
mirror /newapi;
miL 2 h 3 9 A nrror /mirror2;
mirror /I M I [ n t _ m *mirrE 9 K (or3;

# 复制请求体
mirro. : ` v R R p W -r_requ} Q 2 w J u R 5est_bom v x 2 = Vdy on;
}

# 镜像站点
locatG H k pion /tapi {
proxy@ @ X C_pass http://tapi.cjs.com$request_uri;
proxy_pass_re( N qquest_body on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxyM A w [ i : 0 v |_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

4、文档

Nginx文档

http://nginx.org/en/docs/ http://nginx.org/en/docs/http/ngx_http_mirror_module.html http://nginx.org/en/dq { w Kocs/beginners_X o 5 # 1 eguide.htmlM g $ http://nginx.org/en/docs/http/ngx_http__ e _ 0 o N vcore_module.ht| s * ) kml#location http://nginx.org/en/docs/confi4 - 5 i h n - |gure.html

第三方模板

http://luajit.3 F G C _ worg/ https://www.nginx.com/resR U 5 s oources/wiki/ https://www.nginx.com/resources/wiki/modules/lua/ https://www.nginx.com/resources/wiki/modules/index.html https://github.com/openresty/+ g $ L + ~ Y zlua-ng: x o G m Cinx-module

补充

# 查看进程运行时间
ps -eo pid,user,lstart,etime,cmd | grep ngiy ; e 5nx
# 查看已经建立连接的数量
netstat -an | greq ? E 2 = ; 3 d *p ESTABLISHED | wc -l
# 查看80端口的连接数
netstat -an | grep \":80\" | wc -l

测试项目需求请求设置代表功能龙南流量