OpenResty Best Practices

OpenResty 最佳实践

我们提供 OpenResty、Apache APISIX 以及 API 网关方面相关的咨询、培训、性能优化、定制开发等商业支持服务,欢迎联系。 微信:ming69371, 邮箱:wenming#apache.org


在 2012 年的时候,加入到奇虎 360 公司,为新的产品做技术选型。由于之前一直混迹在 Python 圈子里面,也接触过 Nginx C 模块的高性能开发,一直想找到一个兼备 Python 快速开发和 Nginx C 模块高性能的产品。看到 OpenResty 后,有发现新大陆的感觉。

于是在新产品里面力推 OpenResty,团队里面几乎没人支持,经过几轮性能测试,虽然轻松击败所有的其他方案,但是其他开发人员并不愿意参与到基于 OpenResty 这个“陌生”框架的开发中来。于是我开始了一个人的 OpenResty 之旅,刚开始经历了各种技术挑战,庆幸有详细的文档,以及春哥和邮件列表里面热情的帮助,成了团队里面 bug 最少和几乎不用加班的同学。

2014 年,团队进来了一批新鲜血液,很有技术品味,先后选择 OpenResty 来作为技术方向。不再是一个人在战斗,而另外一个新问题摆在团队面前,如何保证大家都能写出高质量的代码,都能对 OpenResty 有深入的了解?知识的沉淀和升华,成为一个迫在眉睫的问题。

我们选择把这几年的一些浅薄甚至可能是错误的实践,通过 gitbook 的方式公开出来,一方面有利于团队自身的技术积累,另一方面,也能让更多的高手一起加入,让 OpenResty 的使用变得更加简单,更多的应用到服务端开发中,毕竟人生苦短,少一些加班,多一些陪家人。

这本书的定位是最佳实践,同时会对 OpenResty 做简单的基础介绍。但是我们对初学者的建议是,在看书的同时下载并安装 OpenResty,把官方网站的 Presentations 浏览和实践几遍。

一直 使用最新的 OpenResty 版本来运行本书的代码。

希望你能 enjoy OpenResty 之旅!


在 gitbook 上查看本书

本书源码在 GitHub 上维护,欢迎参与:我要写书。也可以加入 QQ 群来和我们交流:

  • 34782325(技术交流 ①群 已满)
  • 481213820(技术交流 ②群 已满)
  • 124613000(技术交流 ③群 已满)
  • 679145170(技术交流 ④群)

作者极客时间专栏:《OpenResty 从入门到实战》

Comments
  • mac install the openresty error

    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

  • 内容修复 & 修复问题#122

    内容修复 & 修复问题#122

    phase.md 文件中, 应该是在介绍 *_by_lua_block{} 这种用法。 例子中依然使用了原来的函数,现修改为:

    location /mixed {
        set_by_lua_block $a {
            ngx.log(ngx.ERR, "set_by_lua")
        }
        rewrite_by_lua_block {
            ngx.log(ngx.ERR, "rewrite_by_lua")
        }
    }
    
  • cosokect的疑问

    cosokect的疑问

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

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

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

  • reverse_proxy.cd 中有个链接失效了

    reverse_proxy.cd 中有个链接失效了

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

    location 匹配规则

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

  • lj-lua-stacks.sxx 出错

    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

  • Gitbook 上版本比较老,是否可以同步github 上的版本?

    Gitbook 上版本比较老,是否可以同步github 上的版本?

    Gitbook 上的版本比较老, 缺少了一些章节 ,比如 vscode 的使用. 有些章节还有一些错误: image 它在github 上已经修复了: https://github.com/moonbingbing/openresty-best-practices/blame/master/lua/operator.md#L81

    期望能更新下Gitbook 上的版本. 或者提供一种本地打开这个项目的最佳实践.

  • 补充:Lua 进阶知识,单个lua文件内local变量不超过200个

    补充:Lua 进阶知识,单个lua文件内local变量不超过200个

    单个lua文件内local变量不超过200个

    代码:

    local s = ""
    for i = 1, 1000 do
        s = s.."\nlocal i" .. i .. " = 1"
    end
    local a = "local function x()\n" .. s .. "\nend"
    print(loadstring(a))
    

    运行报错 has more than 200 local variables

  • 有个疑问关于日志记录的-logger

    有个疑问关于日志记录的-logger

    文档里面这里说的:

    • 由于在 content_by_lua* 阶段变量的生命周期会随着请求的终结而终结,所以当日志量小于 flush_limit 的情况下这些日志就不能被累积,也不会触发 _flush_buffer 函数,所以小日志会丢失。
    • 这些坑回头看来这么明显,所有的问题都是因为我们把 lua/log.lua 用错阶段了,应该放到 log_by_lua* 阶段,所有的问题都不复存在。

    虽然小于flush_limit的内容不会flush,但是会放到 模块变量log_buffer_data这个数组中

    
    local function _write_buffer(msg, len)
        log_buffer_index = log_buffer_index + 1
        log_buffer_data[log_buffer_index] = msg
    
        buffer_size = buffer_size + len
    
    
        return buffer_size
    end
    
    

    我的两个疑问:

    1. 既然模块变量会缓存,那么下次就会把之前累加的内容一起给发送了,为什么你这里写的不会累加?
    2. 而且就算放到log_by_lua阶段,小于flush_limit的内容,也只会写入buffer, 不会flush刷到磁盘; 所以我认为 第二句话(放到log_by_lua阶段就没问题了)不太对,还望帮忙解答这个疑问
  • 变量作用范围里关于timer的一处错误

    变量作用范围里关于timer的一处错误

    https://github.com/moonbingbing/openresty-best-practices/blob/master/ngx_lua/lua-variable-scope.md

    在这一节里,提到:

    init_worker_by_lua_block {
        local delay = 5
        local handler
        handler = function()
            counter = counter or 0
            counter = counter + 1
            ngx.log(ngx.ERR, counter)
            local ok, err = ngx.timer.at(delay, handler)
            if not ok then
                ngx.log(ngx.ERR, "failed to create the timer: ", err)
                return
            end
        end
        local ok, err = ngx.timer.at(delay, handler)
        if not ok then
            ngx.log(ngx.ERR, "failed to create the timer: ", err)
            return
        end
    }
    

    每次 counter 的输出都是 1。

    然后实际的情况是,确实每次 timer 执行都在累加,而并没有重新定义。应该需要更新一下文档。

    @membphis

