Home > Backend Development > PHP Tutorial > Nginx copy traffic

Nginx copy traffic

WBOY
Release: 2016-07-29 08:51:21
Original
1340 people have browsed it
Use Ngnix to copy online traffic to the test machine (one http request is regarded as one piece of traffic)
Development environment: CentOS 6.4
nginx installation directory: /usr/local/nginx-1.4.2
Download directory of each module installation package: /data/nginxinstall
1. Install pcre
#yum -y install pcre pcre-devel
2. Install zlib
yum -y install zlib zlib-devel
3. Install LuaJIT
# cd /data/nginxinstall
# wget http: //luajit.org/download/LuaJIT-2.0.2.tar.gz
# tar -xzvf LuaJIT-2.0.2.tar.gz
# cd LuaJIT-2.0.2
# make
lib and include are placed directly /usr/local/lib and usr/local/include
Configure environment variables
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
export LD_LIBRARY_PATH=/usr/local/lib :$LD_LIBRARY_PATH
4. Download and prepare httpLuaModule
# cd /data/nginxinstall
# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz
# tar -xzvf v0 .8.6
5. Start installing Nginx below (trying to use nginx1.10.0 and encountering compilation errors):
# cd /data/nginxinstall/
# wget http://nginx.org/download/nginx-1.4.2.tar .gz
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
# ./configure --prefix=/usr/local/nginx-1.4.2 --add-module=. ./lua-nginx-module-0.8.6
# make -j2
# make install
6. Edit the nginx/conf/nginx.conf file (this file is in json format and can be seen as a tree):
In Add under http nodes:
Upstream online {
server 10.10.12.7:80 ;
}
upstream test {
server 10.10.10.103:80 ;
}
location ~* ^/antispam { Bleak client_body_buffer_size 2m;
Set $svr "on"; ^/s1(.*)$ $1 break;
proxy_pass http://s1; -- Reverse proxy, jump to upstream online
}
location ~* ^/test {
log_subrequest on;
rewrite ^/test(.*)$ $1 break;
proxy_pass http: //test; -- Reverse proxy, jump to upstream test
}
7. Edit nginx/copy_flow.lua file:
local online, test, action
action = ngx.var.request_method
if action == "POST " then
ngx.req.read_body() -- Be sure to read the body before parsing the body parameters
local arg = ngx.req.get_post_args() -- post requires parameters to be passed
arry = {method = ngx.HTTP_POST, body = ngx.var.request_body, args=arg}
else
arry = {method = ngx.HTTP_GET}
end
if ngx.var.svr == "on" then
online, test = ngx.location.capture_multi {
{ "/online" .. ngx.var.request_uri, arry},
{ "/test" .. ngx.var.request_uri, arry},
}
else
online = ngx.location.capture_multi {
{ "/ online" .. ngx.var.request_uri, arry}
}
end
if online.status == ngx.HTTP_OK then -- only return the results of the online server
ngx.say(online.body)
else
ngx.status = ngx.HTTP_NOT_FOUND



end



8. There is Chinese in the POST parameters, encoding problem:

Nginx configuration UTF8: Configure charset utf-8 under the server node; Format:

The above has introduced Nginx copy traffic, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template