Centos7编译安装Nginx加入Lua脚本支持

  1. 关闭防火墙
  2. 系统更新
  3. 类库安装
  4. 安装LuaJIT
  5. 安装Nginx
  6. 我所遇到的异常
  7. 启动测试
  8. 常用API

关闭防火墙

# 关闭防火墙
[root@cent local]# systemctl stop firewalld.service

# 关闭防火墙自启动
[root@cent local]# systemctl disable firewalld.service

系统更新

# 常规更新

[root@cent local]# yum install update -y && yum install upgrade -y

类库安装

# 基础类库 gcc zlib pcre openssl

[root@cent local]# yum -y install gcc gcc-c++ zlib zlib-devel pcre-devel openssl openssl-devel

安装LuaJIT

# 选择安装目录
[root@cent local]# cd /usr/local/

# 下载安装包
[root@cent local]# wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz

# 解压
[root@cent local]# tar –zxvf LuaJIT-2.0.2.tar.gz

# 编译安装
[root@cent local]# cd LuaJIT-2.0.2

[root@cent LuaJIT-2.0.2]# make install

安装Nginx

# 设置安装目录
[root@cent LuaJIT-2.0.2]# mkdir -p /usr/local/nginx
[root@cent LuaJIT-2.0.2]# cd /usr/local/nginx

# 下载安装包
[root@cent nginx]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

[root@cent nginx]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz

[root@cent nginx]# http://nginx.org/download/nginx-1.12.1.tar.gz

# 解压
[root@cent nginx]# tar -zxvf v0.3.0.tar.gz

[root@cent nginx]# tar -zxvf v0.10.9rc7.tar.gz

[root@cent nginx]# tar -zxvf nginx-1.12.1.tar.gz

# 编译
[root@cent nginx]# cd nginx-1.12.1/

[root@cent nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.9rc7

# ./configure \
# --prefix=/usr/local/nginx \
# --with-http_stub_status_module \
# --with-http_ssl_module \
# --add-module=../ngx_devel_kit-0.3.0 \
# --add-module=../lua-nginx-module-0.10.9rc7

[root@cent nginx]# make && make install

异常其他

无法找到libluajit-5.1.so.2

./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: N

# 寻找系统文件并复制
[root@cent nginx]# cp /usr/local/lib/libluajit-5.1.so.2 /usr/lib64/

相关命令

# 测试配置文件
[root@cent nginx]# /usr/local/nginx/sbin/nginx -t

# 加载配置文件
[root@cent nginx]# /usr/local/nginx/sbin/nginx -s reload

# 停止服务
[root@cent nginx]# /usr/local/nginx/sbin/nginx -s stop

# 启动服务
[root@cent nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

启动测试

修改nginx.conf

location / {
    default_type text/html;
    content_by_lua 'ngx.say("hello,lua")';
    # return 200 '{"code":"200"}';
}

启动测试

[root@cent nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

打开浏览器输入域名ip

hello,lua

测试通过

常用API

ngx.header.content_type = "text/html"
ngx.header.PowerBy = "Lua"
-- 请求头table
for k, v in pairs(ngx.req.get_headers()) do
    ngx.say(k, ": ", v)
end
 
-- 请求方法 GET、POST等
ngx.say("METHOD:" .. ngx.var.request_method)
 
-- 获取GET参数
for k, v in pairs(ngx.req.get_uri_args()) do
    ngx.say(k, ":", v)
end
 
 
-- 获取POST参数
ngx.req.read_body()
for k, v in pairs(ngx.req.get_post_args()) do
    ngx.say(k, ":", v)
end
 
-- HTTP版本
ngx.say(ngx.req.http_version())
 
-- 未解析的请求头字符串
ngx.say(ngx.req.raw_header())  
 
-- 未解析的BODY字符串
ngx.print(ngx.req.get_body_data())
 
-- ngx.exit(400)
-- ngx.redirect("/", 200)