OpenResty Best Practices

Overview

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 从入门到实战》

Issues
  • 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

    opened by webkong 10
  • 内容修复 & 修复问题#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")
        }
    }
    
    opened by wangshunping 8
  • cosokect的疑问

    cosokect的疑问

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

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

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

    opened by zzctmac 4
  • reverse_proxy.cd 中有个链接失效了

    reverse_proxy.cd 中有个链接失效了

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

    location 匹配规则

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

    opened by wchaoyi 3
  • lj-lua-stacks.sxx 出错

    lj-lua-stacks.sxx 出错

    我的环境: centos 7, OpenResty 1.15.8.1

    [[email protected] 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

    opened by samuelyao314 2
  • 有个疑问关于日志记录的-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阶段就没问题了)不太对,还望帮忙解答这个疑问
    opened by hy0604 0
  • 变量作用范围里关于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

    opened by RocFang 1
Owner
WenMing
WenMing
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

Lucifer Chase 1 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

OWASP 4.2k Jun 22, 2022
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

OpCycle 5 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

Beat Labs 96 Jun 21, 2022
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

Logur 162 Jun 21, 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

niuyufu 510 Jun 19, 2022
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

Fairwinds 2.6k Jun 21, 2022
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

DigitalOcean 474 Jun 13, 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

AudD 210 Jun 17, 2022
Go Server/API boilerplate using best practices DDD CQRS ES gRPC

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

Rafał Lorenz 623 Jun 18, 2022
A cookbook with the best practices to working with kubernetes.

A cookbook with the best practices to working with kubernetes.

Diego Lima 1.3k Jun 26, 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

Quang Nguyen 4 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

null 0 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

Filipe Alves 10 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

ConsenSys Software 5.8k Jun 21, 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

Lucifer Chase 1 Jan 24, 2022
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

Fábio Morais 1 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. 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.

null 363 Jun 29, 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

OWASP 4.2k Jun 22, 2022
Golang-samples - Help someone need some practices when learning golang

GO Language Samples This project is to help someone need some practices when lea

Gui Chen 1 Jan 11, 2022
💁‍♀️Your new best friend powered by an artificial neural network

??‍♀️ Your new best friend Website — Documentation — Getting started — Introduction — Translations — Contributors — License ⚠️ Please check the Call f

Olivia 3.2k Jun 28, 2022
Realize is the #1 Golang Task Runner which enhance your workflow by automating the most common tasks and using the best performing Golang live reloading.

#1 Golang live reload and task runner Content - ⭐️ Top Features - ???? Get started - ?? Config sample - ?? Commands List - ?? Support and Suggestions

Oxequa 4.2k Jun 19, 2022
The best way to send emails in Go.

Gomail Introduction Gomail is a simple and efficient package to send emails. It is well tested and documented. Gomail can only send emails using an SM

Gomail 3.6k Jun 22, 2022
The best HTTP Static File Server, write with golang+vue

gohttpserver Goal: Make the best HTTP File Server. Features: Human-friendly UI, file uploading support, direct QR-code generation for Apple & Android

Sound Sun 1.7k Jun 28, 2022
GQLEngine is the best productive solution for implementing a GraphQL server 🚀

GQLEngine is the best productive solution for implementing a graphql server for highest formance examples starwars: https://github.com/gqlengine/starw

null 88 Apr 24, 2022
Realize is the #1 Golang Task Runner which enhance your workflow by automating the most common tasks and using the best performing Golang live reloading.

#1 Golang live reload and task runner Content - ⭐️ Top Features - ???? Get started - ?? Config sample - ?? Commands List - ?? Support and Suggestions

Oxequa 4.2k Jun 24, 2022
Moldy CLI the best project starter and manager of the world

You don't know how to start your project ... you want to help other people know your tool or language. Use Moldy! the best helper to start your project

Moldy Community 24 Apr 28, 2022
Moldy CLI the best project starter and manager of the world

Moldy The best project starter of the world ?? What is Moldy ? Hey I present Moldy this beautiful tool that will solve your life in creating, managing

Moldy Community 23 Jun 26, 2022
The best censorship API.

censor-api The best censorship API. Why choose this API? High quality. It could filter all sensitive words around the world. High performance. It coul

秋のかえで 6 Sep 6, 2021