自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Nginx這個(gè)功能厲害了!

系統(tǒng) Linux
為了實(shí)現(xiàn)流量拷貝,Nginx提供了ngx_http_mirror_module模塊,來看一下吧。

 [[384247]]

1. 需求

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

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

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

2. 安裝Nginx

首頁,設(shè)置yum倉庫。為此,創(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. [nginx-mainline]  
  9. name=nginx mainline repo  
  10. baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/  
  11. gpgcheck=1  
  12. enabled=0  
  13. gpgkey=https://nginx.org/keys/nginx_signing.key  
  14. module_hotfixes=true 

yum安裝nginx 

  1. yum install nginx -y 

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

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

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

  1. # 啟動nginx  
  2. nginx  
  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 

一旦master進(jìn)程接收到重新加載配置的信號,它將檢查新配置文件的語法是否正確,并嘗試應(yīng)用其中提供的配置。如果成功,master進(jìn)程將啟動新的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

  1. kill -s QUIT 3997 

初始配置文件長這樣: 

  1. user  nginx;  
  2. worker_processes  1;  
  3. error_log  /var/log/nginx/error.log warn;  
  4. pid        /var/run/nginx.pid;  
  5. events {  
  6.     worker_connections  1024;  
  7.  
  8. http {  
  9.     include       /etc/nginx/mime.types;  
  10.     default_type  application/octet-stream;  
  11.     log_format  main '$remote_addr - $remote_user [$time_local] "$request" '  
  12.                       '$status $body_bytes_sent "$http_referer" '  
  13.                       '"$http_user_agent" "$http_x_forwarded_for"';   
  14.     access_log  /var/log/nginx/access.log main;  
  15.     sendfile        on;  
  16.     #tcp_nopush on;  
  17.     keepalive_timeout  65;  
  18.     #gzip on;  
  19.     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本意是鏡子、鏡像,這里可以理解就像一個(gè)鏡像站點(diǎn)一樣,將所有的請求都收集起來,這個(gè)鏡像就代表了所有真實(shí)有效的原始請求。有了這個(gè)鏡像,后續(xù)我們才可能用這個(gè)鏡像去做一些事情,比如重現(xiàn)一下所有的請求,這就實(shí)現(xiàn)了把線上的流程復(fù)制到別的地方。

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

  1. location / {  
  2.     mirror /mirror;  
  3.     proxy_pass http://backend;  
  4.  
  5. location = /mirror {  
  6.     internal;  
  7.     proxy_pass http://test_backend$request_uri;  

如果請求體被鏡像,那么在創(chuàng)建子請求之前會先讀取請求體。 

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

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

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

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

  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. make & make install 

配置 

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

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  

補(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  

 

責(zé)任編輯:龐桂玉 來源: 馬哥Linux運(yùn)維
相關(guān)推薦

2021-09-17 12:18:53

NginxJavaScript前端

2020-04-27 09:40:43

開源項(xiàng)目 Bug

2017-02-23 08:00:04

智能語音Click

2018-04-11 14:30:33

2018-05-14 22:58:14

戴爾

2021-03-29 13:06:25

開源工具開源

2023-05-06 06:47:46

Bing聊天機(jī)器人

2021-11-01 07:50:44

TomcatWeb應(yīng)用

2021-12-27 07:59:50

ECMAScript JSON模塊Node.js

2022-01-11 12:13:33

JavaScript編程語言

2020-06-08 17:35:27

Redis集群互聯(lián)網(wǎng)

2021-06-03 09:30:30

Python操作注冊表regedit

2022-04-08 08:11:28

Python代碼

2020-03-10 13:35:23

Gihub搜索開源

2019-11-25 21:53:48

代碼算法BUG

2022-05-03 23:44:21

Python動態(tài)鏈接庫Ctypes

2017-07-27 16:51:19

數(shù)字化環(huán)衛(wèi)信息化

2021-05-15 08:02:33

HashMap 散列函數(shù)哈希沖突

2020-06-09 07:42:30

重命名文件 Linux

2018-01-24 10:48:34

神經(jīng)網(wǎng)絡(luò)深度學(xué)習(xí)前端
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號