安装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;
} } }

Nginx使用Lua脚本连接Redis验证身份并下载文件的更多相关文章

  1. nginx插入lua脚本访问redis

    目标:收集用户日志 流程: 浏览器端get方法将数据传到nginx服务 nginx收集到数据,执行内嵌lua脚本,访问redis,根据token获得用户id 将日志信息存入文件 1.nginx安装,参 ...

  2. Lua脚本在Redis事务中的应用实践

    使用过Redis事务的应该清楚,Redis事务实现是通过打包多条命令,单独的隔离操作,事务中的所有命令都会按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命令请求所打断.事务中的命令要么全部 ...

  3. Lua脚本在redis分布式锁场景的运用

    目录 锁和分布式锁 锁是什么? 为什么需要锁? Java中的锁 分布式锁 redis 如何实现加锁 锁超时 retry redis 如何释放锁 不该释放的锁 通过Lua脚本实现锁释放 用redis做分 ...

  4. 运维实践-最新Nginx二进制构建编译lua-nginx-module动态链接Lua脚本访问Redis数据库读取静态资源隐式展现

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x0n 前言 ...

  5. c#中用lua脚本执行redis命令

    直接贴出代码,实现执行lua脚本的方法,用到的第三方类库是 StackExchange.Redis(nuget上有) 注:下面的代码是简化后的,实际使用要修改, using System; using ...

  6. Redis进阶之使用Lua脚本自定义Redis命令

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 1.在Redis ...

  7. 使用Lua 脚本实现redis 分布式锁,报错:ERR Error running script (call to f_8ea1e266485534d17ddba5af05c1b61273c30467): @user_script:10: @user_script: 10: Lua redis() command arguments must be strings or integers .

    在使用SpringBoot开发时,使用RedisTemplate执行 redisTemplate.execute(lockScript, redisList); 发现报错: ERR Error run ...

  8. 使用nginx+lua脚本读写redis缓存

    配置 新建spring boot项目增加redis配置 <dependency> <groupId>org.springframework.boot</groupId&g ...

  9. paramiko连接远程主机,上传下载文件

    Paramiko是基于SSHv2协议实现的一个Python模块,提供客户端和服务器的功能.Paramiko本身是一个围绕SSH网络概念的纯Python接口. Client: # 创建一个SSH连接对象 ...

  10. 使用脚本在FTP上传、下载文件

    由于最近勒索病毒变种又一次爆发,公司内部封锁了TCP 445端口.导致原来通过文件共享的方式上传下载的计划任务无法执行.所以,我开设了FTP服务器来完成这个工作. 关于如何建立FTP服务器,请看这里 ...

随机推荐

  1. django restframework的简单使用

    django restframework的简单使用 1.快速上手 配置: (pip install djangorestframework==3.12.4) # settings.py INSTALL ...

  2. AI人工智能简史

    AI人工智能简史 最近学习AI,顺便整理了一份AI人工智能简史,大家参考: 1951年 第一台神经网络机,称为SNARC: 1956年 达特茅斯学院会议,正式确立了人工智能的研究领域: 1966年 M ...

  3. java中各引用类型的生存时间

    引用类型由上往下一次减弱: 强引用:Object obj=new Object(),无论什么情况下,只要强引用关系还存在,就不会回收被引用的对象. 软引用:像系统中缓存这些,在系统即将报内存溢出异常时 ...

  4. 2022-08-19:以下go语言代码输出什么?A:equal;B:not equal;C:不确定。 package main import ( “fmt“ “reflect“ )

    2022-08-19:以下go语言代码输出什么?A:equal:B:not equal:C:不确定. package main import ( "fmt" "refle ...

  5. 2020-10-31:java中LinkedTransferQueue和SynchronousQueue有什么区别?

    福哥答案2020-11-01:SynchronousQueue:线程A使用put将数据添加到队列,如果没有其他线程使用take去获取数据,那么线程A阻塞,直到数据被其他线程获取,同理 如果线程B从队列 ...

  6. 2022-04-24:位集 Bitset 是一种能以紧凑形式存储位的数据结构。 请你实现 Bitset 类。 Bitset(int size) 用 size 个位初始化 Bitset ,所有位都是 0

    2022-04-24:位集 Bitset 是一种能以紧凑形式存储位的数据结构. 请你实现 Bitset 类. Bitset(int size) 用 size 个位初始化 Bitset ,所有位都是 0 ...

  7. 【源码解读】asp.net core源码启动流程精细解读

    引言 core出来至今,已经7年了,我接触也已经4年了,从开始的2.1,2.2,3.1,5,6再到如今的7,一直都有再用,虽然我是一个Winform仔,但是源码一直从3.1到7都有再看,然后在QQ上面 ...

  8. Python异步编程之web框架异步vs同步 无IO任务压测对比

    前言 在python编程中,通过协程实现的异步编程号称能够提高IO密集型任务的并发量.本系列比较web服务器同步框架和异步框架的性能差异,包括无IO接口和常见IO操作,如文件.mysql.redis等 ...

  9. 03、SECS-I 协议介绍

    03.SECS-I 协议介绍 上一篇我们学习了 SECS-II 协议,对 SECS-II 协议有了初略的了解,现在我们再来一起学习 SECS-I 协议. 文章的内容基本上来自参考资料和自己看的文档,若 ...

  10. 【编程日记】搭建python开发环境

    0.相关确定 0.1确定操作系统 Python是一种跨平台的编程语言,这意味着它能够运行在所有主要的操作系统中.然而,在不同的操作系统(Windows/Mac/Linux)中,安装Python的方法存 ...