当前位置: 首页 > 科技观察

Nginx的这个功能真是太神奇了!

时间:2023-03-19 13:32:29 科技观察

1。需要将生产环境的流量复制到预发布环境或测试环境。这有很多好处,例如:可以验证功能是否正常和服务的性能;使用真实有效的流量请求进行验证,不影响正常上网;这和灰度发布不一样,镜像流量不会影响真实流量;可用于排查在线问题;重构,如果对服务进行重构,这也是一种Test方法;为了实现流量复制,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=trueyuminstallnginxyuinstallnginx-y默认nginx配置文件为nginx.conf一般nginx.conf文件为在/usr/local/nginx/conf或/etc/nginx或/usr/local/etc/nginx目录下要启动nginx,在命令行直接输入nginx回车#Startnginxnginx#fastshutdownnginx-sstop#gracefulshutdownnginx-squit#reloadingtheconfigurationfilenginx-sreload#reopeningthelogfilesnginx-sreopen#listofallrunningnginxprocessesps-ax|grepginx一旦主进程收到重新启动的信号加载配置时,它会检查新配置文件的语法并尝试应用其中提供的配置。如果成功,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{worker_connections1024;}http{include/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;sendfileon;#tcp_nopushon;keepalive_timeout65;#gzipon;include/etc/nginx/conf.d/*.conf;}3.ngx_http_mirror_modulengx_http_mirror_module模块(1.13.4)通过创建后台镜像子请求实现原始请求的镜像。忽略对镜像子请求的响应。我是这样理解的。在这里,mirror的本义是一面镜子。这里可以理解为它就像一个镜像站点,收集所有的请求。这个镜像代表了所有真实有效的原始请求。有了这个镜像,我们后面就可以利用这个镜像来做一些事情,比如复制所有的请求,实现线上进程的复制到其他地方。官网给出的例子很简单,如下:location/{mirror/mirror;proxy_passhttp://backend;}location=/mirror{internal;proxy_passhttp://test_backend$request_uri;}如果请求body是镜像的话,然后create在子请求之前读取请求体。location/{mirror/mirror;mirror_request_bodyoff;proxy_passhttp://backend;}location=/mirror{internal;proxy_passhttp://log_backend;proxy_pass_request_bodyoff;proxy_set_headerContent-Length"";proxy_set_headerX-Original-URI$request_uri;}我们之前安装了Nginx,但是里面没有我们需要的ngx_http_mirror_module模块,所以真正要用的时候最好还是自定义安装,即从源码构建。首先下载源码http://nginx.org/en/download.html接下来编译安装,例如:./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&makeinstall配置upstreamapi。abc.com{server127.0.0.1:8080;}upstreamtapi.abc.com{server127.0.0.1:8081;}server{listen80;#sourcesitelocation/api{proxy_passhttp://api.cjs.com;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;#trafficreplicationmirror/newapi;mirror/mirror2;mirror/mirror3;#copyrequestbodymirror_request_bodyon;}#mirrorsitelocation/tapi{proxy_passhttp://塔皮。cjs.com$request_uri;proxy_pass_request_bodyon;proxy_set_headerHost$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/ngx_http_mirror_module.htmlhttp://nginx.org/en/docs/beginners_guide.htmlhttp://nginx.org/en/docs/http/ngx_http_core_module.html#locationhttp://nginx.org/en/docs/configure.html第三方模板http://luajit.org/https://www.nginx.com/资源/wiki/https://www.nginx.com/resources/wiki/modules/lua/https://www.nginx.com/resources/wiki/modules/index.htmlhttps://github.com/openresty/lua-nginx-module补充#查看进程运行时间ps-eopid,user,lstart,etime,cmd|grepginx#查看已建立连接数netstat-an|grepESTABLISHED|wc-l#查看80端口连接数netstat-一个|grep":80"|wc-l