nginx 增加 lua模块-程序员宅基地

技术标签: java  运维  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,该集成包中有:NginxLuaLuajitngx_lua,以及一些有用的Nginx第三方模块。

安装步骤:

 
  1. ./configure --with-luajit   
  2. make   
  3. 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(推荐)

 

Java代码  
  1. wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz  
  2. tar zxvf LuaJIT-2.0.2.tar.gz  
  3. cd LuaJIT-2.0.2   
  4. make    
  5. 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编译时需要):

Java代码  
  1. -- luajit --   
  2. # tell nginx's build system where to find LuaJIT:   
  3. export LUAJIT_LIB=/path/to/luajit/lib   
  4. export LUAJIT_INC=/path/to/luajit/include/luajit-2.0.2  
  5. -- lua --   
  6. # or tell where to find Lua if using Lua instead:   
  7. export LUA_LIB=/path/to/lua/lib   
  8. export LUA_INC=/path/to/lua/include   

 

我的测试环境里,配置如下:

Java代码  
  1. export LUAJIT_LIB=/usr/local/luajit/lib   
  2. export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0  

   


2、安装 ngx_devel_kit (NDK) 模块

Java代码  
  1. cd /usr/local   
  2. git clone https://github.com/simpl/ngx_devel_kit.git    

 

下载完成后,将在 /usr/local/ 目录下生成子目录 ngx_devel_kit。


3、安装 lua-nginx-module 模块

 

Java代码  
  1. cd /usr/local   
  2. git clone https://github.com/chaoslawful/lua-nginx-module.git  

 

下载完成后,将在 /usr/local/ 目录下生成子目录 lua-nginx-module。


4、重新编译Nginx,需要注意编译顺序!

Java代码  
  1. ./configure --prefix=/usr/local/nginx \   
  2.                 --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \   
  3.                 --add-module=/usr/local/ngx_devel_kit \   
  4.                 --add-module=/usr/local/echo-nginx-module \   
  5.                 --add-module=/usr/local/lua-nginx-module   
  6. make -j2   
  7. 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  

测试结果表明,两个模块都安装成功!

 

简洁版

 

Java代码  
  1. wget http://nginx.org/download/nginx-1.2.7.tar.gz  
  2. wget http://luajit.org/download/LuaJIT-2.0.1.tar.gz  
  3. wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz  
  4. wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.16.tar.gz  
  5.   
  6. tar xzf LuaJIT-2.0.1.tar.gz  
  7. cd LuaJIT-2.0.1  
  8. make  
  9. make install PREFIX=/usr/local/LuaJIT/  
  10.   
  11. cd ..  
  12. tar xzf nginx-1.2.7.tar.gz  
  13. tar xzf v0.2.18.tar.gz  
  14. tar xzf v0.7.16.tar.gz  
  15. cd nginx-1.2.7  
  16.   
  17. export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0  
  18. export LUAJIT_LIB=/usr/local/LuaJIT/lib  
  19. export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH  
  20.   
  21. ./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/  
  22. make  
  23. make insatll  
  24.   
  25.   
  26.   
  27.   
  28. ./configure --prefix=/usr/local/nginx/nginx-1.7.3 \  
  29. --sbin-path=/usr/local/nginx/nginx-1.7.3 \  
  30. --conf-path=/usr/local/nginx/nginx-1.7.3/conf/nginx.conf \  
  31. --with-http_ssl_module \  
  32. --with-openssl=/usr/local/nginx/openssl-1.0.1h \  
  33. --with-pcre=/usr/local/nginx/pcre-8.33 \  
  34. --with-zlib=/usr/local/nginx/zlib-1.2.8 \  
  35. --with-http_stub_status_module \  
  36. --add-module=/usr/local/nginx/ngx_devel_kit-0.2.18 \  
  37. --add-module=/usr/local/nginx/lua-nginx-module-0.7.16 \  
  38. --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

 

 

记录日志:

