亚洲熟女综合色一区二区三区,亚洲精品中文字幕无码蜜桃,亚洲va欧美va日韩va成人网,亚洲av无码国产一区二区三区,亚洲精品无码久久久久久久

Nginx流量復(fù)制

需求

將生產(chǎn)環(huán)境的流量拷貝到預(yù)上線環(huán)境或測(cè)試環(huán)境,這樣做有很多好處,比如:

  • 可以驗(yàn)證功能是否正常,以及服務(wù)的性能;
  • 用真實(shí)有效的流量請(qǐng)求去驗(yàn)證,又不用造數(shù)據(jù),不影響線上正常訪問;
  • 這跟灰度發(fā)布還不太一樣,鏡像流量不會(huì)影響真實(shí)流量;
  • 可以用來(lái)排查線上問題;
  • 重構(gòu),假如服務(wù)做了重構(gòu),這也是一種測(cè)試方式;

為了實(shí)現(xiàn)流量拷貝,Nginx提供了ngx_http_mirror_module模塊

安裝Nginx

首頁(yè),設(shè)置yum倉(cāng)庫(kù)。為此,創(chuàng)建一個(gè)文件
/etc/yum.repos.d/nginx.repo

將以下內(nèi)容寫入文件

1.[nginx-stable]
2.name=nginx stable repo
3.baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
4.gpgcheck=1
5.enabled=1
6.gpgkey=https://nginx.org/keys/nginx_signing.key
7.module_hotfixes=true
8.
9.[nginx-mainline]
10.name=nginx mainline repo
11.baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
12.gpgcheck=1
13.enabled=0
14.gpgkey=https://nginx.org/keys/nginx_signing.key
15.module_hotfixes=true yum安裝nginx

默認(rèn)情況下,nginx配置文件是nginx.conf

一般情況下,nginx.conf文件在 /usr/local/nginx/conf 或者 /etc/nginx 或者 /usr/local/etc/nginx 目錄下

為了啟動(dòng)nginx,直接在命令行里輸入nginx回車即可

1.# 啟動(dòng)
2.nginxnginx
3. # fast shutdown
4.nginx -s stop
5.# graceful shutdown
6.nginx -s quit
7.# reloading the configuration file
8.nginx -s reload
9.# reopening the log files
10.nginx -s reopen
11.# list of all running nginx processes
12.ps -ax | grep nginx
Nginx流量復(fù)制
Nginx流量復(fù)制

一旦master進(jìn)程接收到重新加載配置的信號(hào),它將檢查新配置文件的語(yǔ)法是否正確,并嘗試應(yīng)用其中提供的配置。如果成功,master進(jìn)程將啟動(dòng)新的worker進(jìn)程,并發(fā)送消息給舊的worker進(jìn)程,要求他們shutdown。否則,master進(jìn)程將回滾所做的更改,并繼續(xù)使用舊配置。舊的worker進(jìn)程在接收到關(guān)閉命令后,停止接受新的連接,直到所有之前已經(jīng)接受的連接全部處理完為止。之后,舊的worker進(jìn)程退出。

nginx的master進(jìn)程的進(jìn)程ID,默認(rèn)情況下,放在nginx.pid文件中,該文件所在的目錄一般是/usr/local/nginx/logs 或者 /var/run

還可以這樣停止nginx

初始配置文件長(zhǎng)這樣:

