openresty + lua 데이터 백분율 배분 실현
#!/bin/bash
#NGINX_SERVER_IP="10.153.167.190"
SOURCE_DIR="/data/jenkins_data"
TARGET_DIR="/usr/local"
NGINX_SOURCE_FILE="openresty-1.9.7.3.tar.gz"
NGINX_TARGET_FILE="openresty-1.9.7.3"
INSTALL_DIR="$TARGET_DIR/openresty/nginx/sbin"
echo "servers : ${NGINX_SERVER_IP}"
IP_ARR=(${NGINX_SERVER_IP//,/})
echo "prepare to install pcre"
for IP in ${IP_ARR[@]}
do
ssh root@$IP "yum install -y gcc gcc-c++ && wait"
ssh root@$IP "yum install -y pcre-devel && wait"
ssh root@$IP "yum install -y readline-devel && wait"
ssh root@$IP "yum install -y openssl-devel && wait"
echo "prepare to install nginx"
scp $SOURCE_DIR/openresty-1.9.7.3.tar.gz root@$IP:$TARGET_DIR
ssh root@$IP "cd $TARGET_DIR && tar -zxvf $NGINX_SOURCE_FILE "
ssh root@$IP "cd $TARGET_DIR/$NGINX_TARGET_FILE && ./configure --with-luajit && make && make install && sleep 15"
#ssh root@$IP "cd /lib64 && ln -s libpcre.so.0.0.1 libpcre.so.1"
ssh root@$IP "if [ -e /lib64/libpcre.so.1 ];then echo 'file exits...';else cd /lib64 && ln -s libpcre.so.0.0.1 libpcre.so.1;fi"
ssh root@$IP "cd $INSTALL_DIR && ./nginx"
done
echo " start nginx ! "
설치 디 렉 터 리: / usr / local / openresty
2. lua 스 크 립 트 설정:
그 다음 에 우 리 는 murmurhash 2 의 lua 라 이브 러 리 를 통 해 사용자 uid 에 대한 데이터 분할 기능 을 실현 합 니 다.구체 적 인 수 요 는 다음 과 같다.
url 에서 uid 매개 변수 값 을 hash 로 계산 하고 모델 을 추출 하 며 5% 의 데 이 터 를 upstreamA 로 가리 키 고 나머지 데 이 터 는 upstreamB 로 갑 니 다.
1) murmurhash 2 lua 라 이브 러 리 설치:
다운로드 주소:https://www.rootopen.com/git/5b8ba95364fec049aff8f0dc
github 주소:https://github.com/bungle/lua-resty-murmurhash2
다운로드 후, murmurhash 2. lua 를 / usr / local / openresty / lualib / resty 디 렉 터 리 에 복사 합 니 다.
2) 설정:
upstream local_worker_A {
server 10.9.8.9:80;
server 10.9.9.9:80;
keepalive 1024;
}
upstream local_worker_B {
server 10.6.80.6:8088;
server 10.6.80.7:8088;
keepalive 1024;
}
server {
listen 80 default;
server_name localhost;
root /data/none;
index index.html index.htm;
### deny user agent
if ($http_user_agent ~* (apachebench|spider|robot|wget)) {
return 403;
}
## location
location @worker_A{
proxy_pass http://local_worker_A;
}
location @worker_B{
proxy_pass http://local_worker_B;
}
### refuse illegal access
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
### speed limit
#limit_req zone=java burst=50 nodelay;
location / {
root html;
index index.html index.htm index.php;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Connection "";
#lua_code_cache off;
content_by_lua_block {
local mmh2 = require "resty.murmurhash2"
local uid=ngx.var.arg_uid or ""
if(string.len(uid) ~= 0)
then
local hash = mmh2(uid)
if (hash % 100 > 94)
then
ngx.exec("@worker_redian")
else
ngx.exec("@worker_old")
end
end
}
#proxy_pass http://local_worker_A;
}
}
이 안 에는 두 가지 주의 가 필요 하 다.
1. lua 스 크 립 트 에서 nginx 의 내장 변 수 를 가 져 오고 ngx. var. XXX 방식 을 사용 합 니 다.
$arg_uid: nginx 에서 명령 을 받 으 면 url 에서 uid 매개 변수 값 을 가 져 올 수 있 습 니 다. lua 에서 ngx. var. arg 을 통 해uid 가 져 옵 니 다.
2 、 contentby_lua_block 에 서 는 ngx. exec 를 통 해 해당 하 는 점프 명령 만 수행 할 수 있 습 니 다.
3. nginx. conf 파일 의 server {...} 에 lua 를 추가 합 니 다.code_cache off; lua 스 크 립 트 를 쉽게 디 버 깅 할 수 있 습 니 다. lua 스 크 립 트 를 수정 한 후에 reload ngix 가 필요 없습니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Rails Turbolinks를 페이지 단위로 비활성화하는 방법원래 Turobolinks란? Turbolinks는 링크를 생성하는 요소인 a 요소의 클릭을 후크로 하고, 이동한 페이지를 Ajax에서 가져옵니다. 그 후, 취득 페이지의 데이터가 천이 전의 페이지와 동일한 것이 있...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.