Java代码  
  1. user  root;    
  2. worker_processes  1;    
  3.     
  4. #error_log  logs/error.log;    
  5. #error_log  logs/error.log  notice;    
  6. #error_log  logs/error.log  info;    
  7.     
  8. pid        logs/nginx.pid;    
  9.     
  10.     
  11. events {    
  12.     worker_connections  1024;    
  13. }    
  14.     
  15.     
  16. http {    
  17.     include       mime.types;    
  18.     default_type  application/octet-stream;    
  19.     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";    
  20.         
  21.     
  22.     #access_log  logs/access.log  main;    
  23.     sendfile        on;    
  24.     #tcp_nopush     on;    
  25.     
  26.     #keepalive_timeout  0;    
  27.     keepalive_timeout  65;    
  28.     
  29.     #gzip  on;    
  30.     
  31.     server {    
  32.         listen       80;    
  33.         server_name  localhost;    
  34.     
  35.         #charset koi8-r;    
  36.     
  37.     location /1.gif {    
  38.         #伪装成gif文件    
  39.         default_type image/gif;    
  40.         #本身关闭access_log,通过subrequest记录log    
  41.         access_log off;    
  42.         access_by_lua "    
  43.         -- 用户跟踪cookie名为__utrace    
  44.         local uid = ngx.var.cookie___utrace    
  45.         if not uid then    
  46.         -- 如果没有则生成一个跟踪cookie,算法为md5(时间戳+IP+客户端信息)    
  47.         uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent)    
  48.         end    
  49.         ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'}    
  50.         if ngx.var.arg_domain then    
  51.         -- 通过subrequest到/i-log记录日志,将参数和用户跟踪cookie带过去    
  52.         ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid .. '&time=' .. ngx.localtime())    
  53.         end    
  54.         ";    
  55.         #此请求不缓存    
  56.         add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT";    
  57.         add_header Pragma "no-cache";    
  58.         add_header Cache-Control "no-cache, max-age=0, must-revalidate";    
  59.         #返回一个1×1的空gif图片    
  60.         empty_gif;    
  61.         }    
  62.         location /i-log {    
  63.         #内部location,不允许外部直接访问    
  64.         internal;    
  65.      #设置变量,注意需要unescape    
  66.         set_unescape_uri $u_domain $arg_domain;    
  67.     set_unescape_uri $u_t $arg_t;    
  68.         set_unescape_uri $u_url $arg_url;    
  69.         set_unescape_uri $u_title $arg_title;    
  70.         set_unescape_uri $u_referrer $arg_referrer;    
  71.         set_unescape_uri $u_sh $arg_sh;    
  72.         set_unescape_uri $u_sw $arg_sw;    
  73.         set_unescape_uri $u_cd $arg_cd;    
  74.         set_unescape_uri $u_lang $arg_lang;    
  75.         set_unescape_uri $u_utrace $arg_utrace;    
  76.         set_unescape_uri $u_account $arg_account;    
  77.     set_unescape_uri $u_time $arg_time;    
  78.         #打开日志    
  79.         log_subrequest on;    
  80.         #记录日志到ma.log,实际应用中最好加buffer,格式为tick    
  81.         access_log /usr/local/nginx/nginxlog/access.log tick;    
  82.         #输出空字符串    
  83.         echo '';    
  84.         }    
  85.     
  86.         #error_page  404              /404.html;    
  87.     
  88.         # redirect server error pages to the static page /50x.html    
  89.         #    
  90.         error_page   500 502 503 504  /50x.html;    
  91.         location = /50x.html {    
  92.             root   html;    
  93.         }    
  94.         
  95.     # remove the robots line if you want to use wordpress' virtual robots.txt    
  96.         location = /robots.txt  { access_log off; log_not_found off; }    
  97.         location = /favicon.ico { access_log off; log_not_found off; }    
  98.         # this prevents hidden files (beginning with a period) from being served    
  99.         location ~ /\.          { access_log off; log_not_found off; deny all; }    
  100.     
  101.     }    
  102.     
  103.     
  104.     
  105. }   

转载于:https://www.cnblogs.com/vana/p/10093931.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30701575/article/details/99259164

智能推荐

win10 vscode opencv时运行出现 fatal error: opencv2/opencv.hpp: No such file or directory的超快解决方法_vscode fatal error: opencv2/opencv.hpp: no such fi-程序员宅基地

文章浏览阅读1.7w次,点赞12次,收藏19次。本人第一次用vscode配置opencv,本来照着网上教程一切都比较顺利,到最终测试的时候,居然遇到找不到头文件的情况。查阅各种教程,尝试更换opencv版本、mingw版本、卸载重装vscode等各种方法均无果。 然后网上也有很多遇到这种情况的,但均没有解决我的问题。其实我的解决方法非常简单,如下图。这里有两个三角,点右边的三角就会出现上述找不到头文件的问题,而点左边三角即可正常运行。也可以按f5或ctrl+f5,效果相同。如果出现终端闪退的问题,可以点击文件-首选项,设置,这时右上角就会多出一个_vscode fatal error: opencv2/opencv.hpp: no such file or directory

优化建模_在规定了下料模式种数不超过k种的下料问题中,如果希望余料最少,下列哪种处理方法不能得到问题的最优解-程序员宅基地

