nginx 增加 lua模块
Nginx中的stub_status模块主要用于查看Nginx的一些状态信息.
本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定:
./configure –with-http_stub_status_module 这个模块如果需要也可以加入
######################### 下面是 lua模块
unknown directive "access_by_lua"
unknown directive "set_unescape_uri"
之所以报错是缺少nginx的三方插件,下面介绍安装nginx的第三方插件,插件很多直介绍三个
方式一:
下载 ngx_openresty,该集成包中有:Nginx,Lua或Luajit,ngx_lua,以及一些有用的Nginx第三方模块。
安装步骤:
- ./configure --with-luajit
- make
- make install
安装完成,个人建议第一种安装方便简单,另外这个版本还提供了很多的组件,安装不会出现错误。
方式二:
Ngx_lua手动编译进Nginx。
首先,我的 Nginx 安装路径为:/usr/local/nginx。
我将尝试编译的两个模块:echo,lua。
所需要的模块如下:
liujit http://luajit.org
lua http://www.lua.org
ngx_devel_kit https://github.com/simpl/ngx_devel_kit
echo-nginx-module https://github.com/agentzh/echo-nginx-module
lua-nginx-module https://github.com/chaoslawful/lua-nginx-module
安装步骤:
1、Luajit2.0.2(推荐)
- wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
- tar zxvf LuaJIT-2.0.2.tar.gz
- cd LuaJIT-2.0.2
- make
- sudo make install
安装lua
tar-zxvf lua-5.2.0.tar.gz
cd lua-5.2.0
make linux
make install
完成安装. 如果遇到 lua.c:67:31: fatal error: readline/readline.h: No such file or directory
说明缺少libreadline-dev依赖包 centos: yum install readline-devel
debian: apt-get install libreadline-dev.
下面需要配置一下 luajit 或 lua 的环境变量(Nginx编译时需要):
- -- luajit --
- # tell nginx's build system where to find LuaJIT:
- export LUAJIT_LIB=/path/to/luajit/lib
- export LUAJIT_INC=/path/to/luajit/include/luajit-2.0.2
- -- lua --
- # or tell where to find Lua if using Lua instead:
- export LUA_LIB=/path/to/lua/lib
- export LUA_INC=/path/to/lua/include
我的测试环境里,配置如下:
- export LUAJIT_LIB=/usr/local/luajit/lib
- export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
2、安装 ngx_devel_kit (NDK) 模块
- cd /usr/local
- git clone https://github.com/simpl/ngx_devel_kit.git
下载完成后,将在 /usr/local/ 目录下生成子目录 ngx_devel_kit。
3、安装 lua-nginx-module 模块
- cd /usr/local
- git clone https://github.com/chaoslawful/lua-nginx-module.git
下载完成后,将在 /usr/local/ 目录下生成子目录 lua-nginx-module。
4、重新编译Nginx,需要注意编译顺序!
- ./configure --prefix=/usr/local/nginx \
- --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
- --add-module=/usr/local/ngx_devel_kit \
- --add-module=/usr/local/echo-nginx-module \
- --add-module=/usr/local/lua-nginx-module
- make -j2
- make install
注释:重新编译 Nginx 二进制,Nginx 需要 quit 再启动。而普通配置更新则 reload 即可:
kill -HUP `cat /path/nginx/logs/nginx.pid`
/usr/local/nginx/sbin/nginx -s reload
模块编译成功!
重启Nginx服务器!
在编译安装 Nginx 的第三方模块时,碰到一个错误:
/usr/local/nginx/sbin/ngxin -s reload
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
百事不得其解,后来Google之,发现了解决办法。
在 Nginx 编译时,需要指定 RPATH,加入下面选项即可:
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
或者
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
5、测试Lua
在Nginx.conf 配置文件中,加入以下代码:
location /echo {
default_type 'text/plain';
echo 'hello echo';
}
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
重启Nginx服务器:
/usr/local/nginx/sbin/nginx -s reload
使用curl测试:
[root@localhost] curl http://localhost/echo
hello echo
[root@localhost] curl http://localhost/lua
hello lua
测试结果表明,两个模块都安装成功!
简洁版
- wget http://nginx.org/download/nginx-1.2.7.tar.gz
- wget http://luajit.org/download/LuaJIT-2.0.1.tar.gz
- wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
- wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.16.tar.gz
- tar xzf LuaJIT-2.0.1.tar.gz
- cd LuaJIT-2.0.1
- make
- make install PREFIX=/usr/local/LuaJIT/
- cd ..
- tar xzf nginx-1.2.7.tar.gz
- tar xzf v0.2.18.tar.gz
- tar xzf v0.7.16.tar.gz
- cd nginx-1.2.7
- export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
- export LUAJIT_LIB=/usr/local/LuaJIT/lib
- export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
- ./configure –user=www –group=www –prefix=/usr/local/webserver/nginx –with-http_stub_status_module –with-http_ssl_module –add-module=/root/software/ngx_devel_kit-0.2.18 –add-module=/root/software/lua-nginx-module-0.7.16/
- make
- make insatll
- ./configure --prefix=/usr/local/nginx/nginx-1.7.3 \
- --sbin-path=/usr/local/nginx/nginx-1.7.3 \
- --conf-path=/usr/local/nginx/nginx-1.7.3/conf/nginx.conf \
- --with-http_ssl_module \
- --with-openssl=/usr/local/nginx/openssl-1.0.1h \
- --with-pcre=/usr/local/nginx/pcre-8.33 \
- --with-zlib=/usr/local/nginx/zlib-1.2.8 \
- --with-http_stub_status_module \
- --add-module=/usr/local/nginx/ngx_devel_kit-0.2.18 \
- --add-module=/usr/local/nginx/lua-nginx-module-0.7.16 \
- --add-module=/usr/local/nginx/set-misc-nginx-module-0.27
另一篇文章:http://sunjun041640.blog.163.com/blog/static/2562683220134931235857/
misc-nginx-module和lua-nginx-module插件具体文档可参考如下:
https://github.com/openresty/set-misc-nginx-module/tags
https://github.com/openresty/lua-nginx-module#installation
记录日志:
- user root;
- 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 tick "$msec|||$u_t|||$remote_addr|||$u_domain|||$u_url|||$u_title|||$u_referrer|||$u_sh|||$u_sw|||$u_cd|||$u_lang|||$http_user_agent|||$u_utrace|||$u_account|||$u_time";
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #gzip on;
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- location /1.gif {
- #伪装成gif文件
- default_type image/gif;
- #本身关闭access_log,通过subrequest记录log
- access_log off;
- access_by_lua "
- -- 用户跟踪cookie名为__utrace
- local uid = ngx.var.cookie___utrace
- if not uid then
- -- 如果没有则生成一个跟踪cookie,算法为md5(时间戳+IP+客户端信息)
- uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent)
- end
- ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'}
- if ngx.var.arg_domain then
- -- 通过subrequest到/i-log记录日志,将参数和用户跟踪cookie带过去
- ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid .. '&time=' .. ngx.localtime())
- end
- ";
- #此请求不缓存
- add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT";
- add_header Pragma "no-cache";
- add_header Cache-Control "no-cache, max-age=0, must-revalidate";
- #返回一个1×1的空gif图片
- empty_gif;
- }
- location /i-log {
- #内部location,不允许外部直接访问
- internal;
- #设置变量,注意需要unescape
- set_unescape_uri $u_domain $arg_domain;
- set_unescape_uri $u_t $arg_t;
- set_unescape_uri $u_url $arg_url;
- set_unescape_uri $u_title $arg_title;
- set_unescape_uri $u_referrer $arg_referrer;
- set_unescape_uri $u_sh $arg_sh;
- set_unescape_uri $u_sw $arg_sw;
- set_unescape_uri $u_cd $arg_cd;
- set_unescape_uri $u_lang $arg_lang;
- set_unescape_uri $u_utrace $arg_utrace;
- set_unescape_uri $u_account $arg_account;
- set_unescape_uri $u_time $arg_time;
- #打开日志
- log_subrequest on;
- #记录日志到ma.log,实际应用中最好加buffer,格式为tick
- access_log /usr/local/nginx/nginxlog/access.log tick;
- #输出空字符串
- echo '';
- }
- #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;
- }
- # remove the robots line if you want to use wordpress' virtual robots.txt
- location = /robots.txt { access_log off; log_not_found off; }
- location = /favicon.ico { access_log off; log_not_found off; }
- # this prevents hidden files (beginning with a period) from being served
- location ~ /\. { access_log off; log_not_found off; deny all; }
- }
- }
nginx 增加 lua模块的更多相关文章
- Nginx使用Lua模块实现WAF
前言:最近一段时间在写加密数据功能,对安全相关知识还是缺少积累,无意间接触到了WAF相关知识,刚好Nginx可以实现WAF功能,也简单学习了Lua这门语言,分享下 一.WAF产生的背景 过去企业通常会 ...
- 为nginx增加nginx_http_concat模块
为nginx增加nginx_http_concat模块 时间 2013-06-05 22:14:56 我行我思 原文 http://www.fanjun.me/?p=562 主题 Nginx 缘由 ...
- Debian 为nginx增加第三方模块
为nginx增加第三方模块需要重新编译nginx的,但是debian在安装nginx的时候做了很多事情,比如systemd,/etc/nginx/里的各种文件,所以我们最好在debian源代码包的基础 ...
- nginx增加新模块
以gunzip这个模块为例,讲述一下,在nginx中如何安装新的模块 1.首先查看nginx已经安装了哪些模块. nginx –V 2.发现没有gunzip模块,安装 进入nginx的安装目录中,不是 ...
- nginx增加第三方模块
增加第三方模块 ============================================================ 一.概述nginx文件非常小但是性能非常的高效,这方面完胜ap ...
- nginx安装lua模块实现高并发
nginx安装lua扩展模块 1.下载安装LuaJIT-2.0.4.tar.gz wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar ...
- 编译nginx增加fair模块
安装必要的软件 ubuntu里是 apt install wget gcc make zlib1g-dev libpcre3-dev 去官网下载最新稳定版的nginx源码 git clone 源码 g ...
- nginx 安装第三方模块(lua)并热升级
需求: nginx上将特定请求拒绝,并返回特定值. 解决办法: 使用lua脚本,实现效果. 操作步骤: 安装Luajit环境 重新编译nginx(目标机器上nginx -V 配置一致,并新增两个模块n ...
- Nginx与Lua开发
1.Lua及基础语法 Nginx与Lua环境 场景:用Nginx结合Lua实现代码的灰度发布 1.Lua 是一个简洁.轻量.可扩展的脚本语言 2.Nginx+Lua优势 充分的结合Nginx的并发处理 ...
随机推荐
- ELK新手教程——pomelo + log4js + ELK(一)
随着项目越来越庞大,日志的数量也随之增多,游戏上线后的日志量肯定更多了,所以对日志的收集.存储.查询就非常重要了,开源界第一日志分析系统ELK必不可少. ELK系统由Elasticsearch(分布式 ...
- appium 搭建及实例
一.Appium环境搭建(Java版本) 转载2016-04-26 09:24:55 标签:appium移动端自动化测试 市场需求与职业生涯的碰撞,阴差阳错我就跨进了移动App端自动化测试的大门,前生 ...
- 安全测试6_Web安全工具第一节(浏览器入门及扩展)
今天来学习下浏览器的功能,浏览器是我们经常用到但是功能却很强大的一个东东,我们经常用到的无非是三种(谷歌.火狐.IE) 1.浏览器功能介绍: 下面以谷歌浏览器(Chrome版本为56)为例,介绍下,懂 ...
- LeetCode 5. Longest Palindromic Substring & 回文字符串
Longest Palindromic Substring 回文这种简单的问题,在C里面印象很深啊.希望能一次过. 写的时候才想到有两种情况: 454(奇数位) 4554(偶数位) 第1次提交 cla ...
- iOS工程结构理解
iOS开发中关于工程结构有三个关键部分,分别是:Targets,projects 和 workspaces. Targets指定了工程或者库文件如何编译,包括building setting,comp ...
- 转载:approxPolyDP函数
本文转载http://blog.csdn.net/qq_18343569/article/details/47999257 1.approxPolyDP函数 函数的作用: 对图像轮廓点进行多边形拟合 ...
- PropertiesUtils
package com.icil.elsa.subscribe.milestone.common.utils; import java.io.BufferedInputStream; import j ...
- 利用pyusb来查询当前所以usb设备
具体代码如下 #!/usr/bin/python# -*- coding:utf-8 -*- import sys import usb.core # find USB devices dev = u ...
- linux配置sphinx
1. 配置索引 cd /usr/local/sphinx/etc/ cp sphinx.conf.dist sphinx.conf //备份配置文件,防止改错 vim sphinx.conf 配置文件 ...
- iframe+form表单提交数据
<h6>基于iframe+Form表单</h6> <iframe id="iframe" name="ifra" onclick= ...