Nginx 又一牛X的功能!流量拷贝
1. 需求
将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如:
-
可以验证功能是否正常,以及服务的性能; -
用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问; -
这跟灰度发布还不太一样,镜像流量不会影响真实流量; -
可以用来排查线上问题; -
重构,假如服务做了重构,这也是一种测试方式;
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum install nginx -y
# 启动nginx
nginx
# fast shutdown
nginx -s stop
# graceful shutdown
nginx -s quit
# reloading the configuration file
nginx -s reload
# reopening the log files
nginx -s reopen
# list of all running nginx processes
ps -ax | grep nginx
kill -s QUIT 3997
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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 [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
The ngx_http_mirror_module module (1.13.4) implements mirroring of an original request by creating background mirror subrequests. Responses to mirror subrequests are ignored.
location / {
mirror /mirror;
proxy_pass http://backend;
}
location = /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}
location / {
mirror /mirror;
mirror_request_body off;
proxy_pass http://backend;
}
location = /mirror {
internal;
proxy_pass http://log_backend;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-http_ssl_module
--without-http_limit_req_module
--without-http_mirror_module
--with-pcre=../pcre-8.43
--with-zlib=../zlib-1.2.11
--add-module=/path/to/ngx_devel_kit
--add-module=/path/to/lua-nginx-module
make & make install
upstream api.abc.com {
server 127.0.0.1:8080;
}
upstream tapi.abc.com {
server 127.0.0.1:8081;
}
server {
listen 80;
# 源站点
location /api {
proxy_pass http://api.cjs.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 流量复制
mirror /newapi;
mirror /mirror2;
mirror /mirror3;
# 复制请求体
mirror_request_body on;
}
# 镜像站点
location /tapi {
proxy_pass http://tapi.cjs.com$request_uri;
proxy_pass_request_body on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 查看进程运行时间
ps -eo pid,user,lstart,etime,cmd | grep nginx
# 查看已经建立连接的数量
netstat -an | grep ESTABLISHED | wc -l
# 查看80端口的连接数
netstat -an | grep ":80" | wc -l
来源:
https://www.cnblogs.com/cjsblog/p/12163207.html
·END·
PHP开源社区 进阶·提升·涨薪