Coder Social home page Coder Social logo

openresty-best-practices's People

Contributors

agentzh avatar aifeiasdf avatar akagi201 avatar chenyehai avatar chenyehaiq avatar comdeng avatar darcysail avatar flamingtree avatar flytigerboy avatar gxlct008 avatar halfcrazy avatar idevz avatar jovntoist avatar kr1sten0 avatar lovelvye avatar membphis avatar miss-you avatar moonming avatar orezzero avatar out001a avatar plpcm avatar qinguanri avatar saighost avatar spacewander avatar wall-e avatar wenquan0hf avatar wmzy avatar xtgxiso avatar zhouzhenling avatar zqzjz0911 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openresty-best-practices's Issues

补充:ngx.exit 接口使用说明

  1. ngx.exit 是什么?有什么用?
  2. 结合案例进一步理解ngx.exit退出阶段的含义

ref:
apache/apisix#2703

another ref:
https://github.com/openresty/lua-nginx-module#ngxexit

When status >= 200 (i.e., ngx.HTTP_OK and above), it will interrupt the execution of the current request and return status code to Nginx.

When status == 0 (i.e., ngx.OK), it will only quit the current phase handler (or the content handler if the content_by_lua* directive is used) and continue to run later phases (if any) for the current request.

安装版本

将建议安装版本换成1.9.3.2。后面的例子语法全部使用的高级版本的。

360里面都用lua?

没发现很特别的东西,都是些lua代码啥的,感觉含量不高,哈哈~

关于 consocket 的描述

文中多次表述 consocket 是 “异步非阻塞”

ngx.socket 的各种操作却是同步的,又根据文档描述,ngx.socket 应该是同步非阻塞的

Adding English translation

Hi!
Thanks for your awesome repo! Please add English translation to help other than Chinese developers

mac install the openresty error

I used

./configure --prefix=/opt/openresty\
             --with-cc-opt="-I/usr/local/include"\
             --with-luajit\
             --without-http_redis2_module \
             --with-ld-opt="-L/usr/local/lib"

throw error
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option.

I try

/configure --prefix=/opt/openresty\     
             --with-cc-opt="-I/usr/local/include"\
             --with-luajit\
             --without-http_redis2_module \
             --with-ld-opt="-L/usr/local/lib"\
--with-openssl="/usr/local/Cellar/openssl"

but unworked

补充:ngx.req.get_headers的使用方法

将会补充在Openresty一栏下,新增一栏获取 http header 参数
image

目录结构:

  1. 基础使用
  2. 高性能获取header方法,使用$http_HEADER
  3. max_headers 配置
  4. ngx.req.get_headers 获取的header如何使用,比如其所有键值key均变成小写,所有的下划线都转换为破折号,以防查找失败

openresty的库函数文档下载链接失效

章节目录:Lua编辑器选择 进阶配置

由于emmylua并没有自带openresty的库函数,所以我们需要自己写函数提示,这里我提供我自己写的供你们下载和丰富.请丢到你的lualib根目录中

这里的下载链接失效

font-family in pdf files are terrible

Hi,

After received an email describing this book this afternoon I become interested in it. But after downloading the pdf file and try to read, it is a mess.

view-pdf

I know contents are most important, and people can tolerate any mistake if they really need this book. But generally, if any format error occur, readers may think that the contents of this book are also bad. So could you please rebuild it on a right way or use LaTex instead?

I really enjoy your work and wish this book could make a different.

Best regards.

liao [email protected]

location 匹配规则

根据 nginx 的 location 匹配规则,并没有 !~ 和 !*,这本书相关章节的解释容易误导 openresty 支持这两个规则,而事实上 openresty 仍然会报错 nginx: [emerg] invalid location modifier "!" in ……

《OpenResty 编程指南》专栏(书)目录

  • 安装

  • 第一个 hello world 程序

  • 优势和业界使用场景

  • 生态版图

  • 架构和同步非阻塞编程

  • NGINX 基础和阶段概念

  • Lua 基本语法

  • API 概览

  • 缓存

  • 实战:

  • 实战:用 stream 实现 memcached 服务器

  • 测试

  • 调试

  • 为什么用 LuaJIT?

  • 实战:性能优化
    写出正确的 OpenResty 代码并不困难,如何定位性能问题,如何优化 OpenResty 中的 Lua 代码,相应的示例和总结并不多见。所以有必要用一个专门的章节来由浅入深的讲解。
    我这里列一个大纲,欢迎大家补充和讨论。我会尽快完成对应的内容。

  • 以一个简单的 Lua 脚本为例

  • 如何做正确的性能测试

  • 如何发现没有被 JIT 的代码

  • 如何用火焰图捕获性能瓶颈

  • 如何优化字符串和数组操作

  • 总结:保持高性能的几个代码规范

