1.需要将生产环境的流量复制到预发布环境或测试环境。这样有很多好处,比如:可以验证功能是否正常,服务的性能;使用真实有效的流量请求进行验证,不需要创建数据,不影响正常上网;这和灰度发布不一样,镜像流量不会影响真实流量;可用于排查在线问题;重构,如果重构服务,这也是一个测试方法;Nginx为了实现流量复制,提供了ngx_http_mirror_module模块2.安装Nginx主页,搭建yum仓库。为此,创建文件/etc/yum.repos.d/nginx.repo将以下内容写入文件[nginx-stable]name=nginxstablerepobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true[nginx-mainline]name=nginxmainlinerepobaseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/gpgcheck=1enabled=0gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=trueyuminstallnginxyuminstallnginx-ynginx配置文件默认为nginx.conf一般情况下,nginx.conf文件在/usr/local/nginx/conf或/etc/nginx或/usr/local/etc/nginx中为了启动nginx,只需在命令行中输入nginx并回车#startnginxnginx#fastshutdownnginx-sstop#正常关闭nginx-squit#重新加载配置文件nginx-sreload#重新打开日志文件nginx-sreopen#所有正在运行的nginx进程列表ps-ax|grepnginx一旦主进程收到重新加载配置的信号,它将检查新配置文件在语法上是否正确并尝试应用其中提供的配置。如果成功,master进程将启动新的worker进程,并向旧的worker进程发送一条消息,要求它们关闭。否则,主进程回滚更改并继续使用旧配置。旧工作进程收到关闭命令后,停止接受新连接,直到处理完之前接受的所有连接。之后,旧的worker进程退出。nginx的master进程的进程ID默认放在nginx.pid文件中,文件所在目录一般为/usr/local/nginx/logs或/var/run。还可以这样停止nginxkill-sQUIT3997初始配置文件长这样:usernginx;worker_processes1;error_log/var/log/nginx/error.logwarn;pid/var/run/nginx.pid;events_{connections1er;http{包括/etc/nginx/mime.types;default_typeapplication/octet-stream;log_formatmain'$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.logmain;发送文件开启;#tcp_nopush上;keepalive_timeout65;#gzip上;包含/etc/nginx/conf.d/*.conf;}3.ngx_http_mirror_modulengx_http_mirror_module模块(1.13.4)通过创建后台镜像子请求实现原始请求的镜像。忽略了对镜子子修复的响应。请求。有了这个镜像,我们后面就可以利用这个镜像来做一些事情,比如复制所有的请求,实现线上进程的复制到其他地方。官网给出的例子很简单,如下:location/{mirror/mirror;proxy_passhttp://backend;}location=/mirror{内部;proxy_passhttp://test_backend$request_uri;}如果请求体是镜像的,则在创建子请求之前读取请求体。位置/{镜子/镜子;mirror_request_body关闭;proxy_passhttp://backend;}location=/mirror{内部;proxy_passhttp://log_backend;proxy_pass_request_body关闭;proxy_set_header内容长度“”;proxy_set_headerX-Original-URI$request_uri;}我们前面安装了Nginx,但是里面没有我们需要的ngx_http_mirror_module模块。所以,真正要用的时候最好使用自定义安装,即从源代码构建。首先下载源码http://nginx.org/en/download....接下来编译安装,例如:./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-modulemake&makeinstallconfigureupstreamapi.abc.com{server127.0.0.1:8080;}upstreamtapi.abc.com{server127.0.0.1:8081;}server{listen80;#源站点位置/api{proxy_passhttp://api.cjs.com;proxy_set_header主机$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;#流量复制镜像/newapi;镜像/mirror2;镜像/mirror3;#复制请求体mirror_request_bodyon;}#镜像站点位置/tapi{proxy_passhttp://tapi.cjs.com$request_乌里;proxy_pass_request_body开启;proxy_set_header主机$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;}}4。文档Nginx文档http://nginx.org/en/docs/http://nginx.org/en/docs/http...http://nginx.org/en/docs/begi...http://nginx.org/en/docs/http...http://nginx.org/en/docs/conf...第三方模板http://luajit.org/https://www.nginx.com/resourc...https://www.nginx.com/resourc...https://www.nginx.com/resourc...https://github.com/openresty/...补充#查看进程运行时间ps-eopid,用户,lstart,etime,cmd|grepnginx#查看已建立的连接数netstat-an|grep已建立|wc-l#查看80端口连接数netstat-an|grep“:80”|wc-l原文:https://www.cnblogs.com/cjsbl..作者:废哥
