nginx流量拷贝功能介绍


本文摘自PHP中文网,作者V,侵删。

1、我们为什么要将生产环境的流量拷贝到预上线环境或测试环境呢?

这样做得好处有以下几点:

  • 可以验证功能是否正常,以及服务的性能;

  • 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问;

  • 这跟灰度发布还不太一样,镜像流量不会影响真实流量;

  • 可以用来排查线上问题;

  • 重构,假如服务做了重构,这也是一种测试方式;

为了实现流量拷贝,Nginx提供了ngx_http_mirror_module模块

2、安装Nginx

首页,设置yum仓库。为此,创建一个文件/etc/yum.repos.d/nginx.repo

将以下内容写入文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[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

1

yum install nginx

默认情况下,nginx配置文件是nginx.conf

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

为了启动nginx,直接在命令行里输入nginx回车即可

1

2

3

4

5

6

7

8

9

10

11

12

# 启动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

a5ed99a17150f8ee3e4b02f0d61423b.png

c24f7542b8d3e8870818d767644cb93.png

一旦master进程接收到重新加载配置的信号,它将检查新配置文件的语法是否正确,并尝试应用其中提供的配置。如果成功,master进程将启动新的worker进程,并发送消息给旧的worker进程,要求他们shutdown。否则,master进程将回滚所做的更改,并继续使用旧配置。旧的worker进程在接收到关闭命令后,停止接受新的连接,直到所有之前已经接受的连接全部处理完为止。之后,旧的worker进程退出。

(免费学习视频分享:php视频教程)

nginx的master进程的进程ID,默认情况下,放在nginx.pid文件中,该文件所在的目录一般是/usr/local/nginx/logs 或者 /var/run

还可以这样停止nginx

1

kill -s QUIT 3997

初始配置文件长这样:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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本意是镜子、镜像,这里可以理解就像一个镜像站点一样,将所有的请求都收集起来,这个镜像就代表了所有真实有效的原始请求。有了这个镜像,后续我们才可能用这个镜像去做一些事情,比如重现一下所有的请求,这就实现了把线上的流程复制到别的地方。

官网给出的示例倒是很简单,如下:

1

2

3

4

5

6

7

8

location / {

    mirror /mirror;

    proxy_pass http://backend;

}

location = /mirror {

    internal;

    proxy_pass http://test_backend$request_uri;

}

如果请求体被镜像,那么在创建子请求之前会先读取请求体

阅读剩余部分

相关阅读 >>

welcome to nginx是什么意思

502 bad gateway nginx什么意思

提高nginx安全性方法

nginx怎么卸载

提高nginx安全性方法

nginx怎么解决跨域?

phpfpm和nginx如何通信

tomcat和nginx的区别是什么

nginx高级模块有哪些

如何解决nginx中的500、502、503、504错误

更多相关阅读请进入《nginx》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...