我們這裡使用nginx,全都會在nginx裡去寫lua腳本,因為我們需要自定義一些特殊的業務邏輯。
比如說,流量分發,自己用lua去寫分發的邏輯,在分發層nginx裡去寫的
再比如說,要用lua去寫多級緩存架構存取的控制邏輯,在應用層nginx裡去寫的
後面還要做熱點資料的自動降級機制,也是用lua腳本去寫降級機制的,在分發層nginx裡去寫的
因為我們要用nginx+lua去開發,是以會選擇用最流行的開源方案,就是用OpenResty
nginx+lua打包在一起,而且提供了包括redis用戶端,mysql用戶端,http用戶端在内的大量的元件
我們這一章節是去部署應用層nginx,會采用OpenResty的方式去部署nginx,而且會帶着大家寫一個nginx+lua開發的一個hello world
1、部署第一個nginx,作為應用層nginx(01那個機器上)
(1)部署openresty
OpenResty(又稱:ngx_openresty) 是一個基于 NGINX 的可伸縮的 Web 平台,由中國人章亦春發起,提供了很多高品質的第三方子產品。
OpenResty 是一個強大的 Web 應用伺服器,Web 開發人員可以使用 Lua 腳本語言調動 Nginx 支援的各種 C 以及 Lua 子產品,更主要的是在性能方面,OpenResty可以 快速構造出足以勝任 10K 以上并發連接配接響應的超高性能 Web 應用系統。
//建立檔案位置
mkdir -p /opt/openrestydir
cd /opt/openrestydir
#OpenResty 依賴庫有: perl 5.6.1+, libreadline, libpcre, libssl。
#是以我們需要先安裝好這些依賴庫
yum install -y readline-devel pcre-devel openssl-devel gcc
#接下我們可以在官方(https://openresty.org/cn/)下載下傳最新的 OpenResty 源碼包并解壓編#譯安裝:
cd /opt/openrestydir
wget https://openresty.org/download/ngx_openresty-1.9.7.1.tar.gz
tar -xzvf ngx_openresty-1.9.7.1.tar.gz
cd /opt/openrestydir/ngx_openresty-1.9.7.1.tar.gz
# 裝lua得jit
cd /opt/openrestydir/ngx_openresty-1.9.7.1/bundle/LuaJIT-2.1-20151219
make clean && make && make install
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit
# 在bundle下安裝一些東西
cd /opt/openrestydir/ngx_openresty-1.9.7.1/bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -xvf 2.3.tar.gz
cd /opt/openrestydir/ngx_openresty-1.9.7.1/bundle
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -xvf v0.3.0.tar.gz
# 最後安裝openresty
cd /opt/openrestydir/ngx_openresty-1.9.7.1
#配置安裝位置,和添加一些子產品
./configure --prefix=/opt/openrestydir --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
檢視版本
/opt/openrestydir/nginx/sbin -V
啟動nginx: /usr/servers/nginx/sbin/nginx
(2)nginx+lua開發的hello world
vim /opt/openrestydir/nginx/conf/nginx.conf
在http部分添加:
/opt/openrestydir/nginx/conf下,建立一個lua.conf
server {
listen 80;
server_name _;
}
在nginx.conf的http部分添加:
include lua.conf;
驗證配置是否正确:
/opt/openrestydir/nginx/sbin/nginx -t
在lua.conf的server部分添加:
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("hello world")';
}
/opt/openrestydir/nginx/sbin/nginx -t
重新nginx加載配置
/opt/openrestydir/nginx/sbin/nginx -s reload
通路http: http://192.168.1.51/lua
vi /opt/openrestydir/nginx/conf/lua/test.lua,添加
ngx.say("hello world");
修改lua.conf
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua;
}
檢視異常日志
tail -f /opt/openrestydir/nginx/logs/error.log
(3)工程化的nginx+lua項目結構
項目工程結構
hello
hello.conf
lua
hello.lua
lualib
*.lua
*.so
放在 /opt/openrestydir/hello目錄下
/opt/openrestydir/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 /opt/openrestydir/hello/hello.conf;
}
/opt/openrestydir/hello/hello.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /opt/openrestydir/hello/lua/test.lua;
}
}
如法炮制,在另外一個機器上,也用OpenResty部署一個nginx