nginx lua模块淘宝开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力.nginx以高并发 而知名,lua脚本轻便,两者的搭配堪称完美.接下来请看如何安装nginx + ngx_lua模块.以及最后来个简单的测试.

如果你是ubuntu系统,请看nginx+lua+redis构建高并发应用

http://yyyummy.blog.51cto.com/8842100/1401323  redis主从架构

redis的shell安装脚本,实现在linux下本机主从架构:http://birdinroom.blog.51cto.com/7740375/1401493

结合keepalived实现redis群集高可用故障自动切换 :http://birdinroom.blog.51cto.com/7740375/1401663

系统环境:centos/redhat

1. 下载安装LuaJIT


# cd /usr/local/src        


         # wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz        


         # tar -xzvf LuaJIT-2.0.2.tar.gz        


         # cd LuaJIT-2.0.2        


         # make        


         出现如下内容表示编译成功        


         OK        Successfully built LuaJIT        


         make         [1]: Leaving directory `         /usr/local/src/LuaJIT-2         .0.2         /src         '        


         ==== Successfully built LuaJIT 2.0.2 ====        


         # make install        


         出现如下内容,表示安装成功        


         ==== Successfully installed LuaJIT 2.0.2 to          /usr/local          ====


2. 下载准备nginx lua模块









# cd /usr/local/src/nginx-         1.3         .         1

5.需要替换nginx二进制文件,先备份一下原来的启动脚本。


[root@vmware1 nginx-1.3.1]         # cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak


这一步很重要,否则加了什么一直找不到,发现这个问题花了几天时间,后面把进程杀了重启才发现!

6.需要把nginx进程杀掉,不然复制会报错


[root@vmware1 nginx-1.3.1]         # killall nginx        


         nginx: no process killed        


         [root@vmware1 nginx-1.3.1]         # cp ./objs/nginx /usr/local/nginx/sbin/        


         cp         : overwrite `         /usr/local/nginx/sbin/nginx         ’?          yes


7.此时,查看下nginx的所有的模块,是否把cache_purge这个模块成功编译进去。


[root@vmware1 nginx-1.3.1]         # /usr/local/nginx/sbin/nginx -V        


         .         /objs/nginx         : error          while          loading shared libraries: libluajit-5.1.so.2: cannot          open          shared object          file         : No such          file          or directory        


         解决方法:        


         # ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2        


         或者:        


         #export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH


8.再次查看编译后的模块信息


[root@vmware1 nginx-         1.3         .         1         ]# /usr/local/nginx/sbin/nginx -V        


         built by gcc          4.4         .         7          20120313          (Red Hat          4.4         .         7         -         4         ) (GCC)        


         TLS SNI support enabled        


         configure arguments: --prefix=/usr/local/nginx --pid-path=/         var         /run/nginx.pid --lock-path=/         var         /lock/subsys/nginx --error-log-path=/         var         /log/httpd/error_log --http-log-path=/         var         /log/httpd/access_log --         with         -http_ssl_module --         with         -http_dav_module --         with         -http_flv_module --         with         -http_realip_module --         with         -http_gzip_static_module --         with         -http_stub_status_module --         with         -mail --         with         -mail_ssl_module --         with         -pcre=../pcre-         8.33          --add-module=../lua-nginx-module-         0.9         .5rc2/ --add-module=../echo-nginx-module-master --add-module=../ngx_devel_kit-         0.2         .         19         /


9. nginx lua配置
nginx配置文件加入如下配置:




Vim usr/local/nginx/conf/nignx.conf        


         server {        


                  listen                80         ;        


                  server_name           192.168         .         1.5         ;        


                  index index.php;        


                  root  /         var         /www/product/eliteall/v1/hosts;        


         location ~* ^/         2328         (/.*) {        


                  default_type          'text/plain'         ;        


                  content_by_lua          'ngx.say("hello, ttlsa lua")'         ;        


         }        


         }        


         cd /         var         /www/product/eliteall/v1/hosts        


         mkdir          2328


10.开启nginx.



[root@vmware1 nginx-1.3.1]         # /usr/local/nginx/sbin/nginx        


         [root@vmware1 nginx-1.3.1]         # netstat -anpt | grep nginx        


         tcp        0      0 0.0.0.0:80                  0.0.0.0:*                  LISTEN      6205         /nginx


11.访问测试




# curl http://192.168.1.5/2328/        


         hello, ttlsa lua          //         使用curl测试


nginx lua测试截图