1. 程式人生 > >Nginx流量複製

Nginx流量複製

1. 需求

將生產環境的流量拷貝到預上線環境或測試環境,這樣做有很多好處,比如:

  • 可以驗證功能是否正常,以及服務的效能;
  • 用真實有效的流量請求去驗證,又不用造資料,不影響線上正常訪問;
  • 這跟灰度釋出還不太一樣,映象流量不會影響真實流量;
  • 可以用來排查線上問題;
  • 重構,假如服務做了重構,這也是一種測試方式;

 為了實現流量拷貝,Nginx提供了ngx_http_mirror_module模組

2. 安裝Nginx

首頁,設定yum倉庫。為此,建立一個檔案/etc/yum.repos.d/nginx.repo

將以下內容寫入檔案

[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安裝nginx

yum install nginx

預設情況下,nginx配置檔案是nginx.conf

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

為了啟動nginx,直接在命令列裡輸入nginx回車即可

# 啟動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

一旦master程序接收到重新載入配置的訊號,它將檢查新配置檔案的語法是否正確,並嘗試應用其中提供的配置。如果成功,master程序將啟動新的worker程序,併發送訊息給舊的worker程序,要求他們shutdown。否則,master程序將回滾所做的更改,並繼續使用舊配置。舊的worker程序在接收到關閉命令後,停止接受新的連線,直到所有之前已經接受的連線全部處理完為止。之後,舊的worker程序退出。

nginx的master程序的程序ID,預設情況下,放在nginx.pid檔案中,該檔案所在的目錄一般是/usr/local/nginx/logs 或者 /var/run

還可以這樣停止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;
}

3. 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本意是鏡子、映象,這裡可以理解就像一個映象站點一樣,將所有的請求都收集起來,這個映象就代表了所有真實有效的原始請求。有了這個映象,後續我們才可能用這個映象去做一些事情,比如重現一下所有的請求,這就實現了把線上的流程複製到別的地方。

官網給出的示例倒是很簡單,如下:

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;
} 

前面我們安裝了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-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;
    }
}

4. 文件

Nginx文件

http://nginx.org/en/docs/

http://nginx.org/en/docs/http/ngx_http_mirror_module.html

http://nginx.org/en/docs/beginners_guide.html

http://nginx.org/en/docs/http/ngx_http_core_module.html#location 

http://nginx.org/en/docs/configure.html

 

第三方模板 

http://luajit.org/

https://www.nginx.com/resources/wiki/

https://www.nginx.com/resources/wiki/modules/lua/

https://www.nginx.com/resources/wiki/modules/index.html

https://github.com/openresty/lua-nginx-module 

 

補充

# 檢視程序執行時間
ps -eo pid,user,lstart,etime,cmd | grep nginx
# 檢視已經建立連線的數量
netstat -an | grep ESTABLISHED | wc -l
# 檢視80埠的連線數
netstat -an | grep ":80" | wc -l