Easily kick-start your python project with very opinionated best practices.

Pyproject Easily kickstart your Python project with very opionionated best practices. Manage your project using poetry https://python-poetry.org/ Add

Jan 24, 2022
Go programming language secure coding practices guide

You can download this book in the following formats: PDF, Mobi and ePub. Introduction Go Language - Web Application Secure Coding Practices is a guide

Jan 9, 2023
Docker Swarm Ingress service based on OpenResty with automatic Let's Encrypt SSL provisioning

Ingress Service for Docker Swarm Swarm Ingress OpenResty is a ingress service for Docker in Swarm mode that makes deploying microservices easy. It con

Jun 23, 2022
Microservice framework following best cloud practices with a focus on productivity.

patron Patron is a framework for creating microservices, originally created by Sotiris Mantzaris (https://github.com/mantzas). This fork is maintained

Dec 22, 2022
Logur is an opinionated collection of logging best practices
Logur is an opinionated collection of logging best practices

Logur is an opinionated collection of logging best practices. Table of Contents Preface Features Installation Usage FAQ Why not just X logger? Why not

Dec 30, 2022
Gin best practices, gin development scaffolding, too late to explain, get on the bus.

Table of Contents generated with DocToc gin_scaffold 现在开始 文件分层 log / redis / mysql / http.client 常用方法 swagger文档生成 gin_scaffold Gin best practices, gin

Dec 27, 2022
Validation of best practices in your Kubernetes clusters
Validation of best practices in your Kubernetes clusters

Best Practices for Kubernetes Workload Configuration Fairwinds' Polaris keeps your clusters sailing smoothly. It runs a variety of checks to ensure th

Jan 9, 2023
A best practices checker for Kubernetes clusters. 🤠

Clusterlint As clusters scale and become increasingly difficult to maintain, clusterlint helps operators conform to Kubernetes best practices around r

Dec 29, 2022
Music recognition bot for Reddit powered by audd.io. Note that the code currently needs some cleaning up and doesn't follow the best practices.

Music recognition bot for Reddit u/auddbot identifies music on Reddit. When someone mentions it or writes a question like "what's the song", it sends

Dec 30, 2022
Go Server/API boilerplate using best practices DDD CQRS ES gRPC
Go Server/API boilerplate using best practices DDD CQRS ES gRPC

Go Server/API boilerplate using best practices DDD CQRS ES gRPC

Jan 6, 2023
A cookbook with the best practices to working with kubernetes.
A cookbook with the best practices to working with kubernetes.

A cookbook with the best practices to working with kubernetes.

Dec 27, 2022
A best practices Go source project with unit-test and integration test, also use skaffold & helm to automate CI & CD at local to optimize development cycle

Dependencies Docker Go 1.17 MySQL 8.0.25 Bootstrap Run chmod +x start.sh if start.sh script does not have privileged to run Run ./start.sh --bootstrap

Apr 4, 2022
This plugin will analyse the JFrog Platform instance and provide the non conformance against the best practices based on the predefines rules.

hello-frog About this plugin This plugin is a template and a functioning example for a basic JFrog CLI plugin. This README shows the expected structur

Nov 30, 2021
Golang service boilerplate using best practices

go-boilerplate Golang service boilerplate using best practices. Responsibility: Register (CRUD) and Login Users with JWT. Dependencies Gin-Gonic Swagg

May 11, 2022
A guide to smart contract security best practices

Smart Contract Security Best Practices Visit the documentation site: https://consensys.github.io/smart-contract-best-practices/ Read the docs in Chine

Dec 27, 2022
Easily kick-start your python project with very opinionated best practices.

Pyproject Easily kickstart your Python project with very opionionated best practices. Manage your project using poetry https://python-poetry.org/ Add

Jan 24, 2022
AI-Powered Code Reviews for Best Practices & Security Issues Across Languages
AI-Powered Code Reviews for Best Practices & Security Issues Across Languages

AI-CodeWise ?? AI-Powered Code Reviews for Best Practices & Security Issues Across Languages AI-CodeWise GitHub Action: Your AI-powered Code Reviewer!

May 11, 2023
Search and store the best cryptos for the best scalable and modern application development.

Invst Hunt Search and store the best cryptos for the best scalable and modern application development. Layout Creating... Project Challenge The Techni

Nov 12, 2021
go-zero is a web and rpc framework that with lots of engineering practices builtin.
go-zero is a web and rpc framework that with lots of engineering practices builtin.

go-zero is a web and rpc framework that with lots of engineering practices builtin. It’s born to ensure the stability of the busy services with resilience design, and has been serving sites with tens of millions users for years.

Jan 6, 2023
Go programming language secure coding practices guide

You can download this book in the following formats: PDF, Mobi and ePub. Introduction Go Language - Web Application Secure Coding Practices is a guide

Jan 9, 2023