复习

1、location 匹配规则:精准 --》普通 --》正则(非正则除外)

2、代理传参:proxy_pass = ip:port , 将整个 path 部分传入 tomcat

​ proxy_pass = ip:port/xxx , 只将匹配 path 的剩余部分传入 tomcat

3、rewrite 【break/last/redirect/permanent/null】

​ 中断无 location/中断有location**/中断 302/中断 301/**不中断 location

4、request 全阶段

​ Server_rewrite/Find_config/Rewrite/access…/Content

​ 前一阶段命令全部执行完毕 ----》进行下一阶段命令

5、index 命令 ----》查找文件存在? ----》是,刷新 location 匹配(回 Find_config 阶段)

一. 负载

upstream

语法格式:

upstream 负载名 { [ip_hash;] server ip:port [weight=数字] [down]; server ip:port [weight=数字]; }

1. 轮询(默认)

upstream order { server 192.168.0.128:8383; server 192.168.244.233:8383;
}

2. 权重 weight

upstream order { server 192.168.0.128:8383 weight=3; server 192.168.244.233:8383 weight=1 down;
}

3. ip_hash

upstream order { ip_hash; server 192.168.0.128:8383; server 192.168.244.233:8383;
}

可以简单解决session问题

4. 使用upstream

格式:proxy_pass http://负载名;

location /order/book {proxy_pass http://upstream-name/context
}

二. Openresty 使用

OpenResty 是一个全功能的 Web 应用服务器。它打包了标准的 Nginx 核心,常用的第 三方模块以及大多数依赖项。 可以把它看成是 Nginx 附加众多的第三方插件的合集。其主体是嵌入 lua 脚本的支持,让你能够使用 lua 灵活地处理运算逻辑。

lua 为 Nginx 带来的新的处理方式,及 OpenResty 组件的使用。

0. 卸载nginx

#1.停止nginx
ps -ef | grep nginx
kill -9 pid
# 或者
nginx -s stop#2.查找所有的nginx文件
> find / -name nginx
/etc/nginx
/usr/local/openresty/nginx
/usr/local/nginx
/usr/local/nginx/sbin/nginx
/nginx-1.15.8/objs/nginx#3.删除文件
rm -rf /usr/local/nginx
rm -rf /usr/local/nginx/sbin/nginx

1. Openresty 的安装配置

yum安装方式

此方式简单,缺点是无法干预启停插件

yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty

源码安装方式

wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
tar -zxvf openresty-1.15.8.1.tar.gz
##选择需要的插件启用, --with-Components 激活组件,--without 则是禁止组件
./configure --without-http_redis2_module --with-http_iconv_module
make && make install
vi /etc/profile ##加入 path 路径
source /etc/profile ##生效配置

安装检测

nginx -V ##如下显示,则表示安装成功
nginx version: openresty/1.19.9.1
built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC)
built with OpenSSL 1.1.1k  25 Mar 2021 (running with OpenSSL 1.1.1l  24 Aug 2021)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.20 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.10 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' --with-cc='ccache gcc -fdiagnostics-color=always' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-compat --with-stream --with-http_ssl_module

2. Lua 介入 Nginx

主要帮助对 http 请求取参、取 header 头、输出等

ngx.arg 指令参数,如跟在 content_by_lua_file 后面的参数

ngx.var request 变量,ngx.var.VARIABLE 引用某个变量

ngx.ctx 请求的 lua 上下文

ngx.header 响应头,ngx.header.HEADER 引用某个头

ngx.status 响应码

ngx.log 输出到 error.log

ngx.send_headers 发送响应头

ngx.headers_sent 响应头是否已发送

ngx.resp.get_headers 获取响应头

ngx.is_subrequest 当前请求是否是子请求

ngx.location.capture 发布一个子请求

ngx.location.capture_multi发布多个子请求

ngx.print 输出响应

ngx.say 输出响应,自动添加‘\n‘

ngx.flush 刷新响应

ngx.exit 结束请求

Lua 嵌入 Nginx 的时机阶段

Nginx 执行 lua 脚本片断时,需要明确指明执行的 nginx 阶段时机。主要有以下几种时机:

set_by_lua* : 设置 nginx 变量,实现复杂的赋值逻辑

rewrite_by_lua* : 实现转发、重定向等功能

access_by_lua* : IP 准入、接口访问权限等情况集中处理

content_by_lua* : 接收请求处理并输出响应

header_filter_by_lua* : 设置 header cookie

body_filter_by_lua* : 对响应数据进行过滤,如截断**/**替换等

