Skip to content

Commit 4d28495

Browse files
committed
update README Format
1 parent 3f33a60 commit 4d28495

File tree

1 file changed

+143
-137
lines changed

1 file changed

+143
-137
lines changed

README.md

Lines changed: 143 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
+ [Lua HTTP状态常量](#Openresty_http_status_constants)
3737
+ [ngx Lua APi 介绍使用](#Openresty_ngx_api_used)
3838
+ [连接数据库](#Openresty_connent_redis)
39+
+ [OpenResty缓存](#Openresty_connent_cache)
3940
+ [luajit 执行文件默认安装路径](#Nginx_base_knowledge)
4041
+ [Redis执行Lua脚本基本用法](#Redis_Run_Lua)
4142
+ [Ngx_lua 写入Redis数据,通过CURL请求](#Ngx_lua_write_Redis)
@@ -438,154 +439,154 @@
438439
+ [Lua 脚本](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-redis/Hls-Line-Number-Total.lua)
439440
440441
#### <a name="Openresty_resty-websocket"/> lua-resty-websocket 扩展
441-
+ 代码引入:`lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";`
442-
+ **Lua脚本实现一个websocket连接(测试成功,可上线)**
443-
+ nginx.conf 配置信息
444-
```Lua
445-
http {
446-
lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";
447-
server {
448-
listen 80 so_keepalive=2s:2s:8; #为了防止半开TCP连接,最好在Nginx监听配置指令中启用TCP keepalive:
449-
server_name localhost;
450-
location /ws {
451-
lua_socket_log_errors off;
452-
lua_check_client_abort on;
453-
lua_code_cache off; # 建议测试的时候最好关闭缓存
454-
content_by_lua_file /opt/openresty/nginx/conf/Lua/websocket.lua;
455-
}
442+
+ 代码引入:`lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";`
443+
+ **Lua脚本实现一个websocket连接(测试成功,可上线)**
444+
+ nginx.conf 配置信息
445+
```Lua
446+
http {
447+
lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";
448+
server {
449+
listen 80 so_keepalive=2s:2s:8; #为了防止半开TCP连接,最好在Nginx监听配置指令中启用TCP keepalive:
450+
server_name localhost;
451+
location /ws {
452+
lua_socket_log_errors off;
453+
lua_check_client_abort on;
454+
lua_code_cache off; # 建议测试的时候最好关闭缓存
455+
content_by_lua_file /opt/openresty/nginx/conf/Lua/websocket.lua;
456456
}
457-
}
458-
```
459-
+ [WebSockets服务器Lua脚本websocket.lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-websocket/websocket.lua)
460-
+ [websockets.html客户端代码,代码路径:/usr/local/openresty/nginx/html](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-websocket/websocket.html)
461-
+ 然后打开启用了WebSocket支持的浏览器,然后打开以下url:
462-
![websockt-lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Images/websocket_lua01.png)
463-
#### <a name="Openresty_resty-cjson"/> lua-cjson 扩展
464-
+ 基本用法
465-
+ nginx.conf
466-
```Lua
467-
location /cjson {
468-
content_by_lua_block {
469-
local cjson = require "cjson"
470-
471-
local json = cjson.encode({
472-
foo = "bar",
473-
some_object = {},
474-
some_array = cjson.empty_array
475-
})
476-
ngx.say(json)
477-
}
478457
}
479-
```
480-
+ curl 请求
481-
```Bash
482-
root@tinywan:/opt/openresty/nginx/conf# curl http://127.0.0.1/cjson
483-
{"some_object":{"tel":13669313112,"age":24},"name":"tinywan","some_array":[]}
484-
```
485-
+ [lua对象到字符串、字符串到lua对象](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-cjson/cjson-str-obj.lua)
486-
#### <a name="Openresty_resty-session"/> lua-resty-session 扩展
487-
+ OpenResty 引用第三方 resty 库非常简单,只需要将相应的文件拷贝到 resty 目录下即可
488-
+ 我服务器OpenResty 的 resty 路径:`/opt/openresty/lualib/resty`
489-
+ 下载第三方 resty 库:git clone lua-resty-session 文件路径以及内容:
490-
```Bash
491-
tinywan@tinywan:/opt/openresty/nginx/lua/lua-resty-session/lib/resty$ ls
492-
session session.lua
493-
```
494-
+ 特别注意:这里拷贝的时候要要把session文件和session.lua 文件同时吧、拷贝过去,否则会报错误:
495-
```Bash
496-
/opt/openresty/lualib/resty/session.lua:34: in function 'prequire'
497-
/opt/openresty/lualib/resty/session.lua:211: in function 'new'
498-
/opt/openresty/lualib/resty/session.lua:257: in function 'open'
499-
/opt/openresty/lualib/resty/session.lua:320: in function 'start'
500-
```
501-
+ 拷贝完毕后`/opt/openresty/lualib/resty` OpenResty 引用第三方 resty 的所有库文件
502-
```Bash
503-
tinywan@tinywan:/opt/openresty/lualib/resty$ ls
504-
aes.lua core.lua http_headers.lua lock.lua lrucache.lua memcached.lua random.lua session sha1.lua sha256.lua sha512.lua string.lua upstream
505-
core dns http.lua lrucache md5.lua mysql.lua redis.lua session.lua sha224.lua sha384.lua sha.lua upload.lua websocket
506-
```
507-
+ 基本用法
508-
```Lua
509-
location /start {
510-
content_by_lua_block {
511-
local session = require "resty.session".start()
512-
session.data.name = "OpenResty Fan Tinywan"
513-
session:save()
514-
ngx.say("<html><body>Session started. ",
515-
"<a href=/test>Check if it is working</a>!</body></html>")
516-
ngx.say(session.data.name,"Anonymous")
517-
}
458+
}
459+
```
460+
+ [WebSockets服务器Lua脚本websocket.lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-websocket/websocket.lua)
461+
+ [websockets.html客户端代码,代码路径:/usr/local/openresty/nginx/html](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-websocket/websocket.html)
462+
+ 然后打开启用了WebSocket支持的浏览器,然后打开以下url:
463+
![websockt-lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Images/websocket_lua01.png)
464+
#### <a name="Openresty_resty-cjson"/> lua-cjson 扩展
465+
+ 基本用法
466+
+ nginx.conf
467+
```Lua
468+
location /cjson {
469+
content_by_lua_block {
470+
local cjson = require "cjson"
471+
472+
local json = cjson.encode({
473+
foo = "bar",
474+
some_object = {},
475+
some_array = cjson.empty_array
476+
})
477+
ngx.say(json)
478+
}
518479
}
519-
```
480+
```
520481
+ curl 请求
521482
```Bash
522-
tinywan@tinywan:/opt/openresty/nginx/conf$ curl http://192.168.18.143/start
523-
<html><body>Session started. <a href=/test>Check if it is working</a>!</body></html>
524-
OpenResty Fan Tinywan Anonymous
525-
```
483+
root@tinywan:/opt/openresty/nginx/conf# curl http://127.0.0.1/cjson
484+
{"some_object":{"tel":13669313112,"age":24},"name":"tinywan","some_array":[]}
485+
```
486+
+ [lua对象到字符串、字符串到lua对象](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-cjson/cjson-str-obj.lua)
487+
#### <a name="Openresty_resty-session"/> lua-resty-session 扩展
488+
+ OpenResty 引用第三方 resty 库非常简单,只需要将相应的文件拷贝到 resty 目录下即可
489+
+ 我服务器OpenResty 的 resty 路径:`/opt/openresty/lualib/resty`
490+
+ 下载第三方 resty 库:git clone lua-resty-session 文件路径以及内容:
491+
```Bash
492+
tinywan@tinywan:/opt/openresty/nginx/lua/lua-resty-session/lib/resty$ ls
493+
session session.lua
494+
```
495+
+ 特别注意:这里拷贝的时候要要把session文件和session.lua 文件同时吧、拷贝过去,否则会报错误:
496+
```Bash
497+
/opt/openresty/lualib/resty/session.lua:34: in function 'prequire'
498+
/opt/openresty/lualib/resty/session.lua:211: in function 'new'
499+
/opt/openresty/lualib/resty/session.lua:257: in function 'open'
500+
/opt/openresty/lualib/resty/session.lua:320: in function 'start'
501+
```
502+
+ 拷贝完毕后`/opt/openresty/lualib/resty` OpenResty 引用第三方 resty 的所有库文件
503+
```Bash
504+
tinywan@tinywan:/opt/openresty/lualib/resty$ ls
505+
aes.lua core.lua http_headers.lua lock.lua lrucache.lua memcached.lua random.lua session sha1.lua sha256.lua sha512.lua string.lua upstream
506+
core dns http.lua lrucache md5.lua mysql.lua redis.lua session.lua sha224.lua sha384.lua sha.lua upload.lua websocket
507+
```
508+
+ 基本用法
509+
```Lua
510+
location /start {
511+
content_by_lua_block {
512+
local session = require "resty.session".start()
513+
session.data.name = "OpenResty Fan Tinywan"
514+
session:save()
515+
ngx.say("<html><body>Session started. ",
516+
"<a href=/test>Check if it is working</a>!</body></html>")
517+
ngx.say(session.data.name,"Anonymous")
518+
}
519+
}
520+
```
521+
+ curl 请求
522+
```Bash
523+
tinywan@tinywan:/opt/openresty/nginx/conf$ curl http://192.168.18.143/start
524+
<html><body>Session started. <a href=/test>Check if it is working</a>!</body></html>
525+
OpenResty Fan Tinywan Anonymous
526+
```
526527
#### <a name="Openresty_ngx_api_auth"/> Lua 权限验证
527-
+ Lua 一个HLS的简单地址访问权限验证
528-
+ Nginx.conf 配置
529-
```Lua
530-
location ^~ /live/ {
531-
add_header Cache-Control no-cache;
532-
add_header 'Access-Control-Allow-Origin' '*' always;
533-
add_header 'Access-Control-Allow-Credentials' 'true';
534-
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
535-
add_header 'Access-Control-Allow-Headers' 'Range';
536-
537-
types{
538-
application/dash+xml mpd;
539-
application/vnd.apple.mpegurl m3u8;
540-
video/mp2t ts;
541-
}
542-
if ( $uri ~ \.m3u8 ) {
543-
lua_code_cache off;
544-
access_by_lua_file /opt/openresty/nginx/lua/access.lua;
545-
}
546-
root /home/tinywan/HLS;
547-
}
548-
```
549-
+ access.lua 文件内容
550-
```Lua
551-
if ngx.req.get_uri_args()["wsSecret"] ~= "e65e6a01cf26523e206d5bb0e2a8a95a" then
552-
return ngx.exit(403)
553-
end
554-
```
528+
+ Lua 一个HLS的简单地址访问权限验证
529+
+ Nginx.conf 配置
530+
```Lua
531+
location ^~ /live/ {
532+
add_header Cache-Control no-cache;
533+
add_header 'Access-Control-Allow-Origin' '*' always;
534+
add_header 'Access-Control-Allow-Credentials' 'true';
535+
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
536+
add_header 'Access-Control-Allow-Headers' 'Range';
537+
538+
types{
539+
application/dash+xml mpd;
540+
application/vnd.apple.mpegurl m3u8;
541+
video/mp2t ts;
542+
}
543+
if ( $uri ~ \.m3u8 ) {
544+
lua_code_cache off;
545+
access_by_lua_file /opt/openresty/nginx/lua/access.lua;
546+
}
547+
root /home/tinywan/HLS;
548+
}
549+
```
550+
+ access.lua 文件内容
551+
```Lua
552+
if ngx.req.get_uri_args()["wsSecret"] ~= "e65e6a01cf26523e206d5bb0e2a8a95a" then
553+
return ngx.exit(403)
554+
end
555+
```
555556
#### <a name="Openresty_resty-string"/> lua-resty-string 扩展
556-
+ MD5加密的简单基本用法 md5.lua
557-
```Lua
558-
local resty_md5 = require "resty.md5"
559-
local md5 = resty_md5:new()
560-
if not md5 then
561-
ngx.say("failed to create md5 object")
562-
return
563-
end
564-
local ok = md5:update("hello")
565-
if not ok then
566-
ngx.say("failed to add data")
567-
return
568-
end
569-
local digest = md5:final()
570-
-- ngx.say("md5",digest) ---注意:这样直接输出是乱码
571-
local str = require "resty.string"
572-
ngx.say("md5: ", str.to_hex(digest)) ---注意:必须通过字符串转码方可打印输出
573-
-- yield "md5: 5d41402abc4b2a76b9719d911017c592"
574-
```
557+
+ MD5加密的简单基本用法 md5.lua
558+
```Lua
559+
local resty_md5 = require "resty.md5"
560+
local md5 = resty_md5:new()
561+
if not md5 then
562+
ngx.say("failed to create md5 object")
563+
return
564+
end
565+
local ok = md5:update("hello")
566+
if not ok then
567+
ngx.say("failed to add data")
568+
return
569+
end
570+
local digest = md5:final()
571+
-- ngx.say("md5",digest) ---注意:这样直接输出是乱码
572+
local str = require "resty.string"
573+
ngx.say("md5: ", str.to_hex(digest)) ---注意:必须通过字符串转码方可打印输出
574+
-- yield "md5: 5d41402abc4b2a76b9719d911017c592"
575+
```
575576
#### <a name="Openresty_resty-http"/> lua-resty-http 扩展 (ngx_lua的HTTP客户端cosocket驱动程序)
576-
+ [简单测试:lua-http-test.lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-http/lua-http-test.lua)
577+
+ [简单测试:lua-http-test.lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-http/lua-http-test.lua)
577578
#### <a name="Openresty_resty-mysql"/> lua-resty-mysql 扩展
578-
+ [简单测试:lua-msyql-test.lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-mysql/lua-msyql-test.lua)
579+
+ [简单测试:lua-msyql-test.lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-mysql/lua-msyql-test.lua)
579580
#### <a name="Openresty_resty-srcache"/> srcache-nginx-module 扩展 ([nginx下的一个缓存模块](https://github.com/openresty/srcache-nginx-module))
580-
+ [openresty–redis–srcache缓存的应用](http://www.xtgxiso.com/openresty-redis-srcache-nginx-module%e7%bc%93%e5%ad%98%e7%9a%84%e5%ba%94%e7%94%a8/)
581+
+ [openresty–redis–srcache缓存的应用](http://www.xtgxiso.com/openresty-redis-srcache-nginx-module%e7%bc%93%e5%ad%98%e7%9a%84%e5%ba%94%e7%94%a8/)
581582
#### <a name="Openresty_ngx_adddd"/> openresty扫描代码全局变量
582-
+ 在OpenResty中需要避免全局变量的使用,为此春哥写了一个perl工具,可以扫描openresty lua代码的全局变量
583-
+ [https://github.com/openresty/openresty-devel-utils/blob/master/lua-releng](https://github.com/openresty/openresty-devel-utils/blob/master/lua-releng)
584-
+ 用法相当简单
585-
1. 将代码保存成lua-releng文件
586-
2. 更改lua-releng的权限,chmod 777 lua-releng
587-
3. 假设有一个源码文件为test.lua
588-
4. 执行./lua-releng test.lua,则会扫描test.lua文件的全局变量,并在屏幕打印结果
583+
+ 在OpenResty中需要避免全局变量的使用,为此春哥写了一个perl工具,可以扫描openresty lua代码的全局变量
584+
+ [https://github.com/openresty/openresty-devel-utils/blob/master/lua-releng](https://github.com/openresty/openresty-devel-utils/blob/master/lua-releng)
585+
+ 用法相当简单
586+
1. 将代码保存成lua-releng文件
587+
2. 更改lua-releng的权限,chmod 777 lua-releng
588+
3. 假设有一个源码文件为test.lua
589+
4. 执行./lua-releng test.lua,则会扫描test.lua文件的全局变量,并在屏幕打印结果
589590
#### <a name="Openresty_http_status_constants"/> Lua HTTP状态常量
590591
+ 所有常量列表
591592
```Lua
@@ -684,6 +685,11 @@
684685
{"user_agent":"curl\/7.47.0","info":"[email protected]","client_id":"127.0.0.1"}
685686

686687
```
688+
#### <a name="Openresty_connent_cache"/> OpenResty缓存
689+
+ 指令:`lua_shared_dict`
690+
+ 纯内存的操作,多个worker之间共享的(比如nginx开启10个Worker,则每个worker之间是共享该内存的)
691+
+ 同一份数据在多个worker之间是共享的,只要存储一份数据就可以了
692+
+ 锁的竞争(数据原子性)
687693
### Redis、Lua、Nginx一起工作事迹
688694
+ 解决一个set_by_lua $sum 命令受上下文限制的解决思路,已完美解决
689695
+ - [x] [API disabled in the context of set_by_lua](https://github.com/openresty/lua-nginx-module/issues/275)

0 commit comments

Comments
 (0)