基于Nginx和lua的高性能web平台内置了大量lua库和第三方的模块,为了使用lua开发一些定制功能;
安装环境
# 安装环境
yum -y install pcre-devel openssl-devel gcc curl
# 上传openresty-1.11.2.5.tar.gz
在资源目录下
# 解压
tar -xf openresty-1.11.2.5.tar.gz
# 进入到解压目录
cd openresty-1.11.2.5
# 安装
./configure --prefix=/usr/local/openresty \
--with-luajit \
--without-http_redis2_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_sub_module
# 编译
make
#安装
make install
配置环境变量:
# 修改环境文件
vi /etc/profile
# 修改为以下内容
export JAVA_HOME=/usr/local/jdk8
export OPENRESTY_HOME=/usr/local/openresty/nginx
export PATH=$JAVA_HOME/bin:$OPENRESTY_HOME/sbin:$PATH
# 使环境文件生效
source /etc/profile
创建存放详情页html的目录:
# 创建目录
mkdir -p /usr/local/server/web/items
# 把生成的静态页上传到目录中(真正上线是直接把静态页面生成到该目录)
# 修改Nginx配置文件
vi /usr/local/openresty/nginx/conf/nginx.conf
# 在配置文件中添加以下内容:
location /items/ {
root /usr/local/server/web/;
}
访问测试:
# 启动Nginx(只能启动一次,启动命令第二次执行会报错)
nginx
# 重载Nginx(重新加载最新的配置,相当于重启nginx)
nginx -s reload
为了提供更好的用户体验,我们可以在Nginx中统一配置404页面。
在这里提供两种404的配置方式,任选其一即可
首先需要准备一个404提示页面:
# 创建并编辑404页面
vim /usr/local/openresty/nginx/html/404.html
# 内容就是一个普通的html页面,例如以下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品下架</title>
</head>
<body>
<h3>该商品已下架</h3>
</body>
</html>
1 通用404页面配置
在nginx的 http->server 部分都生效
# 修改配置文件nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf
# 在http配置中添加以下配置:
fastcgi_intercept_errors on;
# 在对应的server中添加以下配置
error_page 404 /404.html;
# 保存并退出nginx.conf的修改,nginx重载
nginx -s reload
2 指定路径404页面配置
如果希望404提示页面只对某一个请求路径生效,可以进行如下配置
# 修改配置文件nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf
# 在server中添加404页面的请求信息
location @np {
rewrite ^/(.*)$ /404.html;
}
# 在server的Location中添加 try_files属性
location /items/ {
root /usr/local/server/web/;
try_files $uri @np;
}
# 保存并退出nginx.conf的修改,并执行nginx重载
nginx -s reload
lua-restry-kafka
:https://github.com/doujiang24/lua-resty-kafka
unzip命令安装
yum install -y unzip
# 上传lua-resty-kafka-master.zip
在资源目录下
# 解压
unzip lua-resty-kafka-master.zip
# 移动到指定目录
mv lua-resty-kafka-master /usr/local/openresty/
# 修改nginx.conf
vi /usr/local/openresty/nginx/conf/nginx.conf
# 添加内容 在配置文件中指定lua-resty-kafka的库文件位置
# 配置到http里面,和server平级
lua_package_path "/usr/local/openresty/lua-resty-kafka-master/lib/?.lua;;";
配置效果如下:
http { ...... #gzip on; #添加内容 在配置文件中指定lua-resty-kafka的库文件位置 lua_package_path "/usr/local/openresty/lua-resty-kafka-master/lib/?.lua;;"; server { listen 80; server_name localhost; ...... }
3)配置请求 指向lua脚本
创建一个lua脚本,items-access.lua
编写测试内容:
ngx.say("test")
ngx.exit(200)
配置文件修改
# lua插件位置
lua_package_path "/usr/local/openresty/lua-resty-kafka-master/lib/?.lua;;";
server {
listen 80;
server_name localhost;
location /web/items/ {
#向kafka发送日志记录,处理请求路径,把/web去掉
content_by_lua_file /usr/local/openresty/nginx/lua/items- access.lua;
}
location /items/ {
root /usr/local/server/web;
}
location / {
root html;
index index.html index.htm;
}
}
lua脚本写法
--引入json解析库
local cjson = require("cjson")
--kafka依赖库
local client = require "resty.kafka.client"
local producer = require "resty.kafka.producer"
--配置kafka的服务地址
local broker_list = {
{ host = "192.168.200.188", port = 9092 }
}
--创建kafka生产者
local pro = producer:new(broker_list,{ producer_type="async"})
--获取IP
local headers=ngx.req.get_headers()
local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
--定义消息内容
local msg = {}
msg["ip"]=ip
msg["actime"]=os.date("%Y-%m-%d %H:%M:%S")
msg["uri"]=ngx.var.uri
msg["token"]="Bearer ITHEIMA"
--发送异步消息,无论消息是否发送成功,都会执行后面的逻辑
local offset, err = pro:send("logsitems", nil, cjson.encode(msg))
--请求转发到/items/,给用户提供html静态页
local uri = ngx.var.uri
uri = string.gsub(uri,"/web","")
ngx.exec(uri)
Lua脚本的时间获取
local getTime = os.date("%c");
其中的%c可以是以下的一种:(注意大小写)
格式 | 输出内容 |
---|---|
%a | abbreviated weekday name (e.g., Wed) |
%A | full weekday name (e.g., Wednesday) |
%b | abbreviated month name (e.g., Sep) |
%B | full month name (e.g., September) |
%c | date and time (e.g., 09/16/98 23:48:10) |
%d | day of the month (16) [01-31] |
%H | hour, using a 24-hour clock (23) [00-23] |
%I | hour, using a 12-hour clock (11) [01-12] |
%M | minute (48) [00-59] |
%m | month (09) [01-12] |
%p | either "am" or "pm" (pm) |
%S | second (10) [00-61] |
%w | weekday (3) [0-6 = Sunday-Saturday] |
%x | date (e.g., 09/16/98) |
%X | time (e.g., 23:48:10) |
%Y | full year (1998) |
%y | two-digit year (98) [00-99] |
%% | the character '%' |