openresty 的小白记录
openresty提供了一个快速访问数据库,快速响应的功能。基于lua + redis,我们可以做到快速响应,达到10k级连接的处理能力。
openresty 的小白记录
好的学习资料,从lua到openresty
https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/simple_api.html
基础类型
布尔类型,可选值 true/false;Lua 中 nil 和 false 为“假”,其它所有值均为“真”。比如 0 和空字符串就是“真”;
在 Lua 中,函数 也是一种数据类型,函数可以存储在变量中,可以通过参数传递给其他函数,还可以作为其他函数的返回值。
Table 类型实现了一种抽象的“关联数组”。“关联数组”是一种具有特殊索引方式的数组,索引通常是字符串(string)或者 number 类型,但也可以是除 nil 以外的任意类型的值。
函数的参数
lua函数的参数大部分是按值传递的
当传值是table是,则是引用关系,除此之外,都是按值传递参数
允许多个返回值,有点像scala的元组
Lua UnPack函数用法实例,unpack它接受一个数组(table)作为参数,并默认从下标1开始返回数组的所有元素
点号与冒号操作符的区别
https://moonbingbing.gitbooks.io/openresty-best-practices/content/lua/dot_diff.html
冒号操作会带入一个 self 参数,用来代表 自己。而点号操作,只是 内容 的展开。
在函数定义时,使用冒号将默认接收一个 self 参数,而使用点号则需要显式传入 self 参数。
nginx相关语法
https://www.hi-linux.com/posts/4336.html
access_by_lua_file lua/access_check.lua; 校验参数
content_by_lua_file lua/$1.lua; 执行lua脚本并返回content
rewrite_by_lua_file 在access阶段前运行,主要用于rewrite重定向
ngx.exit(ngx.HTTP_BAD_REQUEST)
ngx.say(args.a * args.b)
ngx.log(ngx.ERR, "failed to log message: ", err)
ngx.log(ngx.ERR, "num:", num)
ngx.log(ngx.INFO, " string:", str)
local args = ngx.req.get_uri_args()
ngx.say(args.a * args.b)
ngx.var.arg_a
ngx.location.capture 子请求
lua_code_cache 默认打开
local fp = require("my")
内置绑定变量
https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/inline_var.html
共享变量
ngx.ctx 表就是为了解决这类问题而设计的
每个请求,包括子请求,都有一份自己的 ngx.ctx 表,相互隔离,子请求和其他请求间也是隔离的
防止sql注入
https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/safe_sql.html
利用 cosocket 发起子请求
https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/how_request_http.html
share内存
多个worker共享的内存
lua_shared_dict
lua_shared_dict logs 100m;
local domaindb = ngx.shared.domaindb
pcall
protect call
demo
start
cd ~/work
mkdir conf
mkdir lua
mkdir logs
nginx -p `pwd`/ -c conf/nginx.conf
nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
lua_code_cache off;
lua_shared_dict datas 100m;
server {
listen 8080;
server_name opentest2.com;
location / {
default_type text/html;
set $yoxi DSX:$host;
content_by_lua_file lua/index.lua;
}
}
server {
listen 8080;
server_name opentest.com;
location /test {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
-- 请注意这里 auth 的调用过程
local count
count, err = red:get_reused_times()
if 0 == count then
elseif err then
ngx.say("failed to get reused times: ", err)
return
end
ok, err = red:set("dog", "an animal")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ress = red:get("dog");
-- ngx.say("set result: ", ress)
ngx.redirect("http://opentest2.com:8080/index?from8082test")
-- 连接池大小是100个,并且设置最大的空闲时间是 10 秒
local ok, err = red:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
}
}
}
}
index.lua
local datas = ngx.shared.datas
local d_data = datas:get('a')
ngx.log(ngx.ERR,d_data)
datas:set('a','b',10)
if d_data == nil then
d_data = 'a';
ngx.log(ngx.ERR,'a is not exitts')
datas:set('a', 'a', 10)
end
local arg = ngx.req.get_uri_args();
for k,v in pairs(arg) do
ngx.say(datas:get('a'),'----dsxhost:----',ngx.var.yoxi,'\r\n')
ngx.print();
ngx.say("====" .. ngx.var.host .. ngx.var.uri .. "?".. ngx.var.args .. "====")
ngx.say("[GET23 ] key:", k, " v:", v)
end
http模块上设置
lua_code_cache on //线上必须打开,否则性能下降明显
lua_share_dict local_cahce 100m //nginx本地缓存
lua_package_path "" //lua代码包路径
upstream foo.example.com {
server 127.0.0.1:8001 weight=5 max_fails=3 fail_timeout=10s;
server 127.0.0.1:8002 weight=10;
server 10.0.0.11:7070 backup; //其他全部的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
ip_hash;
}
server模块
location / {
content_by_lua_file{
local redis = require "resty.redis"
local red = redis:new()
red:setTimeout(1000)
local ok, err= red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ",err);
return;
local flag = red:get(ngx.var.host)
red:set_keepalive(10000,100)
if(flag == nil) then
ngx.redirect("/error.aspx")
return
ngx.ctx.result_domain = "shop.xx.com"
}
proxy_pass http://ngx.var.result;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
ngx.null is nil
流程
init
rewrite
access
content
ngx.var需要预定义如
location /foo {
set $a 12;
set $b '';
rewrite_by_lua_block {
ngx.var.b = tonumber(ngx.var.a) + 12;
}
ngx.ctx作用于单个模块上下文
content_by_lua_block {
ngx.ctx.result = ngx.ctx.foo
ngx.say(ngx.ctx.result)
}
ngx.var 是获取 Nginx 的变量,需要经历字符串 hash、hash 表查找等过程
ngx.ctx 仅仅是一个 Lua table 而已,它的引用存放在 ngx_lua 的模块上下文(ctx_ref)
使用 ngx.ctx 比 ngx.var 往往是更好的选择
nginx模块含义
http:全局配置
server:虚拟主机
location:是用来为匹配的 URI 进行配置,即php文件如何处理,html文件如何处理...
OSI七层协议是什么
应用层 HTTP FTP
表示层 JPEG ASCll
会话层
传输层 TCP UDP PORT
网络层 IP
数据链路层
物理层
nginx 4层
IP+端口级别,在OSI4层工作,不理解协议,实现流量的负载均衡
nginx 7层
http url级别应用层,除了支持4层以外,还有应用层的信息,如url,更加智能,如缓存图片,html
local count
count, err = red:get_reused_times()
if 0 == count then
ok, err = red:auth('xx')
if not ok then
ngx.say("failed to auth: ", err)
return
end
elseif err then
ngx.say("failed to get reused times: ", err)
return
end
openresty 的小白记录的更多相关文章
- python小白记录一 ——python脚本生成windows可执行exe
1.需要安装pywin32 先查看自己有没有安装:使用如下命令查看 pip show pywin32 如果没有则用下面方式进行安装: pip install pywin32 然后等待安装完成: 2.再 ...
- python学习1(小白记录)
python创建cocos2d-x项目注意点1. 2.7.5版本号的.配置好环境变量之后.要切换到tools文件夹下.直接运行 python create_project.py ..........这 ...
- python小白记录三——pycharm+selenium搭建环境之 no module named 'selenium'异常解决
在pycharm上搭建python+selenium自动化测试环境时,遇到一个很坑的问题:no moduel named 'selenium' 如下图: 1.查看你的python是否正确安装了sele ...
- python小白记录二 ——自动化测试selenium中配置浏览器
1.根据不同的浏览器 下载不同的驱动,下面是谷歌的驱动 下载地址:ChromeDriver - WebDriver for Chrome - Downloads (chromium.org) ...
- openresty记录响应body乱码问题
问题背景 最近新上了一个功能,openresty通过syslog记录请求日志,然后由logstash推送至ES.测试上线时未发现这个问题,在日常查看日志的过程中,发现logstash推送有错误日志,错 ...
- Openresty最佳案例 | 第3篇:Openresty的安装
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616645 本文出自方志朋的博客 我的服务器为一台全新的centos 7的服务器,所以从 ...
- react+redux官方实例TODO从最简单的入门(1)-- 前言
刚进公司的时候,一点react不会,有一个需求要改,重构页面!!!完全懵逼,一点不知道怎么办!然后就去官方文档,花了一周时间,就纯react实现了页面重构,总体来说,react还是比较简单的,由于当初 ...
- CentOS6.3连接Xshell出现的问题(连接失败--需要设置ONBOOT=“yes”,开启网卡驱动)
小白记录: 安装Xshell之后连接CentOS6.3的baseService版本,连接不上, service network restart 只有两个OK, 百度查找资料后--得到结论:网卡设置之 ...
- Java 环境配置 与 碰到的问题
小白记录,希望各位指点,长期整理修改 不定期更新,碰到的与之相关的会添加,做个小笔记,再次碰到可以更好的解决. JDK 下载:Oracle 配置方法:菜鸟教程 - Java 开发环境配置 作用 JAV ...
随机推荐
- IntelliJ IDEA编辑器激活码
2020-3-31 日 亲自测试有效,什么时候失效就母鸡了 激活码一: T3ACKYHDVF-eyJsaWNlbnNlSWQiOiJUM0FDS1lIRFZGIiwibGljZW5zZWVOYW1lI ...
- 关于取整函数ceil(),floor(),round()函数得应用
ceil()返回向上取整最接近的整数. double ceil(double); floor()返回向下取整最接近的整数. double floor(double); round()用于对浮点数的四舍 ...
- Java基础一篇过(二)泛型
一.啥是泛型 概述 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,即所操作的数据类型被指定为一个参数. 格式 类名<类型名> 标记符 E - Element (在集合中使用 ...
- 阿里云恶意软件检测比赛-第三周-TextCNN
LSTM初试遇到障碍,使用较熟悉的TextCNN. 1.基础知识: Embedding:将词的十进制表示做向量化 起到降维增维的作用 嵌入维度数量(New Embedding维度)的一般经验法则: e ...
- PHP代码审计学习(1)
全局变量与超全局变量 $GLOBALS $GLOBALS 是PHP的一个超级全局变量组,在一个PHP脚本的全部作用域中都可以访问,$GLOBALS 是一个包含了全部变量的全局组合数组.变量的名字就是数 ...
- Centos-归档文件或目录-tar
tar 对文件或者目录进行打包归档成一个文件,不是压缩 相关选项 -c 新建文件 -r 将目标文件追加都档案文件末尾 -t 列出归档文件中已经归档文件列表 -x 从归档文件中还原文件 -u 新文件更新 ...
- SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了
SpringBoot整合SpringDataJPA 1.JPA概念 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映 ...
- Python3基础——字符串类型
Text Sequence Type - str(immutable) class str(object='') class str(object=b'', encoding='utf-8', err ...
- Python基础-列表、元组、字典、字符串(精简解析)
一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...
- Vue.js 学习笔记之四:Vue 组件基础
到目前为止,这个系列的笔记所展示的都是一些极为简单的单页面 Web 应用程序,并且页面上通常只有几个简单的交互元素.但在实际生产环境中,Web 应用程序的用户界面往往是由多个复杂的页面共同组成的.这时 ...