1. 程式人生 > >打造雙層nginx,分發層與應用層

打造雙層nginx,分發層與應用層

分發層+應用層,雙層nginx

分發層nginx,負責流量分發的邏輯和策略,這個裡面它可以根據你自己定義的一些規則,比如根據productId去進行hash,然後對後端的nginx數量取模

將某一個商品的訪問的請求,就固定路由到一個nginx後端伺服器上去,保證說只會從redis中獲取一次快取資料,後面全都是走nginx本地快取了

後端的nginx伺服器,就稱之為應用伺服器; 最前端的nginx伺服器,被稱之為分發伺服器

看似很簡單,其實很有用,在實際的生產環境中,可以大幅度提升你的nginx本地快取這一層的命中率,大幅度減少redis後端的壓力,提升效能

分發層搭建

部署openresty

mkdir -p /usr/servers
cd /usr/servers/

yum install -y readline-devel pcre-devel openssl-devel gcc

wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
tar -xzvf ngx_openresty-1.7.7.2.tar.gz
cd /usr/servers/ngx_openresty-1.7.7.2/

cd bundle/LuaJIT-2.1-20150120/
make clean && make && make install
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit

cd bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -xvf 2.3.tar.gz

cd bundle
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -xvf v0.3.0.tar.gz

cd /usr/servers/ngx_openresty-1.7.7.2
./configure –prefix=/usr/servers –with-http_realip_module –with-pcre –with-luajit –add-module=./bundle/ngx_cache_purge-2.3/ –add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
make && make install

cd /usr/hello/lualib/resty/
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua

cd /usr/servers/
ll

/usr/servers/luajit
/usr/servers/lualib
/usr/servers/nginx
/usr/servers/nginx/sbin/nginx -V

啟動nginx: /usr/servers/nginx/sbin/nginx

工程化的nginx+lua專案結構

專案工程結構

hello
    hello.conf     
    lua              
        hello.lua
    lualib            
        *.lua
        *.so

放在/usr/hello目錄下

vim /usr/servers/nginx/conf/nginx.conf

worker_processes  2;  

error_log  logs/error.log;  

events {  
    worker_connections  1024;  
}  

http {  
    include       mime.types;  
    default_type  text/html;  

    lua_package_path "/usr/hello/lualib/?.lua;;";  
    lua_package_cpath "/usr/hello/lualib/?.so;;"; 
    include /usr/hello/hello.conf;  
}  
  • 中文不亂碼,需要在server中新增 charset UTF-8;

構建結構

cp -rf /usr/servers/luajit /usr/hello

cp -rf /usr/servers/lualib /usr/hello

vim /usr/hello/hello.conf

server {  
    listen       80;  
    server_name  _;  

    charset UTF-8;

    location /lua {  
        default_type 'text/html';  
        lua_code_cache off;  
        content_by_lua_file /usr/hello/lua/test.lua;  
    }  
}

cd /usr/hello

mkdir lua

cd lua

vim test.lua

ngx.say("111")

測試訪問

localhost/lua

應用層層搭建

新開兩臺虛擬機器,參照分發層搭建教程進行搭建

分發層lua指令碼 test.lua

local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]

local hosts = {"192.168.48.129", "192.168.48.130"}
local hash = ngx.crc32_long(productId)

local index = (hash % 2) + 1
local backend = "http://"..hosts[index]

local requestPath = uri_args["requestPath"]
requestPath = "/"..requestPath.."?productId="..productId

local http = require("resty.http")
local httpc = http.new()

local resp, err = httpc:request_uri(backend,{
  method = "GET",
  path = requestPath
})

if not resp then
  ngx.say("request error: ", err)
  return
end

ngx.say(resp.body)

httpc:close()