OpenResty从入门到实战
温铭
OpenResty软件基金会主席,《OpenResty 最佳实践》作者
立即订阅
4332 人已学习
课程目录
已完结 52 讲
0/4登录后,你可以任选4讲全文学习。
开篇词 (1讲)
开篇词 | OpenResty,为你打开高性能开发的大门
免费
入门篇 (14讲)
01 | 初探OpenResty的三大特性
02 | 如何写出你的“hello world”?
03 | 揪出隐藏在背后的那些子项目
04 | 如何管理第三方包?从包管理工具luarocks和opm说起
05 | [视频]opm项目导读
06 | OpenResty 中用到的 NGINX 知识
07 | 带你快速上手 Lua
08 | LuaJIT分支和标准Lua有什么不同?
09 | 为什么 lua-resty-core 性能更高一些?
10 | JIT编译器的死穴:为什么要避免使用 NYI ?
11 | 剖析Lua唯一的数据结构table和metatable特性
12 | 高手秘诀:识别Lua的独有概念和坑
13 | [视频]实战:基于FFI实现的lua-resty-lrucache
14 | 答疑(一):Lua 规则和 NGINX 配置文件产生冲突怎么办?
API篇 (11讲)
15 | OpenResty 和别的开发平台有什么不同?
16 | 秒杀大多数开发问题的两个利器:文档和测试案例
17 | 为什么能成为更好的Web服务器?动态处理请求和响应是关键
18 | worker间的通信法宝:最重要的数据结构之shared dict
19 | OpenResty 的核心和精髓:cosocket
20 | 超越 Web 服务器:特权进程和定时任务
21 | 带你玩转时间、正则表达式等常用API
22 | [视频]从一个安全漏洞说起,探寻API性能和安全的平衡
23 | [视频]导读lua-resty-requests:优秀的lua-resty-*是如何编写的?
24 | 实战:处理四层流量,实现Memcached Server
25 | 答疑(二):特权进程的权限到底是什么?
测试篇 (5讲)
26 | 代码贡献者的拦路虎:test::nginx 简介
27 | test::nginx 包罗万象的测试方法
28 | test::nginx 还可以这样用?
29 | 最容易失准的性能测试?你需要压测工具界的“悍马”wrk
30 | 答疑(三)如何搭建测试的网络结构?
性能优化篇 (16讲)
31 | 性能下降10倍的真凶:阻塞函数
32 | 让人又恨又爱的字符串操作
33 | 性能提升10倍的秘诀:必须用好 table
34 | 特别放送:OpenResty编码指南
35 | [视频]实际项目中的性能优化:ingress-nginx中的几个PR解读
36 | 盘点OpenResty的各种调试手段
37 | systemtap-toolkit和stapxx:如何用数据搞定“疑难杂症”?
38 | [视频]巧用wrk和火焰图,科学定位性能瓶颈
39 | 高性能的关键:shared dict 缓存和 lru 缓存
40 | 缓存与风暴并存,谁说缓存风暴不可避免?
41 | lua-resty-* 封装,让你远离多级缓存之痛
42 | 如何应对突发流量:漏桶和令牌桶的概念
43 | 灵活实现动态限流限速,其实没有那么难
44 | OpenResty 的杀手锏:动态
45 | 不得不提的能力外延:OpenResty常用的第三方库
46 | 答疑(四):共享字典的缓存是必须的吗?
API网关篇 (4讲)
47 | 微服务API网关搭建三步曲(一)
48 | 微服务API网关搭建三步曲(二)
49 | 微服务API网关搭建三步曲(三)
50 | 答疑(五):如何在工作中引入 OpenResty?
结束语 (1讲)
结束语 | 行百里者半九十
OpenResty从入门到实战
登录|注册

02 | 如何写出你的“hello world”?

