hello world

作者: 赵帅_a4fe | 来源:发表于2019-06-15 10:12 被阅读0次

    hello world 编写

    $ resty -e "ngx.say('hello world')"
    hello world
    

    一行代码,非常的简洁。
    openresty是基于nginx的,为什么我们看不到nginx的影子 ?
    我们修改下代码,执行完打印以后,休眠一段时间,再退出程序

    resty -e "ngx.say('hello world'); ngx.sleep(10)" &
    

    我们看下nginx的进程

    $ 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
    

    可以看到 hello world 程序执行,openresty启动了一个nginx服务。

    openresty 安装

    推荐优先使用 yum、apt-get、brew 这类包管理,来安装openresty

    brew tap openresty/brew
    brew install openresty
    

    这里有两个问题

    • 为什么我不推荐使用源码来安装呢?
      使用源码安装,需要自行编译,中间可能出出现各种奇怪的环境问题,还需要自己处理PCRE、OpenSSL等外部依赖以及打补丁,另外还有版本兼容的问题,除非对这些细节非常的清楚,我们更推荐使用官方的打包脚本
    • 为什么不能直接从操作系统的官方仓库安装,而是需要先设置另外一个仓库地址?
      官方仓库不愿意接受第三方维护的 OpenSSL、PCRE 和 zlib 包,这会导致其他使用者的困惑,不知道选用哪一个合适
      OpenResty 又需要指定版本的 OpenSSL、PCRE 库才能正常运行,而系统默认自带的版本都比较旧。

    安装完成以后,我们可以看下openresty的版本信息

    $ openresty -V
    nginx version: openresty/1.13.6.2
    built by clang 10.0.0 (clang-1000.10.44.4)
    built with OpenSSL 1.1.0h  27 Mar 2018
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/Cellar/openresty/1.13.6.2/nginx ...
    

    hello world web版

    • 第一步, 创建工作目录
    mkdir geektime
    cd geektime
    mkdir logs/ conf/
    
    
    • 第二步,修改 NGINX 的配置文件,把 Lua 代码嵌入其中
    events {
        worker_connections 1024;
    }
    http {
        server {
            listen 8080;
            location / {
                content_by_lua '
                    ngx.say("hello, world")
                ';
            }
        }
    }
    
    • 第三步,启动 OpenResty 服务
      首先确认openresty加入到PATH环境中,然后再启动openresty服务
    openresty -p `pwd` -c conf/nginx.conf
    
    • 第四步,请求服务
    $ curl -i 127.0.0.1:8080
    HTTP/1.1 200 OK
    Server: openresty/1.13.6.2
    Content-Type: text/plain
    Transfer-Encoding: chunked
    Connection: keep-alive
    
    hello, world
    

    至此,web版的hello world 就完成了

    抽离lua代码

    • 第一步,创建lua代码目录,在工作目录下创建代码目录lua
    $ mkdir lua
    $ cat lua/hello.lua
    ngx.say("hello, world")
    
    • 第二步,修改nginx.conf配置,把 content_by_lua_block 改为content_by_lua_file
    pid logs/nginx.pid;
    events {
        worker_connections 1024;
    }
    
    http {
        server {
            listen 8080;
            location / {
                content_by_lua_file lua/hello.lua;
                }
            }
        }
    }
    

    content_by_lua_file lua/hello.lua;写的是相对路径,openresty是如何找到对应的文件的呢?
    openresty 启动时会有-p PATH ,使用这个路径作为前缀,然后拼接上相对路径,从而构成了一个绝对路径定位到文件
    另外,openresty 提供了lua_package_path命令来定义lua模块的查找路径,比如$prefix/lua/?.lua;;
    $prefix为-p Path,lua/?.lua表示lua目录下所有的以.lua结尾的文件,;;代表内置的代码搜索路径
    项目中一般我们都会定义lua代码的包路径
    lua代码会在第一个请求时被加载,并且默认缓存起来,每次修改lua代码以后,需要重新加载才能生效,在开发调试过程中,一般将lua_code_cache关闭,不缓存从而每次都加载最新的代码,但线上欢迎一定要打开缓存,否则会非常影响性能。

    • 第三步,重启服务
    $ sudo kill -HUP `cat logs/nginx.pid`
    

    相关文章

      网友评论

        本文标题:hello world

        本文链接:https://www.haomeiwen.com/subject/mouvfctx.html