`
sillycat
  • 浏览: 2543355 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Nginx and Proxy 2019(1)Nginx Enable Lua and Parse JSON

 
阅读更多
Nginx and Proxy 2019(1)Nginx Enable Lua and Parse JSON

Set Up Nginx
> wget http://nginx.org/download/nginx-1.16.1.tar.gz
> tar zxvf nginx-1.16.1.tar.gz
> ./configure --prefix=/home/carl/tool/nginx-1.16.1
> make
> make install
> sudo ln -s /home/carl/tool/nginx-1.16.1 /opt/nginx-1.16.1
> sudo ln -s /opt/nginx-1.16.1 /opt/nginx

Start the Service
> sudo sbin/nginx

Visit the page
http://ubuntu-master/

Check this and enable lua
https://docs.nginx.com/nginx/admin-guide/dynamic-modules/lua/#
https://github.com/openresty/lua-nginx-module#installation
https://github.com/openresty/lua-nginx-module#nginx-compatibility

Install LuaJIT
http://luajit.org/install.html
> wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
> tar zxvf LuaJIT-2.0.5.tar.gz
> cd LuaJIT-2.0.5/
> make
> make install PREFIX=/home/carl/tool/luajit-2.0.5
> sudo ln -s /home/carl/tool/luajit-2.0.5 /opt/luajit-2.0.5
> sudo ln -s /opt/luajit-2.0.5 /opt/luajit

Add to PATH
PATH="/opt/luajit/bin:$PATH"

Check version
> luajit -v
LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/

Install NDK (nix_devel_kit)
https://github.com/simplresty/ngx_devel_kit
Download the latest codes from here https://github.com/simplresty/ngx_devel_kit/tags
> wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1.tar.gz
> tar zxvf v0.3.1.tar.gz
> mv ngx_devel_kit-0.3.1 ~/tool/
> sudo ln -s /home/carl/tool/ngx_devel_kit-0.3.1 /opt/ngx_devel_kit-0.3.1
> sudo ln -s /opt/ngx_devel_kit-0.3.1 /opt/ngx_devel_kit

Instal ngx_lua
https://github.com/openresty/lua-nginx-module
> wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
> tar zxvf v0.10.15.tar.gz
> mv lua-nginx-module-0.10.15 ~/tool/
> sudo ln -s /home/carl/tool/lua-nginx-module-0.10.15 /opt/lua-nginx-module-0.10.15
> sudo ln -s /opt/lua-nginx-module-0.10.15 /opt/lua-nginx-module

Compile and Install Nginx
Check version here https://github.com/openresty/lua-nginx-module#nginx-compatibility
> wget https://nginx.org/download/nginx-1.17.6.tar.gz
> tar zxvf nginx-1.17.6.tar.gz
> cd nginx-1.17.6/

> export LUAJIT_LIB=/opt/luajit/lib
> export LUAJIT_INC=/opt/luajit/include/luajit-2.0

>  ./configure --prefix=/home/carl/tool/nginx-1.17.6 --with-ld-opt="-Wl,-rpath,/opt/luajit/lib" --add-module=/opt/ngx_devel_kit --add-module=/opt/lua-nginx-module
> make
> make install
> sudo ln -s /home/carl/tool/nginx-1.17.6 /opt/nginx-1.17.6
> sudo ln -s /opt/nginx-1.17.6 /opt/nginx

> sudo sbin/nginx
nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
nginx: [error] lua_load_resty_core failed to load the resty.core module from https://github.com/openresty/lua-resty-core; ensure you are using an OpenResty release from https://openresty.org/en/download.html (rc: 2, reason: module 'resty.core' not found:
no field package.preload['resty.core']
no file './resty/core.lua'

I may find the release from here LUAJIT2
https://github.com/openresty/luajit2/releases
> wget https://github.com/openresty/luajit2/archive/v2.1-20190912.tar.gz
> tar zxvf v2.1-20190912.tar.gz
> cd luajit2-2.1-20190912/
> make
> make install PREFIX=/home/carl/tool/luajit-2.1.0.3
> sudo ln -s /home/carl/tool/luajit-2.1.0.3 /opt/luajit-2.1.0.3
> sudo ln -s /opt/luajit-2.1.0.3 /opt/luajit

> export LUAJIT_LIB=/opt/luajit/lib
> export LUAJIT_INC=/opt/luajit/include/luajit-2.1

>  ./configure --prefix=/home/carl/tool/nginx-1.17.6 --with-ld-opt="-Wl,-rpath,/opt/luajit/lib" --add-module=/opt/ngx_devel_kit --add-module=/opt/lua-nginx-module
> make
> make install

Solution:
https://github.com/openresty/lua-nginx-module/issues/1533
https://superuser.com/questions/1426961/error-while-installing-lua-nginx-module
Add Confguration
> vi conf/nginx.conf
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;
    lua_load_resty_core off;

> sudo sbin/nginx


Check my implementation
https://github.com/openresty/lua-nginx-module#rewrite_by_lua_file
https://www.cnblogs.com/tinywan/p/6526191.html

        location /DWH {
            rewrite_by_lua_file /opt/nginx/lua/DWH_proxy.lua;
        }

> cat lua/DWH_proxy.lua
ngx.log(ngx.ERR, "hello, lua working");
ngx.req.set_uri("/",true);

URL rewrite
There are 2 commands to support that. ngx.exec and ngx.redirect
Try this library
https://github.com/ledgetech/lua-resty-http
> wget https://github.com/ledgetech/lua-resty-http/archive/v0.15.tar.gz
> cp /home/carl/install/lua-resty-http-0.15/lib/resty/* /opt/nginx/lua/

It does not work like that, it load the dependencies like this
2019/11/25 22:49:04 [error] 28551#0: *1 lua entry thread aborted: runtime error: /opt/nginx/lua/DWH_proxy.lua:2: module 'resty.http' not found:
no field package.preload['resty.http']
no file './resty/http.lua'
no file '/usr/local/share/luajit-2.1.0-beta3/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http/init.lua'
no file './resty/http.so'
no file '/usr/local/lib/lua/5.1/resty/http.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'

Solution:
Install and Setup luarocks
Check https://luarocks.org/
https://luarocks.org/#quick-start

> wget https://luarocks.org/releases/luarocks-3.2.0.tar.gz
> tar zxvf luarocks-3.2.0.tar.gz
> cd luarocks-3.2.0/

> ./configure --prefix=/opt/luajit --with-lua=/opt/luajit --lua-suffix=jit-2.1.0-beta3 --with-lua-include=/opt/luajit/include/luajit-2.1

> make build
> make install

> luarocks --version
/opt/luajit/bin/luarocks 3.2.0
LuaRocks main command-line interface

Install package
> luarocks install md5

> luarocks install lua-resty-http
Installing https://luarocks.org/lua-resty-http-0.15-0.src.rock
lua-resty-http 0.15-0 is now installed in /opt/luajit (license: 2-clause BSD)

Stop the nginx
> sudo sbin/nginx -s stop

Error load package
no file '/usr/local/share/luajit-2.1.0-beta3/luarocks/loader.lua'
Looking for  /opt/luajit/share/luajit-2.1.0-beta3

Reinstall that
> ./configure --prefix=/home/carl/tool/luarocks-3.2.0
> make
> make install
> sudo ln -s /home/carl/tool/luarocks-3.2.0 /opt/luarocks-3.2.0
> sudo ln -s /opt/luarocks-3.2.0 /opt/luarocks
Add to Path
PATH="/opt/luarocks/bin:$PATH"

> luarocks --version
/opt/luarocks/bin/luarocks 3.2.0

Add this to make it working
http {
    lua_package_path 'lua/?.lua;/opt/luarocks/share/lua/5.1/?.lua;;';

My testing LUA file works fine
> cat lua/DWH_proxy.lua
ngx.log(ngx.ERR, "hello, lua working");
local http = require "resty.http";
local httpc = http.new();
local res, err = httpc:request_uri("http://www.baidu.com",{
   method="GET"
});
if res.status == ngx.HTTP_OK then
       ngx.exec("/");
else
       ngx.exit(res.status);
end

This seems working fine for me
        location /DWH2 {
            resolver 8.8.8.8 8.8.4.4;
            set $proxy "";
            set $proxy_uri "";
            rewrite_by_lua '
                ngx.log(ngx.ERR, "hello, lua22 working");
                local http = require "resty.http";
                local httpc = http.new();
                local res, err = httpc:request_uri("http://www.baidu.com",{
                        method="GET"
                });
                if res.status == ngx.HTTP_OK then
                        ngx.var.proxy = "stagingdwhapi.sillycatcloudbeta.com:9000";
                        ngx.var.proxy_uri = "/api/1.0/accounts/01616dae-f6d6-4eda-b818-6991a9144685/streams?dateEnd=2019-11-22&dateStart=2019-11-08&filter=list";
                else
                        ngx.status=ngx.HTTP_UNAUTHORIZED;
                        ngx.exit(ngx.status)
                end
            ';
            proxy_pass http://$proxy$proxy_uri;
        }

Download the exactly the same version and compile with SSL
> wget https://nginx.org/download/nginx-1.14.0.tar.gz

> export LUAJIT_LIB=/opt/luajit/lib
> export LUAJIT_INC=/opt/luajit/include/luajit-2.1

Add Open SSL Library
> sudo apt-get install -y libssl-dev

>  ./configure --prefix=/home/carl/tool/nginx-1.14.2 --with-ld-opt="-Wl,-rpath,/opt/luajit/lib" --add-module=/opt/ngx_devel_kit --add-module=/opt/lua-nginx-module --with-http_ssl_module
> make
> make install

1.14.0 Not make well there.
Change to use 1.14.2, works well.

My previous working copy
cat /opt/nginx-1.17.6/conf/nginx.conf
#user  nobody;
worker_processes  1;
error_log  logs/error.log  notice;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    lua_package_path 'lua/?.lua;/opt/luarocks/share/lua/5.1/?.lua;;';
    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;
    lua_load_resty_core off;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location /DWH {
    resolver 8.8.8.8 8.8.4.4;
            set $proxy "";
            set $proxy_uri "";
            rewrite_by_lua '
               ngx.log(ngx.ERR, "lua working start---------");
local h = ngx.req.get_headers()
                for k, v in pairs(h) do
                    if k == "cookie" then
                    ngx.log(ngx.ERR, k,": ",v);
                    end
                end
local http = require "resty.http";
local httpc = http.new();
local res, err = httpc:request_uri("https://login.sillycatcloudbeta.com/ls/validate",{
   ssl_verify=true,
method="GET",
                        headers = h
});
                ngx.log(ngx.ERR, res);
if res.status == ngx.HTTP_OK then
       ngx.var.proxy = "stagingdwhapi.sillycatcloudbeta.com:9000";
        ngx.var.proxy_uri = "/api/1.0/accounts/01616dae-f6d6-4eda-b818-6991a9144685/streams?dateEnd=2019-11-22&dateStart=2019-11-08&filter=list";
else
       ngx.status=ngx.HTTP_UNAUTHORIZED;
            ngx.exit(ngx.status)
end
            ';
            proxy_pass http://$proxy$proxy_uri;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Install cjson to parse the JSON response
> luarocks install lua-cjson

The installation is not working
https://www.kyne.com.au/~mark/software/lua-cjson-manual.html
> wget https://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
> tar zxvf lua-cjson-2.1.0.tar.gz

This is a working copy
https://github.com/jiyinyiyong/json-lua


References:
https://github.com/Ermlab/nginx-lua-proxy
https://stackoverflow.com/questions/22788236/how-can-i-manipulate-the-json-body-of-a-post-request-using-nginx-and-lua
https://openresty-reference.readthedocs.io/en/latest/Directives/

https://serverfault.com/questions/946228/nginx-openresty-how-to-reverse-proxy-a-call-to-2-different-servers-based-bas
https://yos.io/2016/01/28/building-an-api-gateway-with-lua-and-nginx/
https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API/
https://github.com/upyun/slardar/issues/19


分享到:
评论

相关推荐

    Nginx之proxy_redirect使用详解

    所以在这里用到了nginx的proxy_redirect指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值 以下是截取nginx的一小段配置文档 server { listen 80; server_name www.boke.com; l

    Nginx+Lua+Redis+Json

    "Nginx、Lua、Redis 和 Json 的结合应用" Nginx 是一个高性能的 Web 服务器, Lua 是一种轻量级的脚本语言,而 Redis 是一个高性能的 NoSQL 数据库, Json 是一种轻量级的数据交换格式。通过结合使用这些技术,可以...

    docker-letsencrypt-nginx-proxy-companion-examples, 结合 Docker gen和 letsencrypt Nginx 代理伙伴的示例.zip

    docker-letsencrypt-nginx-proxy-companion-examples, 结合 Docker gen和 letsencrypt Nginx 代理伙伴的示例 docker-letsencrypt-nginx-proxy-companion-examples这个库是使用 nginx代理插件, docker gen和 docker-...

    Nginx反向代理和proxy

    此文比较详细讲述了Nginx与proxy共同搭建反向代理服务的配置方法

    Nginx+lua通过url传参的方式实现动态代理

    ### Nginx+Lua通过URL传参方式实现动态代理 #### 概述 在现代Web应用开发中,灵活地管理后端服务之间的通信是至关重要的。动态代理技术为这一需求提供了解决方案,使得前端可以方便地根据业务逻辑将请求转发至不同...

    跟我学Nginx+Lua开发_nginxlua原理_nginx_

    《跟我学Nginx+Lua开发》这本书主要探讨了如何利用Nginx与Lua进行高效、灵活的Web服务开发。Nginx以其高性能、反向代理和负载均衡能力而闻名,而Lua则是一种轻量级的脚本语言,两者结合可以实现强大的服务器端功能。...

    nginx实战-基于lua语言

    1. **Nginx模块 ngx_lua**:Nginx与Lua的桥梁是ngx_lua模块,由OpenResty公司开发,它允许我们在Nginx配置文件中直接嵌入Lua脚本,实现动态处理请求。 2. **性能优势**:由于LuaJIT(Just-In-Time编译器)的存在,...

    Nginx+upload+lua实现简单文件上传服务

    本教程将详细解析如何使用Nginx、upload模块以及lua来搭建一个简单的文件上传系统。 首先,我们需要了解Nginx的ngx_http_upload_module模块,这是一个用于处理HTTP POST请求中的文件上传的第三方模块。它允许我们在...

    nginx-proxy-manager中的Nginx配置

    **Nginx配置详解——以Nginx Proxy Manager为例** Nginx Proxy Manager是一款基于Nginx构建的网络代理管理工具,它允许用户通过友好的Web界面轻松地配置和管理Nginx反向代理设置。在深入理解Nginx Proxy Manager的...

    lua-nginx-module-master.zip

    1. **模块安装**:首先,你需要解压"lua-nginx-module-master.zip"并将其添加到Nginx的源代码构建路径中。然后,使用`--add-module`选项配置Nginx的编译过程,指定lua-nginx-module的源代码目录。完成编译和安装后,...

    lua-nginx-module-0.10.13

    Lua-Nginx-Module,简称lua-nginx-module,是Nginx服务器的一个重要扩展模块,它将强大的Lua脚本语言集成到Nginx中,允许用户在Nginx配置文件中直接编写Lua代码,极大地增强了Nginx的功能性和灵活性。版本0.10.13是...

    nginx_tcp_proxy_module-master.zip

    nginx_tcp_proxy_module-master.zip

    linux-一个用于nginxproxy的轻量级协同容器它能够自动创建更新LetsEncrypt证书

    标题中的“Linux-一个用于nginx-proxy的轻量级协同容器它能够自动创建更新LetsEncrypt证书”指的是一个专门设计用于nginx-proxy的轻量级Docker容器,它的主要功能是自动化处理Let's Encrypt证书的申请和更新过程。...

    基于lua的nginx自定义负载均衡

    基于lua的nginx自定义负载均衡基于lua的nginx自定义负载均衡

    nginx+lua+redis 集群 连接插件和脚本

    在构建高性能、高可用性的Web服务时,常常会利用到Nginx作为反向代理和负载均衡器,Lua作为扩展Nginx功能的脚本语言,而Redis则作为内存数据存储,提供快速的数据访问。本资源包“nginx+lua+redis集群 连接插件和...

    Nginx转发到动态域名的proxy.conf

    Nginx在vhost里的配置站点,通过proxy转发到动态域名的具体配置。 反向代理,动态域名 ,Proxy

    nginx+lua简要说明

    而目前市面上用的非常成熟的扩展是由章亦春将Lua和Nginx粘合的ngx_lua模块,并且将Nginx核心、LuaJIT、ngx_lua模块、许多有用的Lua库和常用的第三方Nginx模块组合在一起成为OpenResty,这样开发人员就可以安装...

    docker-letsencrypt-nginx-proxy-companion:LetsEncrypt伴侣容器用于nginx-proxy

    标题中的“docker-letsencrypt-nginx-proxy-companion”是一个开源项目,它专为与nginx-proxy容器协同工作设计,目的是为了方便地获取并自动更新Let's Encrypt的免费SSL/TLS证书。Let's Encrypt是一个非营利组织,...

    lua-nginx-16

    1. **非阻塞 I/O**:Lua 脚本在 Nginx 的事件驱动模型中运行,允许在不阻塞其他请求的情况下处理任务,提高了整体性能。 2. **轻量级嵌入**:Lua 作为内联脚本语言,可以在配置文件中直接编写,简化了部署和维护。 3...

Global site tag (gtag.js) - Google Analytics