文章浏览阅读7.0k次,点赞2次,收藏3次。1 单选(2分)对目标函数系数作敏感性分析,目的是得到( )。A. 最优解不变时单个目标函数系数变化时的变化范围B. 最优值C. 最优解不变时多个目标函数系数变化时的变化范围D. 最优解正确答案:A2 单选(2分)下列说法错误的是( )。A. 处理成运输问题求解后,仍然需要归总销量才能得到各个月的产量B. 能处理成运输问题的关键是有产地,销地,单位费用C...._在规定了下料模式种数不超过k种的下料问题中,如果希望余料最少,下列哪种处理方法不能得到问题的最优解

npm install mint-ui运行报错_npm install --save mint-ui mint-ui/lib/style.css报错-程序员宅基地

文章浏览阅读649次。vue引用minit-UI报错npm intall mint-ui运行报错,解决方法:npm install mint-ui_npm install --save mint-ui mint-ui/lib/style.css报错

arcgis10之将多个shp文件合并成一个shp文件_arcgis合并多个shp文件-程序员宅基地

文章浏览阅读4.1w次,点赞6次,收藏54次。第一步第二步_arcgis合并多个shp文件

2020CVPR-面向人脸反欺骗的单边域泛化_single-side domain generalization for face anti-sp-程序员宅基地

文章浏览阅读2.9k次。2020CVPR,Single-Side Domain Generalization for Face Anti-Spoofing,中科院,有源代码文章链接:Single-Side Domain Generalization for Face Anti-Spoofing (thecvf.com)https://openaccess.thecvf.com/content_CVPR_2020/papers/Jia_Single-Side_Domain_Generalization_for_Face_Anti_single-side domain generalization for face anti-spoofing

联想台式机BIOS升级教程(2018.8)_联想电脑bios升级教程-程序员宅基地

文章浏览阅读492次,点赞10次,收藏10次。BIOS(Basic Input/Output System)是计算机硬件与操作系统之间的桥梁,它负责在开机时初始化硬件设备,并为操作系统提供必要的信息。随着技术的发展,BIOS也需要不断升级以适应新的硬件和操作系统。对于联想台式机用户,BIOS升级可能会遇到一些困难。本文将分享联想台式机BIOS升级教程(2018.8),帮助大家轻松掌握BIOS升级的方法和步骤,确保计算机的正常运行和性能提升。无论您是初学者还是有一定经验的用户,相信本文都将为您提供有益的参考和指导。_联想电脑bios升级教程

随便推点

cf 558 - Amr and Chemistry-程序员宅基地

文章浏览阅读218次。题意: 给你n个整数,你可以对这些数进行*2或者/2的操作,最终使n个数全部相同,问最少操作步骤是多少。思路: 由于这个题的范围是[1,1e5],可以直接对一个数进行枚举:把这个数和它(/2^n)之后的数一直乘2。代码:#include <bits/stdc++.h>using namespace std;int num[100005];int sum...

Pytest报告添加描述时报错:AttributeError: ‘TestReport‘ object has no attribute ‘description‘_attributeerror: 'testreport' object has no attribu-程序员宅基地

文章浏览阅读5.2k次。Pytest报告添加描述时报错AttributeError: 'TestReport' object has no attribute 'description'AttributeError: ‘TestReport’ object has no attribute ‘description’在conftest文件增加报告描述,如下:@pytest.mark.optionalhookdef pytest_html_results_table_header(cells): cells.inse_attributeerror: 'testreport' object has no attribute 'description

微信支付 商户API密钥key的生成与设置_apiv3密钥生成-程序员宅基地

文章浏览阅读5.3w次,点赞2次,收藏5次。设置商户号支付密钥方法如下:1. 申请通过审核后,打开微信发来的邮件。直接获取微信支付商户号(接口文档中的商户号MCHID);(也叫 受理商ID 填写到后台)。商户支付密钥key 按以下步骤自己设置生成。 2 点击访问 新商户平台http://mch.weixin.qq.com 或https://pay.weixin.qq.com,用账号和密码登录。并安_apiv3密钥生成

视频教程-蜂鸣器-第1季第11部分-单片机/工控-程序员宅基地

文章浏览阅读84次。蜂鸣器-第1季第11部分 互联网课程品牌《朱老师物联网大讲堂》创始人。精通U..._单片机视频教程蜂鸣器

shell 创建文件夹-程序员宅基地

文章浏览阅读1.6w次。#!/bin/bashdir="/root/test_dir"if [ ! -d "$dir" ];thenmkdir $direcho "创建文件夹成功"elseecho "文件夹已经存在"fi_shell 创建文件夹