我会给出多个优化后对比的代码和性能对比,以便大家对照实践。

建议 iResty 组织收书

这书明显是必须长期持续增补的
现在私人仓库不合适协同维护
建议及时创建 iResty 组织,
将春哥等拉入,
变成一个社区项目.

get_body_file 不能得到在上传文件的时候不能得到表单信息

在html 里面 我的表单又上传文件,又有其他值

比如:
html 代码:

<form action="http://192.168.1.197/img" enctype="multipart/form-data" method="post"> 
 <input type="file" name="htupload" size="40">  
 <input type="text" name="input" id="button" value="提交"> 
 <label><input type="submit" name="button" id="button" value="提交"></label>  
</form>

LUA代码:

function getFile(file_name)
    local f = assert(io.open(file_name, 'r'))
    local string = f:read("*all")
    f:close()
    return string
end

local method        =   ngx.var.request_method
local input_data    =   '';
if method == 'POST' then  
    ngx.req.read_body()   
    input_data = ngx.req.get_body_data()    
elseif method =='GET' then 
    input_data  =   ngx.var.args;
end

if  input_data==nil then
    local file_name = ngx.req.get_body_file()
    if file_name ~= nil then
        ngx.log(ngx.ERR,">> temp file: ", file_name)
        input_data  =   getFile(file_name);
    end
end

但是从 body_file里面获取的只有 图片信息
testPostFile.lua:29: >> input_data :------WebKitFormBoundaryhQr1xTCXDR4FwPRk^M
Content-Disposition: form-data; name="htupload"; filename="555.jpg"^M
Content-Type: image/jpeg^M ~~~~~

可以添加的新章节

openresty

table.clone
table.move
table.isarray
table.size

pgmoon 引起的内存疯涨问题

wrk 问题的追踪过程

  • stap 语言简单介绍
  • 编写 stap 脚本

require 一个 lua 模块,却返回 userdata

  • 需要看更前面的错误日志

测试套件

  • asan
  • valgrind
  • benchmark
  • wrk
  • test::nginx 入门

特性

  • privilege agent
  • capture error log
  • ngx.pipe

lj-lua-stacks.sxx 出错

我的环境: centos 7, OpenResty 1.15.8.1

[root@localhost workspace]# lj-lua-stacks.sxx --arg time=10 --skip-badvars -x 18767
Found exact match for libluajit: /home/samuelyao/openresty/luajit/lib/libluajit-5.1.so.2.1.0
semantic error: while processing function luajit_G

semantic error: unable to find member 'ptr32' for struct MRef (alternatives: ptr64): operator '->' at stapxx-cyF6xeHE/luajit.stp:162:103
source: return @cast(L, "lua_State", "/home/samuelyao/openresty/luajit/lib/libluajit-5.1.so.2.1.0")->glref->ptr32

2.1.元表中__index元方法的例子, 当访问不存在的key时会stack overflow

mytable = setmetatable({key1 = "value1"}, --原始表
  {__index = function(self, key)       --重载函数
    if key == "key2" then
      return "metatablevalue"
    else
      return self[key]
    end
  end
})

print(mytable.key1,mytable.key2)  -->value1 metatablevalue
例子没问题, 不过试了下输出`mytable.key3`的时候stack overflow

是不是改成这样比较好
if key == "key2" then
  return "metatablevalue"
end
书写的非常好, 正在看, 赞赞赞.

错别字

"Socket 编程发展"一节中,
倒数第二排应该是: "epoll 是通过内核与用户空间 mmap 同一块内存", 对吗?
"于" -> "与"

cosokect的疑问

在看《怎样理解 cosocket》一章中,看到如下解释:

用户的 Lua 脚本每触发一个网络操作,都会触发一个协程的 yield 以及 resume,因为每个请求的 Lua 脚本实际上都运行在独享协程之上,可以在任何需要的时候暂停自己(yield),也可以在任何需要的时候被唤醒(resume)。