温铭 2019-05-29
你好,我是温铭。今天起,就要开始我们的正式学习之旅。
每当我们开始学习一个新的开发语言或者平台,都会从最简单的hello world开始,OpenResty 也不例外。让我们先跳过安装的步骤,直接看下,最简单的 OpenResty 程序是怎么编写和运行的:
$ resty -e "ngx.say('hello world')"
hello world
这应该是你见过的最简单的那种 hello world 代码写法,和 Python 类似:
$ python -c 'print("hello world")'
hello world
这背后其实是 OpenResty 哲学的一种体现,代码要足够简洁,也好让你打消“从入门到放弃“的念头。我们今天的内容,就专门围绕着这行代码来展开聊一聊。
上一节我们讲过,OpenResty 是基于 NGINX 的。那你现在是不是有一个疑问:为什么这里看不到 NGINX 的影子?别着急,我们加一行代码,看看 resty背后真正运行的是什么:
resty -e "ngx.say('hello world'); ngx.sleep(10)" &
我们加了一行 sleep 休眠的代码,让 resty 运行的程序打印出字符串后,并不退出。这样,我们就有机会一探究竟:
$ ps -ef | grep nginx
501 25468 25462 0 7:24下午 ttys000 0:00.01 /usr/local/Cellar/openresty/''1.13.6.2/nginx/sbin/nginx -p /tmp/resty_AfNwigQVOB/ -c conf/nginx.conf
终于看了熟悉的 NGINX 进程。看来,resty 本质上是启动了一个 NGINX 服务,那么resty 又是一个什么程序呢?我先卖个关子,咱后面再讲。
你的机器上可能还没有安装 OpenResty,所以,接下来,我们先回到开头跳过的安装步骤,把 OpenResty 安装完成后再继续。
取消
完成
0/1000字
划线
笔记
复制
© 版权归极客邦科技所有,未经许可不得传播售卖。 页面已增加防盗追踪,如有侵权极客邦将依法追究其法律责任。
该试读文章来自付费专栏《OpenResty从入门到实战》,如需阅读全部文章,
请订阅文章所属专栏。
立即订阅
登录 后留言

