目錄
安裝Nginx
下載
# 進入下載目錄
cd /root/software
# 下載
wget http://nginx.org/download/nginx-1.18.0.tar.gz
解壓安裝包
# 進入安裝目錄
cd /root/program/
# 創建目錄
mkdir nginx
cd nginx/
# 復制文件
cp /root/software/nginx-1.18.0.tar.gz .
# 解壓文件
tar -zxvf nginx-1.18.0.tar.gz
安裝依賴
# 安裝gcc,nginx底層采用c++編寫,因此需要gcc環境進行編譯
yum install gcc-c++
# 安裝pcre,一個Perl庫,包括perl兼容的正則表達式,nginx的http模塊使用pcre來解析正則表達式
yum install pcre pcre-devel
# 安裝zlib,zlib庫提供了多種壓縮和解壓縮方式,nginx使用zlib對http包的內容進行gzip
yum install zlib zlib-devel
# 安裝openssl,openssl是一個強大的安全套接字層密碼庫,囊括了主要的密碼算法、常用的秘鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用
yum install openssl openssl-devel
安裝
# 進入安裝目錄
cd /root/program/nginx
# 創建安裝目錄
mkdir nginx
# 指定安裝目錄編譯
cd /root/program/nginx/nginx-1.18.0
./configure --prefix=/root/program/nginx/nginx
# 編譯
cd /root/program/nginx/nginx-1.18.0
make
# 安裝
cd /root/program/nginx/nginx-1.18.0
make install
# 確認安裝后文件,只是生成了啟動文件,並沒有啟動
cd /root/program/nginx/nginx
ll
啟動
# 進入目錄
cd /root/program/nginx/nginx/sbin
# 創建軟連接
ln -s /root/program/nginx/nginx/sbin/nginx /usr/bin/nginx
# 啟動
nginx
修改后配置文件如下:
# 啟動用戶
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
測試訪問
# 地址
http://192.168.110.129/
安裝LuaJIT
LuaJIT,即Lua及時編譯器。
# 進入軟件下載目錄
cd /root/software
# 下載
wget https://luajit.org/download/LuaJIT-2.0.5.tar.gz
# 創建安裝目錄
cd /root/program/
mkdir LuaJIT
cd LuaJIT
# 解壓
cp /root/software/LuaJIT-2.0.5.tar.gz .
tar xf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
# 編譯並安裝
make && make install
# 建立軟連接,如果不建立軟鏈接,則會出現share object錯誤
ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
# 驗證軟連接
ll libluajit-5.1.so.2
# 加載lua庫,加入到ld.so.conf文件
echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
安裝ngx_devel_kit
# 進入軟件下載目錄
cd /root/software
mkdir ngx_devel_kit
cd ngx_devel_kit
# 下載
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
# 創建安裝目錄
cd /root/program/
mkdir ngx_devel_kit
cd ngx_devel_kit
# 解壓
cp /root/software/ngx_devel_kit/v0.2.19.tar.gz .
tar xf v0.2.19.tar.gz
安裝lua-nginx-module
# 進入軟件下載目錄
cd /root/software
mkdir lua-nginx-module
cd lua-nginx-module
# 下載
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
# 創建安裝目錄
cd /root/program/
mkdir lua-nginx-module
cd lua-nginx-module
# 解壓
cp /root/software/lua-nginx-module/v0.10.13.tar.gz .
tar xf v0.10.13.tar.gz
在已安裝的Nginx中添加Lua模塊
# 進入源碼目錄
cd /root/program/nginx/nginx-1.18.0
# 查看編譯參數
nginx -V
# 構造已運行的Nginx的模塊模塊
./configure --prefix=/root/program/nginx/nginx
# 構造新的Nginx模塊參數,注意根據實際情況修改目錄地址
./configure --prefix=/root/program/nginx/nginx **新增模塊配置,運行時刪除** --add-module=/root/program/ngx_devel_kit/ngx_devel_kit-0.2.19/ --add-module=/root/program/lua-nginx-module/lua-nginx-module-0.10.13/
# 重新編譯Nginx,make完成后不要繼續輸入“make install”,以免現在的nginx出現問題
make
# 以上完成后,會在objs目錄下生成一個nginx文件
cd objs
./nginx -V
# 替換Nginx文件
mv /root/program/nginx/nginx/sbin/nginx /root/program/nginx/nginx/sbin/nginx.bak
cp nginx /root/program/nginx/nginx/sbin/
# 重新啟動,重要,一定要停機后重新啟動
nginx -s quit
ps -ef | grep nginx
nginx
ps -ef | grep nginx
Lua腳本測試
編寫lua腳本
# 進入配置文件目錄
cd /root/program/nginx/nginx/conf
# 創建腳本文件
vim test.lua
# 腳本內容
ngx.say("hello world.");
修改nginx.conf
# 在Server節點下添加下面的內置內容:
# 中文
charset utf-8;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file conf/test.lua;
}
# 文件下載服務
location /file_server {
# 內部請求(即一次請求的Nginx內部請求),禁止外部訪問,重要。
internal;
# 文件路徑
alias /root/data/files/;
limit_rate 200k;
# 瀏覽器訪問返回200,然后轉由后台處理
#error_page 404 =200;
}
###########################################################################
# 驗證配置文件
nginx -t
# 重新加載配置文件
nginx -s reload
# 測試訪問
http://192.168.110.129/lua
https://blog.csdn.net/yinjinshui/article/details/109738580
連接單例Redis
下載第三方依賴庫
# Git地址
https://github.com/openresty/lua-resty-redis
復制第三方依賴庫
# 進入系統第三方包目錄
cd /usr/local/lib
# 將解壓后的文件上傳至該目錄
# 目錄名稱:lua-resty-redis-master
# 包文件路徑
cd /usr/local/lib/lua-resty-redis-master/lib/resty
在nginx配置文件中添加依賴
# 進入目錄
cd /root/program/nginx/nginx/conf/
# 修改配置文件
vim nginx.conf
# 在http節點下添加下面配置,具體目錄地址視具體服務器調整
# you do not need the following line if you are using
# the OpenResty bundle:
lua_package_path "/usr/local/lib/lua-resty-redis-master/lib/resty/?.lua;;";
編寫腳本文件
# 進入腳本文件目錄
cd /root/program/nginx/nginx/conf
# 修改腳本
vim test.lua
# 腳本內容如下所示:
local redis = require "resty.redis"
--local cjson = require "cjson"
local red = redis:new()
-- 從header中取值
--local token = ngx.req.get_headers()[“token”];
local args = ngx.req.get_uri_args();
-- 獲取參數
local userid = args.userid;
local showlength = args.showlength;
red:set_timeouts(1000, 1000, 1000) -- 1 sec
local ok, err = red:connect("10.120.160.110", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local redis_key = "Navigation:"..userid;
local res, err = red:get(redis_key)
if not res then
ngx.say("failed to get: ", err)
return
end
if res == ngx.null then
ngx.say("<b>not found.</b>")
return
end
--ngx.say("<b>[Navigation:"..userid.."]: </b>", string.sub(res,1,showlength), "<br/>")
-- 請求文件
res = ngx.location.capture("/file_server/1.zip");
if res.status ~= 200 then
ngx.print("Not found file.");
return;
end
-- 下載文件
ngx.header["Content-Disposition"] = "attachment; filename=1.zip";
ngx.header["Content-Type"] ="application/x-zip-compressed";
ngx.print(res.body);
執行
# 檢查nginx配置
nginx -t
# 重新加載配置
nginx -s reload
# 瀏覽器中輸入測試地址
http://192.168.110.129/lua?userid=10076&showlength=500
# 請求后可以直接下載文件。
連接Redis集群
下載第三方依賴庫
# Redis集群連接庫依賴連接Redis單實例連接庫[resty.redis],因此需要提前安裝此第三方依賴庫
# resty-redis-cluster
https://github.com/steve0511/resty-redis-cluster
# lua-resty-lock
https://gitee.com/mirrors_bungle/lua-resty-lock/tree/master
復制第三方依賴庫
# 進入系統第三方包目錄
cd /usr/local/lib
# 將解壓后的文件上傳至該目錄
# 目錄名稱:resty-redis-cluster-master
# 包文件路徑
cd /usr/local/lib/resty-redis-cluster-master/lib/resty
cd /usr/local/lib/lua-resty-lock/lib/resty
在nginx配置文件中添加依賴
# 進入目錄
cd /root/program/nginx/nginx/conf/
# 修改配置文件
vim nginx.conf
# 修改內容如下,具體視需求而定
# 在http節點下添加下面配置,具體目錄地址視具體服務器調整
lua_package_path "/usr/local/lib/lua-resty-redis-master/lib/?.lua;;;/usr/local/lib/lua-resty-lock/lib/resty/?.lua;;;/usr/local/lib/resty-redis-cluster-master/lib/?.lua;;;/usr/local/lib/resty-redis-cluster-master/lib/resty/?.lua;;";
# 添加緩存配置
lua_shared_dict redis_cluster_slot_locks 100k;
# 在Server節點下添加下面的內置內容:
# 中文
charset utf-8;
location /lua/cluster/ {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file conf/test_cluster.lua;
}
# 文件下載服務
location /file_server {
# 內部請求(即一次請求的Nginx內部請求),禁止外部訪問,重要。
internal;
# 文件路徑
alias /root/data/files/;
limit_rate 200k;
# 瀏覽器訪問返回200,然后轉由后台處理
#error_page 404 =200;
}
# 驗證配置文件
nginx -t
# 重新生效配置文件
nginx -s reload
編寫腳本文件
# 進入腳本文件目錄
cd /root/program/nginx/nginx/conf
# 修改腳本
vim test_cluster.lua
# 腳本內容如下所示:
-- 獲取請求信息
local request_uri = ngx.var.request_uri;
local args = ngx.req.get_uri_args();
-- 獲取參數
local key = args.key;
local internal_file_name = args.filename;
local file_type = request_uri:match(".+%.(%w+)");
local config = {
name = "testCluster", --rediscluster name
serv_list = { --redis cluster node list(host and port),
{ ip = "10.120.160.110", port = 7000 },
{ ip = "10.120.160.114", port = 7000 },
{ ip = "10.120.68.96", port = 7000 },
{ ip = "10.120.160.110", port = 7001 },
{ ip = "10.120.160.114", port = 7001 },
{ ip = "10.120.68.96", port = 7001 }
},
keepalive_timeout = 60000, --redis connection pool idle timeout
keepalive_cons = 1000, --redis connection pool size
connect_timeout = 1000, --timeout while connecting
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5, --maximum retry attempts for redirection
max_connection_attempts = 1, --maximum retry attempts for connection
auth = "***************" --set password while setting auth
}
local redis_cluster = require "rediscluster"
local red_c = redis_cluster:new(config)
local redis_key = key;
local res, err = red_c:get(redis_key)
if err or (not res) or (res == ngx.null) then
ngx.exit(401);
return;
end
return ngx.exec("/file_server/"..internal_file_name.."."..file_type);
執行
# 檢查nginx配置
nginx -t
# 重新加載配置
nginx -s reload
# 瀏覽器中輸入測試地址
http://192.168.110.129/lua/cluster/張三.zip?key=name&filename=1
http://192.168.110.129/lua/cluster/test01.docx?key=name&filename=test
# 請求后可以直接下載文件。
附錄-nginx.conf配置文件
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# you do not need the following line if you are using
# # the OpenResty bundle:
lua_package_path "/usr/local/lib/lua-resty-redis-master/lib/?.lua;;;/usr/local/lib/lua-resty-lock/lib/?.lua;;;/usr/local/lib/resty-redis-cluster-master/lib/?.lua;;;/usr/local/lib/resty-redis-cluster-master/lib/resty/?.lua;;";
# 添加緩存配置
lua_shared_dict redis_cluster_slot_locks 100k;
server {
listen 80;
server_name localhost;
# 中文
charset utf-8;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file conf/test.lua;
}
location /lua/cluster {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file conf/test_cluster.lua;
}
# 文件下載服務
location /file_server {
# 內部請求(即一次請求的Nginx內部請求),禁止外部訪問,重要。
internal;
lua_code_cache off;
alias /root/data/files/;
limit_rate 1024k;
add_header Cache-Control no-store; # 禁止瀏覽器緩存文件
# 瀏覽器訪問返回200,然后轉由后台處理
#error_page 404 =200;
}
# 文件下載服務
location /test_file_server {
# 內部請求(即一次請求的Nginx內部請求),禁止外部訪問,重要。
#internal;
lua_code_cache off;
set $filepath "";
# 文件路徑
content_by_lua_file conf/test_file_server.lua;
#return 200 $filepath;
#try_files $filepath $filepath/;
}
# 文件下載服務
location /test_find_file {
# 內部請求(即一次請求的Nginx內部請求),禁止外部訪問,重要。
internal;
lua_code_cache off;
alias /root/data/files/;
limit_rate 200k;
# 瀏覽器訪問返回200,然后轉由后台處理
#error_page 404 =200;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}