那么我可不可以理解为:
在一个请求处理的过程中,前一个网络请求等待(暂停了自己)的时候下一个网络请求还是得等着(唤醒以后往下执行)?

补充:ngx.req.set_header的使用方法

将会补充在Openresty一栏下,新增一栏设置 http header 参数
image

目录结构:

  1. 基础使用
  2. 设置host的注意事项(包括其他特殊header注意事项)

reverse_proxy.cd 中有个链接失效了

(1) location

location 项对请求 URI 进行匹配,location 后面配置了匹配规则。例如上面的例子中,如果请求的 URI 是 localhost/,则会匹配 location / 这一项;如果请求的 URI 是 localhost/README.md,则会匹配 location /README.md 这项。

上面这个例子只是针对一个确定的 URI 做了反向代理,有的读者会有疑惑:如果对每个页面都进行这样的配置,那将会大量重复,能否做 批量 配置呢?此时需要配合使用 location 的正则匹配功能。具体实现方法可参考本书的 **URL 匹配章节**。

虚变量表述不准确

Lua 提供了一个虚变量(dummy variable),以单个下划线(“_”)来命名,用它来丢弃不需要的数值,仅仅起到占位的作用。

这个表述让人觉得_是一个特殊的变量,其实它只是一个命名惯例,和golang里的_完全不是一回事.

防止SQL注入的%s应该不需要用单引号,不确定是否还有其他情况需要考虑?

https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/safe_sql.html

db:query(string.format([[select * from cats where id = '%s']],
    ndk.set_var.set_quote_sql_str(req_id)))

跟下面的不一样,set_quote_sql_strquote_sql_str 结果都会给参数加上单引号。
https://github.com/openresty/lua-resty-mysql#sql-literal-quoting

local name = ngx.unescape_uri(ngx.var.arg_name)
local quoted_name = ngx.quote_sql_str(name)
local sql = "select * from users where name = " .. quoted_name

灰度发布功能咨询

你好!
灰度发布下的:
1、分流引擎设计
2、控制台开发
3、向运维平台发展

灰度发布方面的有计划开源写出来嘛,想了解了解。

openresty-best-practices/ngx_lua/timer.md

1.开头的连接“请求返回后继续执行”不对,连接到了timer.md本身
2.建议写明ngx.timer.at的使用环境,比如如果想nginx启动就执行,则该函数建议放到init_worker_by_lua_block中。
3.建议补充ngx.req.*的代替方法

参数校验章节等待修改

对输入参数数量判断可以使用 select('#', ...) 方法来获取,这样用户输入 nil 也就能感知到了

错别字提醒

刚开始看,看到错别字就顺手提醒一下吧:

1、openresty-best-practices/base/intro.md

 通过柔和众多设计良好的 Nginx 模块

 “柔和”(错误) =》 “糅合”(正确)

补充:urlencode介绍以及接口使用最佳实践

  1. 介绍urlencode和decode
  2. 介绍ngx.escape_uri接口使用
  3. 对ngx.escape_uri、socket.url.escape等接口进行辨析
  4. 安全案例:nginx uri配合使用ngx.escape_uri时可能会有安全风险,需要进一步进行封装safe encode操作

关于4.7节 简单API server框架 的一点问题

https://github.com/moonbingbing/openresty-best-practices/blob/master/openresty/simple_api.md

上面是文章链接。

让我疑惑的是:

$  nginx  curl '127.0.0.1:6699/api/addition?a=1'
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>openresty/1.9.3.1</center>
</body>
</html>
$  nginx  curl '127.0.0.1:6699/api/addition?a=1&b=3'
4

好像例子中并没有做参数个数的审查。也就是说。运行

venv)guo@pingdeMacBook-Air-2 04.lua_learn$ curl '127.0.0.1:6699/api/addition?a=1'
<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>openresty/1.9.3.2</center>
</body>
</html>

而这样

(venv)guo@pingdeMacBook-Air-2 04.lua_learn$ curl '127.0.0.1:6699/api/addition?a=1&b=c'
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>openresty/1.9.3.2</center>
</body>
</html>

才会400。

请问是否是我理解有问题?

如果需要修正,我可以pull request :)

使用mongodb3.0时的问题

我在使用openresty连接mongo时,遇到了权限认证问题,github上的项目都为涉及到mongodb3.0权限认证问题。
请问你们是如何实现的

pdf版本的问题

貌似pdf的有不少的空白页?有的话,可以提供最新的pdf版本么?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.