精选留言(45)

  • Geek_144c1d
    先将代码打包成`.lua` 文件
    使用配置文件指令 `content_by_lua_file` 引用
    类库代码 如果有 reqiure 的需求
    可利用 `lua_package_path` 和 `lua_package_cpath` 设定类库加载目录

    作者回复: 👍

    2019-05-29
    18
  • cylim
    既然是基础课,应该提醒我们关闭server。
    openresty -s quit -p `pwd` -c conf/nginx.conf


    把lua代码写在其他文件上,然后带入nginx.conf使用。

    作者回复: 欢迎补充:)

    2019-05-29
    6
  • aaron
    我使用yum安装了openresty之后并没有resty工具,我也没发现-p 'pwd'的意义何在,-c nginx.conf也启动不起来,最后我是用openresty -c /opt/geektime/conf/nginx.conf启动的。。。

    作者回复: centos 下需要单独安装:sudo yum install openresty-resty

    2019-05-30
    2
    5
  • helloworld
    编译安装PART1:
    老师说的对,对于大多数项目来说都没有必要自己折腾编译安装,费时费力。不过有时候自己动手编译安装也是必须的,因为根据具体项目的需要,比如CDN项目,官方的默认编译选项就缺少一些必要的模块,比如ngx_cache_purge模块,如果需要对ipv6做支持,还需要nginx的--with-ipv6编译选项,等等。下面是我根据官方打包文件总结的centos平台下编译安装最新版本的openresty具体流程。centos6下完美运行,7也应该不是问题。给各位小伙伴在需要的时候参考,少走弯路,节省宝贵时间。
    # 安装 pcre
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.bz2
    tar xjf pcre-8.42.tar.bz2
    cd pcre-8.42
     ./configure --prefix=/usr/local/openresty/pcre \
     --disable-cpp --enable-jit \
     --enable-utf --enable-unicode-properties
    make -j24 V=1 > /dev/stderr
    make install

    rm -rf /usr/local/openresty/pcre/bin
    rm -rf /usr/local/openresty/pcre/share
    rm -f /usr/local/openresty/pcre/lib/*.la
    rm -f /usr/local/openresty/pcre/lib/*pcrecpp*
    rm -f /usr/local/openresty/pcre/lib/*pcreposix*
    rm -rf /usr/local/openresty/pcre/lib/pkgconfig

    # 安装zlib
    cd /usr/local/src
    wget http://www.zlib.net/zlib-1.2.11.tar.xz
    tar xf zlib-1.2.11.tar.xz
    cd zlib-1.2.11
     ./configure --prefix=/usr/local/openresty/zlib
    make -j24 \
    CFLAGS='-O3 -D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN -g' \
    SFLAGS='-O3 -fPIC -D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN -g' > /dev/stderr
    make install
    rm -rf /usr/local/openresty/zlib/share/
    rm -f /usr/local/openresty/zlib/lib/*.la
    rm -rf /usr/local/openresty/zlib/lib/pkgconfig/

    # 安装openssl
    cd /usr/local/src
    wget https://www.openssl.org/source/openssl-1.1.0j.tar.gz
    wget https://raw.githubusercontent.com/openresty/openresty/master/patches/openssl-1.1.0d-sess_set_get_cb_yield.patch --no-check-certificate
    wget https://raw.githubusercontent.com/openresty/openresty/master/patches/openssl-1.1.0j-parallel_build_fix.patch --no-check-certificate
    tar zxf openssl-1.1.0j.tar.gz
    cd openssl-1.1.0j
    继续见PART2

    作者回复: 👍

    2019-06-21
    4
  • 业余草
    content_by_lua指令看起来非常怪!没有用{}

    作者回复: 嗯,可以用 content_by_lua_block

    2019-05-29
    3
  • One、Two、Three
    老师好,有个问题请教一下
    openresty -v
    nginx version: openresty/1.15.8.1
    which openresty
    /usr/local/bin/openresty
    which resty
    /usr/local/bin/resty
    这些都没问题
    which luajit
    luajit not found
    luajit为什么没有呢

    作者回复: luajit 的可执行文件并没有被拷贝到/usr/local/bin目录下,所有找不到。这个是为了避免了已经安装的 luajit 冲突,毕竟 OpenResty 自带的 LuaJIT 是自己维护的版本

    2019-06-15
    2
  • 回家
    温铭老师好,假设操作系统中已经安装openssl/pcre/zlib,使用openresty的仓库地址,然后使用包管理器安装openresty,这个时候操作系统里有几个openssl/pcre/zlib呢?只有一个的话,是不是openresy维护的openssl/pcre/zlib?如果是的话,升级或者说安装操作系统中的更新版本的openssl(不是升级openresy维护的openssl),能否升级成功呢?如果升级成功,openresty执行的时候是否会出错呢?

    作者回复: openresty 维护的 openssl/pcre/zlib 都会安装在 /usr/local/openresty/ 目录下,并不会冲突。

    2019-06-04
    2
  • moshufenmo
    请问老师,开发openresty使用什么IDE? 一直在用sublime,但是lua文件一多,相互间引用关系就很难查看

    作者回复: 没有什么特别好用的,我用的是微软的 vscode

    2019-05-29
    2
  • 天天向上
    mac brew安装貌似很费劲 网上很多方法都报错

    作者回复: brew 安装也需要先指定 OpenResty 的仓库地址,具体请查看openresty.org 的文档

    2019-05-29
    2
  • helloworld
    编译安装PART3:
    # 最后开始编译安装openresty
    cd /usr/local/src
    wget https://openresty.org/download/openresty-1.15.8.1.tar.gz # 如果下载出现ssl报错,yum update wget,再下载
    tar zxf openresty-1.15.8.1.tar.gz
    cd openresty-1.15.8.1
    ./configure \
        --prefix=/usr/local/openresty \
        --with-cc-opt="-DNGX_LUA_ABORT_AT_PANIC \
                    -I/usrl/local/openresty/zlib/include \
                    -I/usr/local/openresty/pcre/include \
                    -I/usr/local/openresty/openssl/include" \
        --with-ld-opt="-L/usr/local/openresty/zlib/lib \
                    -L/usr/local/openresty/pcre/lib \
                    -L/usr/local/openresty/openssl/lib \
                    -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl/lib" \
        --with-pcre-jit \
        --without-http_rds_json_module \
        --without-http_rds_csv_module \
        --without-lua_rds_parser \
        --with-stream \
        --with-stream_ssl_module \
        --with-stream_ssl_preread_module \
        --with-http_v2_module \
        --without-mail_pop3_module \
        --without-mail_imap_module \
        --without-mail_smtp_module \
        --with-http_stub_status_module \
        --with-http_realip_module \
        --with-http_addition_module \
        --with-http_auth_request_module \
        --with-http_secure_link_module \
        --with-http_random_index_module \
        --with-http_gzip_static_module \
        --with-http_sub_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gunzip_module \
        --with-threads \
        --with-luajit-xcflags='-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT' \
        -j24
    make -j24
    make install

    rm -rf /usr/local/openresty/luajit/share/man
    rm -rf /usr/local/openresty/luajit/lib/libluajit-5.1.a

    [END]
    2019-06-21
    1
  • helloworld
    编译安装PART2:
    patch -p1 < ../openssl-1.1.0d-sess_set_get_cb_yield.patch
    patch -p1 < ../openssl-1.1.0j-parallel_build_fix.patch
    ./config \
        no-threads shared zlib -g \
        enable-ssl3 enable-ssl3-method \
        --prefix=/usr/local/openresty/openssl \
        --libdir=lib \
        -I%/usr/local/openresty/zlib/include \
        -L%/usr/local/openresty/zlib/lib \
        -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/openssl/lib
    make -j24
    make install_sw

    rm -f /usr/local/openresty/openssl/bin/c_rehash
    rm -rf /usr/local/openresty/openssl/lib/pkgconfig

    继续见PART3
    2019-06-21
    1
  • 回家
    温铭老师,你好,有读者问vscode有没有openresty扩展,你说你用的lua扩展,这个是什么意思呢?还有老师你用的是什么IDE呀?

    作者回复: 我用的是 vs code,用的是 lua 和 luacheck 两个插件

    2019-06-04
    1
  • 曹峰
    老师,mac最新系统 10.15 无法安装 openresty , https://github.com/openresty/homebrew-brew/issues/10
    2019-10-23
    1
  • 阿柒
    老师那个ngx这个全局变量是谁提供的,怎么来的啊?
    2019-10-10
  • void
    老师,我也是用的mac vscode 装了luacheck。然后lua脚本里写ngx.say的时候,底下命令行窗口就会报一句:accessing undefined variable `ngx` luacheck[1,1] 。当然实际上 能运行,但是就是一直报这句,看着有点强迫症受不了。搜了好久也没找到解决方案,不知道老师用vscode的时候 会不会报这个,是如何解决的?
    2019-10-04
  • 程斌
    最近工作也用到这个工具了。特地买了教程来学习,谢谢老师。这个是我的学习笔记,会一步一步的跟着老师脚步做好笔记。https://www.iffor.cn/tech/openresty-install.html

    作者回复: 👍 总结之后的才是自己的知识

    2019-07-22
  • 在下不恭
    老师您好,安装完openresty以后,执行which resty 提示: no resty in (/usr/local/msp/runtime/jre-8u131-linux-x64/bin:/usr/local/msp/runtime/jre-8u131-linux-x64/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

    作者回复: 你那边是什么操作系统?使用什么方式安装的呢?如果使用 rpm 安装的话,是会有的,可以从这里看到:https://github.com/openresty/openresty-packaging/blob/master/rpm/SPECS/openresty.spec#L233

    2019-07-08
  • Leon📷
    已经解决了,新版的openresty的nginx依赖ssl1.1,所以启动不起来,我看了access log 然后编译了ssl1.1的动态库放在lib64下就好了,这个其他同学也可以参考下

    作者回复: 1.15.8 里面是依赖 openssl 1.1 的,所以尽量用 rpm 之类方式来处理

    2019-07-03
  • Leon📷
    resty --shdict='dogs 1m' -e 'local dict = ngx.shared.dogs
    > dict:set("Tom", 56)
    > print(dict:get("Tom"))'
    nginx: [emerg] unknown directive "lua_socket_log_errors" in /tmp/resty_oFWCJrrqwD/conf/nginx.conf:47 老师我一只报这个错误

    作者回复: 你的 OpenResty 是什么版本的呢?

    2019-07-01
  • yswang
    在我的Mac下按照文中安装步骤:
    brew tap openresty/brew
    brew install openresty

    还是安装失败,报 OpenSSL 找不到,但是在 /usr/local/Celler/openresty-openssl/1.1.0j 已经安装了啊:
    checking for PCRE library ... found
    checking for PCRE JIT support ... found
    checking for OpenSSL library ... not found
    checking for OpenSSL library in /usr/local/ ... not found
    checking for OpenSSL library in /usr/pkg/ ... not found
    checking for OpenSSL library in /opt/local/ ... not found

    这是什么原因啊?
    2019-06-27
收起评论
45
返回
顶部