Nginx 又一牛 X 功能:流量拷貝

1. 需求 html

將生產環境的流量拷貝到預上線環境或測試環境,這樣作有不少好處,好比:java

  • 能夠驗證功能是否正常,以及服務的性能;
  • 用真實有效的流量請求去驗證,又不用造數據,不影響線上正常訪問;
  • 這跟灰度發佈還不太同樣,鏡像流量不會影響真實流量;
  • 能夠用來排查線上問題;
  • 重構,假如服務作了重構,這也是一種測試方式;

爲了實現流量拷貝,Nginx提供了ngx_http_mirror_module模塊nginx

2. 安裝Nginxgit

首頁,設置yum倉庫。爲此,建立一個文件/etc/yum.repos.d/nginx.repogithub

將如下內容寫入文件面試

[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 -y

默認狀況下,nginx配置文件是nginx.confcentos

通常狀況下,nginx.conf文件在 /usr/local/nginx/conf  或者 /etc/nginx  或者 /usr/local/etc/nginx 目錄下api

爲了啓動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....

接下來,編譯安裝,例如:

./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..._http_mirror_module.html

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

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

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 -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/cjsbl...

推薦去個人博客閱讀更多:

1.Java JVM、集合、多線程、新特性系列教程

2.Spring MVC、Spring Boot、Spring Cloud 系列教程

3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程

4.Java、後端、架構、阿里巴巴等大廠最新面試題

生活很美好,明天見~

相關文章
相關標籤/搜索