Nginx安装配置Lua支持
文章博客转载自:https://blog.csdn.net/qq_31725371/article/details/85226116
Nginx安装配置Lua支持
- 默认情况下
Nginx
不支持Lua
模块,需要安装LuaJIT
解释器,并且重新编译Nginx
,或者可使用国人开发的openrestry
- 需要的模块:
LuaJIT
,Ngx_devel
和lua-nginx-module
- Luajit官网:https://luajit.org
1. 环境准备
[root@nginx_lua ~]# yum install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel
- 1
2. 下载最新的luajit
和ngx_devel_kit
以及lua-nginx-module
解压
[root@nginx_lua ~]# mkdir -p /soft/src
[root@nginx_lua ~]# cd /soft/src/
wget https://luajit.org/download/LuaJIT-2.0.4.tar.gz
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
- 1
- 2
- 3
- 4
- 5
3. 解压ngx_devel_kit
以及lua-nginx-module
[root@nginx_lua src]# tar xf v0.2.19.tar.gz
[root@nginx_lua src]# tar xf v0.10.13.tar.gz
- 1
- 2
4. 编译安装LuaJIT
,即Lua
及时编译器
[root@nginx_lua src]# tar xf LuaJIT-2.0.4.tar.gz
[root@nginx_lua src]# cd LuaJIT-2.0.4/
[root@nginx_lua LuaJIT-2.0.4]# make && make install
- 1
- 2
- 3
- 4
5. 编译安装Nginx
[root@nginx_lua src]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@nginx_lua src]# tar xf nginx-1.14.2.tar.gz
[root@nginx_lua src]# cd nginx-1.14.2
[root@nginx_lua nginx-1.14.2]# ./configure --prefix=/soft/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_dav_module --with-file-aio --with-http_dav_module --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.10.13/
[root@nginx_lua nginx-1.14.2]# make && make install
[root@nginx_lua nginx-1.14.2]# ln -s /soft/nginx/sbin/nginx /usr/bin/nginx
[root@nginx_lua conf]# vim nginx.conf #简单配置写nginx测试Nginx是否已经支持Lua(lua指令方式)
...
server {
location /test_lua {
default_type text/html;
content_by_lua_block {
ngx.say("Hello Lua!")
}
}
...
}
#lua指令方式
#在server 中添加一个localtion
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
#lua文件方式
#在server 中添加一个localtion
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
}
#test.lua文件内容
ngx.say("hello world");
//建立软连接,如果不建立软链接,则会出现share object错误
[root@nginx_lua conf]# nginx -t
/soft/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
[root@nginx_lua conf]#
[root@nginx_lua lib64]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
[root@nginx_lua lib64]# ll libluajit-5.1.so.2
lrwxrwxrwx 1 root root 33 Dec 21 20:52 libluajit-5.1.so.2 -> /usr/local/lib/libluajit-5.1.so.2
[root@nginx_lua lib64]#
#//加载lua库,加入到ld.so.conf文件(暂时不执行这一步)
#[root@nginx_lua nginx-1.14.2]# echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
[root@nginx_lua conf]# nginx -t
nginx: the configuration file /soft/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /soft/nginx/conf/nginx.conf test is successful
[root@nginx_lua conf]# nginx -s reload
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
6. 测试安装Lua成功
这里我们可以直接下载window版本的
我们可以进行验证下,我们对nginx的配置如下
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid 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; #lua_package_path "$prefix/?.luac;$prefix/?.lua;;"; #gzip on; lua_package_path "F:\Openresty_For_Windows_1.13.5.1001_64Bit\skywalking-nginx-lua\lib\?.lua;;"; # Buffer represents the register inform and the queue of the finished segment lua_shared_dict tracing_buffer 100m; # Init is the timer setter and keeper # Setup an infinite loop timer to do register and trace report. init_worker_by_lua_block { local metadata_buffer = ngx.shared.tracing_buffer metadata_buffer:set('serviceName', 'wy nginx') -- Instance means the number of Nginx deloyment, does not mean the worker instances metadata_buffer:set('serviceInstanceName', 'User Service Instance Name') -- type 'boolean', mark the entrySpan include host/domain metadata_buffer:set('includeHostInEntrySpan', false) -- set randomseed require("skywalking.util").set_randomseed() require("skywalking.client"):startBackendTimer("http://127.0.0.1:8080") -- Any time you want to stop reporting metrics, call `destroyBackendTimer` -- require("skywalking.client"):destroyBackendTimer() -- If there is a bug of this `tablepool` implementation, we can -- disable it in this way -- require("skywalking.util").disable_tablepool() skywalking_tracer = require("skywalking.tracer") } server { listen 8787; location /hello2 { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; } } server { listen 8888; location /test { default_type text/html; rewrite_by_lua_block { require("tracer"):start("upstream service") } # 这里则是 上图中 nginx 上游服务的地址 proxy_pass http://127.0.0.1:7777/hello; body_filter_by_lua_block { if ngx.arg[2] then require("tracer"):finish() end } log_by_lua_block { require("tracer"):prepareForReport() } } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
这里我们启动nginx,访问
这样说明我们的nginx就安装成功并且集成的lua模块就已经ok了
nginx集成了lua模块,如果lua模块在nginx中安装成功,在安装目录中的下面会存在一个lua的模块
上面我们直接验证了,接下来我们验证下nginx支持文件的lua
默认情况下,我们编写的lua文件默认放在lua目录下
#test.lua文件内容 ngx.say("hello world");
我们在nginx中使用也可以使用直接路径的形式
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid 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; #lua_package_path "$prefix/?.luac;$prefix/?.lua;;"; #gzip on; lua_package_path "F:\Openresty_For_Windows_1.13.5.1001_64Bit\skywalking-nginx-lua\lib\?.lua;;"; # Buffer represents the register inform and the queue of the finished segment lua_shared_dict tracing_buffer 100m; # Init is the timer setter and keeper # Setup an infinite loop timer to do register and trace report. init_worker_by_lua_block { local metadata_buffer = ngx.shared.tracing_buffer metadata_buffer:set('serviceName', 'wy nginx') -- Instance means the number of Nginx deloyment, does not mean the worker instances metadata_buffer:set('serviceInstanceName', 'User Service Instance Name') -- type 'boolean', mark the entrySpan include host/domain metadata_buffer:set('includeHostInEntrySpan', false) -- set randomseed require("skywalking.util").set_randomseed() require("skywalking.client"):startBackendTimer("http://127.0.0.1:8080") -- Any time you want to stop reporting metrics, call `destroyBackendTimer` -- require("skywalking.client"):destroyBackendTimer() -- If there is a bug of this `tablepool` implementation, we can -- disable it in this way -- require("skywalking.util").disable_tablepool() skywalking_tracer = require("skywalking.tracer") } server { listen 8787; location /hello2 { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; } } server { listen 8777; location /hello2 { default_type 'text/plain'; rewrite_by_lua_file F:/Openresty_For_Windows_1.13.5.1001_64Bit/x64/nginx/test.lua; } } server { listen 8888; location /test { default_type text/html; rewrite_by_lua_block { require("tracer"):start("upstream service") } # 这里则是 上图中 nginx 上游服务的地址 proxy_pass http://127.0.0.1:7777/hello; body_filter_by_lua_block { if ngx.arg[2] then require("tracer"):finish() end } log_by_lua_block { require("tracer"):prepareForReport() } } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
这里nginx是安装在中文的目录下:中文的路径一定要使用/,不能写成下面的路径否则会出现错误F:\Openresty_For_Windows_1.13.5.1001_64Bit\x64\nginx\lua
出现错误可以进入到nginx的error.log中查看日志
rewrite_by_lua_file F:/Openresty_For_Windows_1.13.5.1001_64Bit/x64/nginx/test.lua;
在linux安装环境中,如果出现错误一定要看下nginx启动的用户和用户名,是否能够有权限访问创建lua的权限,否则会出现问题
test.lua内容如下
ngx.say("hello world222222");
我们访问页面上面输出的内容如下
接下来我们来看skywalking如何支持对nginx的对接
posted on 2021-02-25 16:24 luzhouxiaoshuai 阅读(2068) 评论(0) 编辑 收藏 举报