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

        共 6780字,需瀏覽 14分鐘

         ·

        2021-03-15 07:48


        原文:https://www.cnblogs.com/cjsblog/p/12163207.html

        作者:廢物大師兄

        1. 需求

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

        • 可以驗(yàn)證功能是否正常,以及服務(wù)的性能;

        • 用真實(shí)有效的流量請(qǐng)求去驗(yàn)證,又不用造數(shù)據(jù),不影響線上正常訪問;

        • 這跟灰度發(fā)布還不太一樣,鏡像流量不會(huì)影響真實(shí)流量;

        • 可以用來排查線上問題;

        • 重構(gòu),假如服務(wù)做了重構(gòu),這也是一種測(cè)試方式;

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


        2. 安裝Nginx

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

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

        [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


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

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

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

        # 啟動(dòng)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進(jìn)程接收到重新加載配置的信號(hào),它將檢查新配置文件的語法是否正確,并嘗試應(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

        kill -s QUIT 3997


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

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

        官網(wǎng)給出的示例倒是很簡(jiǎn)單,如下:
        location / {
            mirror /mirror;
            proxy_pass http://backend;
        }

        location = /mirror {
            internal;
            proxy_pass http://test_backend$request_uri;
        }


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

        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模塊,因此,真正要使用的時(shí)候最好還是采用自定義安裝,即從源碼構(gòu)建。


        首先,下載源碼  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;
           # 源站點(diǎn)
            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;

                # 流量復(fù)制
          mirror /newapi;
          mirror /mirror2;
          mirror /mirror3;

          # 復(fù)制請(qǐng)求體
          mirror_request_body on;
            }

            # 鏡像站點(diǎn)
            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 

         

        補(bǔ)充

        # 查看進(jìn)程運(yùn)行時(shí)間
        ps -eo pid,user,lstart,etime,cmd | grep nginx
        # 查看已經(jīng)建立連接的數(shù)量
        netstat -an | grep ESTABLISHED | wc -l
        # 查看80端口的連接數(shù)
        netstat -an | grep ":80" | wc -l
        瀏覽 36
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 国产探花系列视频在线观看 | 别墅轮换游戏五对情侣图片 | 久久久久网精品影视一起草 | 日韩视频欧美视频 | 无遮挡性视频真人免费 |