生成Nginx-Docker镜像包
Dockerfile
# nginx Dockerfile
# Version 1.0
# author fendo
# Base images 基础镜像
FROM centos:centos7
#FROM hub.c.163.com/netease_comb/centos:7
#安装相关依赖
RUN yum -y update
RUN yum -y install gcc gcc-c++ autoconf automake make
RUN yum -y install zlib zlib-devel openssl* pcre* wget lua-devel
#MAINTAINER 维护者信息
MAINTAINER fendo aa@qq.com
#ADD 获取url中的文件,放在当前目录下
ADD http://nginx.org/download/nginx-1.14.0.tar.gz /tmp/
#LuaJIT 2.1
#ADD http://luajit.org/download/LuaJIT-2.0.5.tar.gz /tmp/
ADD https://github.com/LuaJIT/LuaJIT/archive/v2.0.5.tar.gz /tmp/
#ngx_devel_kit(NDK)模块
ADD https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz /tmp/
#lua-nginx-module 模块
ADD https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz /tmp/
#nginx ngx_cache_purge模块
ADD http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz /tmp/
#切换目录
WORKDIR /tmp
#安装LuaJIT 2.0.5
#RUN wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz -P /tmp/
RUN tar zxf v2.0.5.tar.gz
WORKDIR /tmp/LuaJIT-2.0.5
#RUN cd LuaJIT-2.0.5
RUN make PREFIX=/usr/local/luajit
RUN make install PREFIX=/usr/local/luajit
#安装ngx_devel_kit(NDK)
WORKDIR /tmp
RUN tar -xzvf v0.3.0.tar.gz
RUN cp -r ngx_devel_kit-0.3.0/ /usr/local/src/
#安装lua-nginx-module模块
RUN tar -xzvf v0.10.13.tar.gz
RUN cp -r lua-nginx-module-0.10.13/ /usr/local/src/
#安装nginx ngx_cache_purge模块
RUN tar -xzvf ngx_cache_purge-2.3.tar.gz
RUN cp -r ngx_cache_purge-2.3/ /usr/local/src/
#设置环境变量
RUN export LUAJIT_LIB=/usr/local/lib
RUN export LUAJIT_INC=/usr/local/include/luajit-2.0
RUN mkdir -p {/usr/local/nginx/logs,/var/lock}
#编译安装Nginx
RUN useradd -M -s /sbin/nologin nginx
RUN tar -zxvf nginx-1.14.0.tar.gz
RUN mkdir -p /usr/local/nginx
RUN cd /tmp/nginx-1.14.0 \
&& ./configure --prefix=/etc/nginx --user=nginx --group=nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_sub_module \
--add-module=/usr/local/src/lua-nginx-module-0.10.13 \
--add-module=/usr/local/src/ngx_devel_kit-0.3.0 \
--add-module=/usr/local/src/ngx_cache_purge-2.3 \
&& make && make install
#参数说明
#--prefix 用于指定nginx编译后的安装目录
#--add-module 为添加的第三方模块,此次添加了fdfs的nginx模块
#--with..._module 表示启用的nginx模块,如此处启用了http_ssl_module模块
RUN /etc/nginx/sbin/nginx -c /etc/nginx/nginx.conf
RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
#EXPOSE 映射端口
EXPOSE 80 443
#CMD 运行以下命令
#CMD ["nginx"]
CMD ["/etc/nginx/sbin/nginx","-g","daemon off;"]
docker build -f Dockerfile -t nginx_lua:1.0 .
镜像生成后启动容器:docker run --name mynginx -p 80:80 -d nginx_lua:1.0
安装luasocket模块
下载 : luasocket-2.0.3-rc1.zip 解压并复制到容器内
在宿主机: docker cp luasocket-2.0.3/ mynginx:/tmp
进入到容器内: docker exec -it mynginx bash
cd:/tmp/luasocket-2.0.3
vi config
修改成如下内容(或直接复制替换):
#------
# LuaSocket makefile configuration
#
#------
# Output file names
#
EXT=so
SOCKET_V=2.0.3
MIME_V=1.0.3
SOCKET_SO=socket.$(EXT).$(SOCKET_V)
MIME_SO=mime.$(EXT).$(MIME_V)
UNIX_SO=unix.$(EXT)
#------
# Lua includes and libraries
#
#LUAINC=-I/usr/local/include/lua50
#LUAINC=-I/usr/local/include/lua5.1
LUAINC=-I/opt/local/include
#------
# Compat-5.1 directory
#
#COMPAT=-Icompat-5.1r5
#------
# Top of your Lua installation
# Relative paths will be inside the src tree
#
#INSTALL_TOP_SHARE=/usr/local/share/lua/5.0
#INSTALL_TOP_LIB=/usr/local/lib/lua/5.0
#需要修改的地方-修改成如下内容==================================================
INSTALL_TOP_SHARE=/usr/share/lua/5.1
INSTALL_TOP_LIB=/usr/lib64/lua/5.1
INSTALL_DATA=cp
INSTALL_EXEC=cp
#------
# Compiler and linker settings
# for Mac OS X
#
#CC=gcc
#DEF= -DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN
#CFLAGS= $(LUAINC) $(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common
#LDFLAGS=-bundle -undefined dynamic_lookup
#LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
#------
# Compiler and linker settings
# for Linux
CC=gcc
DEF=-DLUASOCKET_DEBUG
CFLAGS= $(LUAINC) $(DEF) -pedantic -Wall -O2 -fpic
LDFLAGS=-O -shared -fpic
LD=gcc
#------
# End of makefile configuration
#
配置Nginx
- 进入docker容器内 ,docker exec -it mynginx bash;
- 进入Nginx目录 ,cd /etc/nginx;
- 编辑Nginx配置 ,vi nginx.conf;
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#init_by_lua_file 'file.lua';
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format main '[$upstream_response_time]-$resp_body';
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
#gzip on;
upstream websocket {
#hash $remote_addr consistent;
server 10.xx.xx.23:18081;
server 10.xx.xx.24:18081;
server 10.xx.xx.25:18081;
}
upstream authserver {
server 192.168.32.1:18081;
}
server {
listen 80;
server_name localhost;
#lua_need_request_body on;
underscores_in_headers on;
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://10.133.106.23;
}
location /ai-asr {
proxy_pass http://websocket;
proxy_set_header Host $host:$server_port;
proxy_http_version 1.1;
proxy_connect_timeout 4s;
proxy_read_timeout 300s;
proxy_send_timeout 12s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /ai-asr/on-line {
set $resp_body start;
#access_log logs/asr-access.log main;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
proxy_set_header Host $proxy_host;
proxy_set_header Application-Id "Hiro-888888888888";
proxy_set_header Content-Type "application/octet-stream";
proxy_set_header Asr-Protocol "1";
proxy_set_header Net-State "2";
proxy_set_header Applicator "1";
#proxy_http_version 1.1;
access_by_lua '
local headers_tab = ngx.req.get_headers()
local appCode = headers_tab["App-Code"]
local atimestamp = headers_tab["Timestamp"]
local asign = headers_tab["Sign"]
local requestId = headers_tab["Request-Id"]
local sequenceId = headers_tab["Sequence-Id"]
local res = ngx.location.capture("/ai-asr/auth/" .. appCode .."/" .. atimestamp .. "/" .. asign .. "/" .. requestId .."/" .. sequenceId)
ngx.var.resp_body = [[{"app_code":"]] .. appCode .. [[","request_id":"]] .. requestId .. [[","sequence_id":"]] .. sequenceId .. [["]]
if res.status == ngx.HTTP_OK and res.body == "success" then
else
ngx.print(res.body)
end
headers_tab = nil
appCode = nil
atimestamp = nil
asign = nil
requestId = nil
sequenceId = nil
res = nil
';
proxy_pass http://192.168.32.1:8080/test_auth/asr;
body_filter_by_lua '
local respbody = ngx.arg[1]
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. respbody
if ngx.arg[2] then
ngx.var.resp_body = ngx.var.resp_body .. [[,"resp_body":"]] .. ngx.ctx.buffered .. [["}]]
end
';
log_by_lua '
local socket = require "socket"
local address = "192.168.32.1"
local port = 9989
local udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)
udp:send(ngx.var.resp_body)
socket = nil
udp = nil
';
}
location /ai-asr/auth {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
proxy_pass http://authserver;
}
}
}
鉴权接口 : AuthenticationController
@GetMapping("/auth/{app_code}/{timestamp}/{sign}/{request_id}/{sequence_id}")
public String auth(@PathVariable("app_code") String app_code,
@PathVariable("timestamp") String timestamp,
@PathVariable("sign") String sign,
@PathVariable("request_id") String request_id,
@PathVariable("sequence_id") String sequence_id) {
/**
鉴权逻辑
**/
return "success";
}
日志记录(UDP)
import io.netty.util.CharsetUtil;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TestJava {
public static void main(String[] args) {
try {
// 1.构建DatagramSocket实例,指定本地端口
DatagramSocket socket = new DatagramSocket(9989);
byte[] buf = new byte[10240];
// 2.构建需要收发的DatagramPacket报文--构造 DatagramPacket,用来接收长度为 length 的数据包。
DatagramPacket packet = new DatagramPacket(buf, buf.length);
while (true) {
// 收报文---从此套接字接收数据报包,此方法在接收到数据报前一直阻塞
socket.receive(packet);
System.out.println("ip:" + packet.getAddress().getHostAddress());
// 接收信息
String receive = new String(packet.getData(), CharsetUtil.UTF_8).trim();
// app_code request_id sequence_id resp_time resp_body
System.out.println("接收信息" + receive);
buf = new byte[10240];
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}