1.user  nginx;
2.worker_processes  1;
3.
4.error_log  /var/log/nginx/error.log warn;
5.pid        /var/run/nginx.pid;
6.
7.
8.events {
9.       worker_connections  1024;
10. }
11.
12.
13.http {
14.     include       /etc/nginx/mime.types;
15.     default_type  application/octet-stream;
16.
17.    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
18.                                 '$status $body_bytes_sent "$http_referer" '
19.                                '"$http_user_agent" "$http_x_forwarded_for"';
20.
21.    access_log  /var/log/nginx/access.log  main;
22.    sendfile        on;
23.    #tcp_nopush     on;
24.
25.    keepalive_timeout  65;
26.
27.    #gzip  on;
28.
29.    include /etc/nginx/conf.d/*.conf;
30.}
31.

ngx_http_mirror_module




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.

我是這樣理解的,這里,mirror本意是鏡子、鏡像,這里可以理解就像一個(gè)鏡像站點(diǎn)一樣,將所有的請(qǐng)求都收集起來(lái),這個(gè)鏡像就代表了所有真實(shí)有效的原始請(qǐng)求。有了這個(gè)鏡像,后續(xù)我們才可能用這個(gè)鏡像去做一些事情,比如重現(xiàn)一下所有的請(qǐng)求,這就實(shí)現(xiàn)了把線上的流程復(fù)制到別的地方。

官網(wǎng)給出的示例倒是很簡(jiǎn)單,如下:

1.location / {
2.    mirror /mirror;
3.    proxy_pass http://backend;
4.}

5.
6.6.

internal;    proxy_pass http://test_backend$request_uri;}

如果請(qǐng)求體為鏡像,那么在創(chuàng)建子請(qǐng)求之前會(huì)先讀取請(qǐng)求體

1.location / {
2.    mirror /mirror;
3.    mirror_request_body off;
4.    proxy_pass http://backend;
5.}
6.
7.location = /mirror {
8.    internal;
9.    proxy_pass http://log_backend; 
10.  proxy_pass_request_body off;
11.  proxy_set_header Content-Length "";
12.  proxy_set_header X-Original-URI $request_uri;
13.}?

前面我們安裝了Nginx,但是里面沒有包含我們所需的ngx_http_mirror_module模塊,因此,真正要使用的時(shí)候最好還是采用自定義安裝,即從源碼構(gòu)建

首先,下載源碼
http://nginx.org/en/download.html

接下來(lái),編譯安裝,例如:

1../configure
2.      --sbin-path=/usr/local/nginx/nginx
3.      --conf-path=/usr/local/nginx/nginx.conf
4.      --pid-path=/usr/local/nginx/nginx.pid
5.      --with-http_ssl_module
6.      --without-http_limit_req_module
7.      --without-http_mirror_module
8.      --with-pcre=../pcre-8.43
9.      --with-zlib=../zlib-1.2.11
10.    --add-module=/path/to/ngx_devel_kit
11.    --add-module=/path/to/lua-nginx-module
12.
13.make & make install

配置

1.upstream api.abc.com {
2.  server 127.0.0.1:8080;
3.}
4.
5.upstream tapi.abc.com {
6.       server 127.0.0.1:8081;
7.}
8.
9.server {
10.       listen 80;
11.   # 源站點(diǎn)
12.      location /api { 
13.       proxy_pass http://api.cjs.com;
14.        proxy_set_header Host $host;
15.        proxy_set_header X-Real-IP $remote_addr;
16.        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
17.
18.        # 流量復(fù)制
19.  mirror /newapi;
20.  mirror /mirror2;
21.  mirror /mirror3;
22.
23.    # 復(fù)制請(qǐng)求體
24.  mirror_request_body on;
25.????}
26.    # 鏡像站點(diǎn)
27.    location /tapi {
28.        proxy_pass http://tapi.cjs.com$request_uri;
29.        proxy_pass_request_body on;
30.        proxy_set_header Host $host;
31.        proxy_set_header X-Real-IP $remote_addr;
32.        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
33.    }
34.}

文檔

1.Nginx文檔
2.http://nginx.org/en/docs/
3.http://nginx.org/en/docs/http/ngx_http_mirror_module.html
4.http://nginx.org/en/docs/beginners_guide.html
5.http://nginx.org/en/docs/http/ngx_http_core_module.html#location
6.?http://nginx.org/en/docs/configure.html
7.
8.第三方模板
9.?http://luajit.org/
10.https://www.nginx.com/resources/wiki/
11.https://www.nginx.com/resources/wiki/modules/lua/
12.https://www.nginx.com/resources/wiki/modules/index.html
13.https://github.com/openresty/lua-nginx-module?

補(bǔ)充

1.# 查看進(jìn)程運(yùn)行時(shí)間
2.ps -eo pid,user,lstart,etime,cmd | grep nginx
3.# 查看已經(jīng)建立連接的數(shù)量
4.netstat -an | grep ESTABLISHED | wc -l
5.# 查看80端口的連接數(shù)
6.netstat?-an?|?grep?":80"?|?wc?-l?
好啦!今天的分享到這里就結(jié)束了,希望大家持續(xù)關(guān)注馬哥教育官網(wǎng),每天們都會(huì)有大量?jī)?yōu)質(zhì)內(nèi)容與大家分享。聲明;文章轉(zhuǎn)載于馬哥教育官網(wǎng),版權(quán)歸原作者所有!

相關(guān)新聞

歷經(jīng)多年發(fā)展,已成為國(guó)內(nèi)好評(píng)如潮的Linux云計(jì)算運(yùn)維、SRE、Devops、網(wǎng)絡(luò)安全、云原生、Go、Python開發(fā)專業(yè)人才培訓(xùn)機(jī)構(gòu)!