3. Lua 基础功能

openrety默认集成了lua脚本功能

1. hello world

server {listen 81;server_name say.hello.com;default_type  text/plain; # 不添加就会直接让你下载, 所以说默认是文件类型?location /hello {## ngx.say -- 输入文本content_by_lua 'ngx.say("hello, Openresty")'; ## 为什么返回变成了下载, 因为不知道我们返回的是什么内容, 需要添加类型}
}
## 可以配置在http中, 也可以单独为每一个server配置
default_type  application/octet-stream; ## 文本流方式下载
default_type  text/plain; ## 文本方式解析

2. 执行 lua 脚本文件

server {listen 81;server_name say2.hello.com;default_type  text/plain;location /hello {## ngx.say -- 输入文本content_by_lua_file /usr/local/openresty/nginx/conf/other.d/lua/lua_say_hello.lua; }
}
--/usr/local/openresty/nginx/conf/other.d/lua/lua_say_hello.lua
ngx.say("hello , Openresty by lua")

3. lua 取 get 参数

server {listen 81;server_name args.com;default_type  text/plain;location /args {content_by_lua_file /usr/local/openresty/nginx/conf/other.d/lua/lua_args.lua; }location /args2 {##ngx.var表示取参数列表, arg_a表示取参数acontent_by_lua_block {--这里的注释只能使用--,即使用lua的注释语法ngx.say(ngx.var.arg_a)ngx.say(ngx.var.arg_b)}}
}

lua_args.lua

--lua的注释
--key-value形式取得所有的url上的参数--get型参数local arg = ngx.req.get_uri_args() -- 取全量参数
for k,v in pairs(arg) dongx.say("[GET ] ", k, " :", v)
end--key-value形式取得所有post的参数ngx.req.read_body() --解析 body 参数之前一定要先读取 body local arg = ngx.req.get_post_args()
for k,v in pairs(arg) dongx.say("[POST] ", k, " :", v)
end

4. lua取header信息

lua_req.conf

server {listen 81;server_name req.com;default_type  text/plain;location /req {content_by_lua_file /usr/local/openresty/nginx/conf/other.d/lua/lua_req.lua; }
}

lua_req.lua

--读请求头信息
local headers = ngx.req.get_headers()
ngx.say("Host : ", headers.Host)
ngx.say("Host : ", headers["Host"])
ngx.say("--------------")
for k,v in pairs(headers) doif type(v) == "table" then--table.concat是table操作,意指将v内所有值合并ngx.say(k, " : ", table.concat(v, ","))elsengx.say(k, " : ", v)end
end

http://req.com:81/req

Host : req.com:81
Host : req.com:81
--------------
connection : keep-alive
upgrade-insecure-requests : 1
accept-language : zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
user-agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55
accept-encoding : gzip, deflate
host : req.com:81
accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9

5. 给 lua 脚本传参

server {listen 81;server_name set.com;default_type  text/plain;location /set {##给lua脚本传递参数set_by_lua_file $val "/usr/local/openresty/nginx/conf/other.d/lua/set.lua" $arg_a $arg_b;        echo $val;}
}

set.lua

local a=tonumber(ngx.arg[1])
local b=tonumber(ngx.arg[2])
return a + b

http://set.com:81/set?a=1&b=2

6. 权限校验

指定在 access 阶段执行脚本

server {listen 81;server_name acc.com;default_type  text/plain;location /acc {##给lua脚本传递参数access_by_lua_file "/usr/local/openresty/nginx/conf/other.d/lua/access.lua";        echo "welcome $arg_name !";}
}

access.lua

if ngx.var.arg_passwd == "123456"
thenreturn
else            ngx.exit(ngx.HTTP_FORBIDDEN)
end

http://acc.com:81/acc?name=hgy&passwd=12345

7. 内容过滤

Nginx 有时候,需要对下游服务生成的内容进行处理过滤

server {listen 81;server_name filter.com;default_type  text/plain;location /filter {echo 'hello nginx';echo 'you are welcome!';body_filter_by_lua_file "/usr/local/openresty/nginx/conf/other.d/lua/filter.lua";}
}

filter.lua

--ngx.arg[1]是输出块内容
local chunk = ngx.arg[1]
if string.match(chunk, "hello") thenngx.arg[2] = true  -- 设置为true,表示输出结束 eof return
end-- just throw away any remaining chunk data
ngx.arg[1] = nil

http://filter.com:81/filter?k=hello123

8. 重定向

server {listen 81;server_name rew.com;location /rew {##重定向rewrite_by_lua 'ngx.exec("/hello")';echo "I am rewrite_by_lua";}location /hello {echo "hello kitt";}location /redirect {##页面重定向,是要生成内容的,因此使用content阶段content_by_lua_block {ngx.redirect("http://www.baidu.com", 302)}}
}

http://rew.com:81/rew

http://rew.com:81/redirect

4. Lua 引入第三方模块的使用

OpenResty 提供了非常多的第三方插件,支持操作 redis/mysql 等服务,lua 使用它们的模式 一般按以下流程

  • require “resty/xxx” :导入模块功能,类似 java 中的 import 导入类

  • local obj = xxx:new() :模块创建对象 obj

  • local ok, err = obj :connect :对象连接到目标库

  • obj :method :这里可以为所欲为,尽情操纵目标库了

1. Lua-resty-redis 连接 redis 用法

Lua-resty-redis 插件,对 Nginx 操作 redis 的支持十分强大

redis.lua

local redis = require "resty.redis"--打开redis连接
local function open_redis()local red = redis:new()red:set_timeout(1000) -- 超时时间1 secondlocal res = red:connect('192.168.0.128',6379)if not res thenreturn nilendres = red:auth(123456)  --密码校验if not res thenreturn nilendred.close = closereturn red
end--关闭连接
local function close(self)local sock = self.sockif not sock thenreturn nil, "not initialized"endif self.subscribed thenreturn nil, "subscribed state"endreturn sock:setkeepalive(10000, 50)
endlocal key =  'name'
local val =  "100"
local arg = ngx.req.get_uri_args() --取req里所有的参数
for k,v in pairs(arg) dokey = kval = vbreak;
endlocal red = open_redis()
--local value = red:get(key)  --取值
--red:set(key,val)      --设新值
--close(red)red:init_pipeline()
value = red:get(key)
red:set(key, val)
red:commit_pipeline()--返回值到页面
ngx.say(key,':',value)

lua调用库函数使用冒号(

【2. Nginx高级知识】相关推荐

  1. MySQL高级知识(十五)——主从复制

    前言:本章主要讲解MySQL主从复制的操作步骤.由于环境限制,主机使用Windows环境,从机使用用Linux环境.另外MySQL的版本最好一致,笔者采用的MySQL5.7.22版本,具体安装过程请查 ...

  2. Linux的slab和nginx的区别,Nginx核心知识100讲》nginx Slab管理器

    极客专栏<Nginx核心知识100讲>38小节的笔记 nginx 不同的worker之间需要共享信息的时候,只能通过共享内存.共享内存会使用链表,红黑树这样的数据结构.但是每个红黑树上有很 ...

  3. [学习记录] macOS下的Nginx安装 Nginx基本知识

    [学习记录] macOS下的Nginx安装 && Nginx基本知识 一. 安装相关 1. 安装:直接通过brew安装即可 2. 启动: 3. 成功运行判断: 4. 改端口号:由于80 ...

  4. MySQL高级知识(十六)——小表驱动大表

    前言:本来小表驱动大表的知识应该在前面就讲解的,但是由于之前并没有学习数据批量插入,因此将其放在这里.在查询的优化中永远小表驱动大表. 1.为什么要小表驱动大表呢 类似循环嵌套 for(int i=5 ...

  5. MySQL高级知识(十一)——Show Profile

    前言:Show Profile是mysql提供的可以用来分析当前会话中sql语句执行的资源消耗情况的工具,可用于sql调优的测量.默认情况下处于关闭状态,并保存最近15次的运行结果. 1.分析步骤 # ...

  6. MySQL高级知识(一)——基础

    MySQL高级知识(一)--基础 前言:MySQL高级知识 1.关于MySQL的一些文件 MySQL如何安装.如何配置自启动,这里不进行讲述,可自行搜索相关安装教程进行处理.这里主要介绍MySQL的主 ...

  7. check oracle 为空值_索引(index)-Oracle高级知识(3)-数据库(23)

    这篇文章是Oracle数据库的高级知识: 索引(index) 是非常重要的内容.知识点繁杂,靠理解.实战和笔记来熟练运用. 3 索引(Index) 3.1 什么是索引 在关系型数据库中,索引是一种单独 ...

  8. 揭秘认知智能,小i机器人的“高级知识分析师”高级在哪里?

    认知智能有一个非常重要的部分,就是知识. 算法是发动机,知识是燃料,机器人的认知能力就是输出的能量. 作为认知智能领头企业,小i机器人拥有专业.庞大的知识分析团队,这个团队是幕后的主干力量.此次,来自 ...

  9. Nginx高级课程扩容与高效

    Nginx高级 第一部分:扩容 通过扩容提升整体吞吐量 1.单机垂直扩容:硬件资源增加 云服务资源增加 整机:IBM.浪潮.DELL.HP等 CPU/主板:更新到主流 网卡:10G/40G网卡 磁盘: ...

最新文章

  1. 远程服务器的url怎么配置文件,Linux常用命令(5)--SSH访问远程服务器、SCP服务器间文件拷贝...
  2. SpringMVC 学习笔记(五) 基于RESTful的CRUD
  3. 计算机网络技术中的NAT,计算机网络技术中的NAT-2.ppt
  4. 【NLP】Github标星7.7k+:常见NLP模型的PyTorch代码实现
  5. Vue学习(增删改查、ES6模块化概念)-学习笔记
  6. linux防火墙简介
  7. c语言二分法查找一个数_算法竞赛小专题系列(1):二分法、三分法
  8. gis投影中未定义的地理转换_ArcGIS中5分钟搞懂坐标系相关知识
  9. 360浏览器通过访问插件管理界面启用flash实例演示,360浏览器启用Adobe Flash Player方法
  10. C语言要点系统复习三:scanf读取缓冲区的那些事
  11. 搭建AutoCAD License服务器
  12. 镁光闪存颗粒对照表_海力士、南亚、镁光内存颗粒编码解析,妈妈再也不用担心你买内存条了...
  13. 内蒙古小学计算机老师招聘试题及答案,2016内蒙古事业单位计算机考试题库:计算机考试练习题(57)...
  14. 如何打造数字原生企业?易捷行云EasyStack有话要说
  15. vs code python插件_vs code Python code runner配置 , 飞跃|Fly
  16. FPGA_硬件电路(自用)
  17. 苹果id界面无法打开解决方法「iphone技巧」
  18. 了解Windows 10中的新开始菜单
  19. css:currentColor和inherit属性的区别
  20. 神思 身份证读卡器(ID)delphi7 源码

热门文章

  1. 识别图片文字怎么弄?我来教大家怎么识别图片中的文字
  2. 有了 HTTP 协议,为什么还需要 Websocket?
  3. upc Divide a Cuboid
  4. 【2022版】 Java基础面试题整理(含答案解析)
  5. 常用正则以及正则学习(整理)
  6. Python经典编程习题100例:第78例:找出年龄最大的人
  7. B站视频CC字幕提取和转换方法
  8. 2021牛客多校第八场补题 D-OR
  9. 是谁说的测试工资高的,应届毕业生,面试测试岗5k薪资都没人要.....
  10. 大数据时代,个人如何选择?