原创

nginx 安装 lua 模块

1. 系统环境

Centos 7

2. 下载

3. 安装

nginx

  • 解压压缩包: nginx-1.17.9.tar.gzngx_devel_kit-0.3.1.tar.gzlua-nginx-module-0.10.14.tar.gzluajit2-2.1-20200102.tar.gz

  • 安装nginxnginx 模块

cd luajit2-2.1-20200102
make && make install PREFIX=/usr/local/luajit

cd nignx-1.17.9
# /usr/local/lib64 为 rabbitmq-c 库目录
./configure  --prefix=/usr/local/nginx --with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" --with-http_ssl_module  --with-http_stub_status_module    --add-module=/home/soft/ngx_devel_kit-0.3.1     --add-module=/home/soft/lua-nginx-module-0.10.14
make && make install
  • 注意点
    这里 lua-nginx-module 使用 0.10.14版本, 如果高于此版本会出现如下兼容性问题
sbin/nginx -c conf/nignx.conf
nginx: [alert] failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: ./resty/core.lua:6: module 'resty.core.regex' not found:
    no field package.preload['resty.core.regex']
    no file './resty/core/regex.lua'
    no file '/usr/local/share/luajit-2.1.0-beta3/resty/core/regex.lua'
    no file '/usr/local/share/lua/5.1/resty/core/regex.lua'
    no file '/usr/local/share/lua/5.1/resty/core/regex/init.lua'
    no file './resty/core/regex.so'
    no file '/usr/local/lib/lua/5.1/resty/core/regex.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'
    no file './resty.so'
    no file '/usr/local/lib/lua/5.1/resty.so'
    no file '/usr/local/lib/lua/5.1/loadall.so') in /usr/local/nginx/conf/nginx.conf:88

4. 配置 nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #log_format  dm  ' msg::"$request_body"##ua::$http_user_agent##referer::$http_referer##clientIP::$remote_addr##createdAt::$time_iso8601" ';

    server {

        listen       80;
        server_name  localhost;

        location /lua {
            default_type 'text/plain';
            content_by_lua 'ngx.say("hello, lua")';
        }       

        #error_page  404              /404.html;
        #
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

5. 测试

cd /usr/local/nginx
sbin/nginx -c conf/nginx.conf

curl http://localhost/lua
hello, lua
正文到此结束
本文目录