1,官方网站:
http://openresty.org/
作者很NB,国人开发的。
2,安装nginx+lua环境
直接从openresty下载源代码进行编译安装,已经包括了一个最新的nginx代码。
我使用的是64bit的centos搭建环境的。
首先按照所需要的lib库。
(lib必须安装否则编译出现问题)
yum -y install gcc gcc-c++ autoconf make pcre-devel openssl openssl-devel
wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz
安装:
tar xzvf ngx_openresty-VERSION.tar.gz
cd ngx_openresty-VERSION/
./configure --with-luajit
make
make install
/usr/local/openresty/nginx 默认的安装位置
启动nginx。
/usr/local/openresty/nginx/sbin/nginx -s stop
/usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx/ -c conf/nginx.conf
修改iptables
vi /etc/sysconfig/iptables
增加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
service iptables restart
然后nginx就可以访问了。这个是带lua版本的nginx。
2,hello world
增加nginx配置:
location /hello {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
ngx.say(tostring(ngx.var.remote_addr),"<br/>")
ngx.say(tostring(ngx.var.arg_name),"<br/>")
';
}
这样就可以使用nginx开发动态接口了。
niginx的其他参数:
非常多的扩展和使用方法
https://github.com/chaoslawful/lua-nginx-module
3,连接数据库redis
local mysql = require "resty.mysql"
local memcached = require "resty.memcached"
local function query_mysql()
local db = mysql:new()
db:connect{
host = "127.0.0.1",
port = 3306,
database = "test",
user = "monty",
password = "mypass"
}
local res, err, errno, sqlstate =
db:query("select * from cats order by id asc")
db:set_keepalive(0, 100)
ngx.say("mysql done: ", cjson.encode(res))
end
local function query_memcached()
local memc = memcached:new()
memc:connect("127.0.0.1", 11211)
local res, err = memc:get("some_key")
ngx.say("memcached done: ", res)
end
ngx.thread.spawn(query_mysql) -- create thread 1
ngx.thread.spawn(query_memcached) -- create thread 2
4,其他rpm库
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum install lua lua-devel lua-static
RHEL/CentOS 6 64 Bit OS
wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm -Uvh rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
[http://www.tecmint.com/install-and-enable-rpmforge-repository-in-rhel-centos-6-5-4]
关闭yum-fastestmirror
vi /etc/yum/pluginconf.d/fastestmirror.conf
#禁用
enable=0