github 项目地址:
nginx 是经常会用到的web 服务器,它有出色的并发性能,因此尝尝被用来当做负载均衡服务器,反向代理服务器。
nginx 的安装很简单,我通常是使用docker 安装,在编写dockerfile 时加入自己想要的功能,这样的话就可以随时随地拿来用。
统计qps 功能的nginx 服务,我只是下面的将github上两个项目的内容组合在一起。
一个是:
一个是:
感谢上面两个作者,给我们提供了方便。我在其中也修改了上面两个项目的内容。
其中Dockerfile:
FROM ubuntu:16.04ENV VER_NGINX_DEVEL_KIT=0.2.19ENV VER_LUA_NGINX_MODULE=0.10.7ENV VER_NGINX=1.10.3ENV VER_LUAJIT=2.0.5ENV NGINX_DEVEL_KIT ngx_devel_kit-${VER_NGINX_DEVEL_KIT}ENV LUA_NGINX_MODULE lua-nginx-module-${VER_LUA_NGINX_MODULE}ENV NGINX_ROOT=/nginxENV WEB_DIR ${NGINX_ROOT}/htmlENV LUAJIT_LIB /usr/local/libENV LUAJIT_INC /usr/local/include/luajit-2.0RUN apt-get -qq update#RUN apt-get install --assume-yes apt-utilsRUN apt-get -qq -y install wget# ***** BUILD DEPENDENCIES *****# Common dependencies (Nginx and LUAJit)RUN apt-get -qq -y install make# Nginx dependenciesRUN apt-get -qq -y install libpcre3RUN apt-get -qq -y install libpcre3-devRUN apt-get -qq -y install zlib1g-devRUN apt-get -qq -y install libssl-dev# LUAJit dependenciesRUN apt-get -qq -y install gccRUN apt-get install libluajit-5.1-dev --assume-yes# ***** DOWNLOAD AND UNTAR *****# DownloadRUN wget http://nginx.org/download/nginx-${VER_NGINX}.tar.gzRUN wget http://luajit.org/download/LuaJIT-${VER_LUAJIT}.tar.gzRUN wget https://github.com/simpl/ngx_devel_kit/archive/v${VER_NGINX_DEVEL_KIT}.tar.gz -O ${NGINX_DEVEL_KIT}.tar.gzRUN wget https://github.com/openresty/lua-nginx-module/archive/v${VER_LUA_NGINX_MODULE}.tar.gz -O ${LUA_NGINX_MODULE}.tar.gz# UntarRUN tar -xzvf nginx-${VER_NGINX}.tar.gz && rm nginx-${VER_NGINX}.tar.gzRUN tar -xzvf LuaJIT-${VER_LUAJIT}.tar.gz && rm LuaJIT-${VER_LUAJIT}.tar.gzRUN tar -xzvf ${NGINX_DEVEL_KIT}.tar.gz && rm ${NGINX_DEVEL_KIT}.tar.gzRUN tar -xzvf ${LUA_NGINX_MODULE}.tar.gz && rm ${LUA_NGINX_MODULE}.tar.gz# ***** BUILD FROM SOURCE *****# LuaJITWORKDIR /LuaJIT-${VER_LUAJIT}RUN makeRUN make install# Nginx with LuaJITWORKDIR /nginx-${VER_NGINX}RUN ./configure --prefix=${NGINX_ROOT} --with-ld-opt="-Wl,-rpath,${LUAJIT_LIB}" --add-module=/${NGINX_DEVEL_KIT} --add-module=/${LUA_NGINX_MODULE}RUN make -j2RUN make installRUN ln -s ${NGINX_ROOT}/sbin/nginx /usr/local/sbin/nginx# ***** MISC *****WORKDIR ${WEB_DIR}EXPOSE 4397EXPOSE 80EXPOSE 443# ***** CLEANUP *****RUN rm -rf /nginx-${VER_NGINX}RUN rm -rf /LuaJIT-${VER_LUAJIT}RUN rm -rf /${NGINX_DEVEL_KIT}RUN rm -rf /${LUA_NGINX_MODULE}COPY ngx_lua_reqstatus/ /nginxCOPY proxy_test.conf /nginx/conf/nginx.conf# TODO: Uninstall build only dependencies?# TODO: Remove env vars used only for build?# This is the default CMD used by nginx:1.9.2 imageCMD ["nginx", "-g", "daemon off;"]
proxy_test.conf 的内容如下
worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; map $http_upgrade $connection_upgrade { default upgrade; '' close; } lua_shared_dict statics_dict 1M; # 初始化变量 lua_package_path "/nginx/?.lua"; #路径 log_by_lua_file "/nginx/hook.lua"; # 添加此句 server { listen 0.0.0.0:4397; location /{ content_by_lua_file "/nginx/status.lua"; } } server { listen 80 default_server; access_log off; return 200 'Hello, World! - nginx\n'; } server { listen 80; server_name recomm.cnblogs.com; access_log off; location /{ resolver 127.0.0.11; proxy_set_header Host $host; proxy_pass http://dev-recomm_web; } }}
而 ngx_lua_reqstatus 文件夹里的内容则是第一个项目中的.lua 文件。没有添加可视化。
最后的效果,就是使用curl 命令:
curl http://127.0.0.1:4397/?domain=xxx.xxxx.xxxServer Name key: xxx.xxx.xxxSeconds SinceLast: 1.0710000991821Average Req Time Sec: 0.98439929089361Request Count: 283Requests Per Secs: 264.239004474525xx num: 0
qps 的统计原理是:每次获取qps时,会统计当前获取和上次获取之间的请求数和相差时间,从而算出qps.
最新版本镜像已经上传到dockerhub 仓库中