您当前的位置:首页 > 网站建设 > 网站维护
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax | jQuery |

nginx+lua单机上万并发的实现

51自学网 2022-07-04 11:32:17
  网站维护

nginx是我们最常用的服务器,常用于做内容分发和反向代理,lua是一种类C的脚本语言,广泛应用于游戏行业,十年前页游流行的时候,我曾经买过传奇类游戏的源码,游戏中的服务端就是用lua实现的。我们常用来配合nginx、envoy和redis做一些简单实用的功能,比如:超卖和少卖、排行榜等,减少请求到达后端java的频率

下面开始构建nginx+lua的镜像,自己构建的原因是怕别人提供的镜像里有病毒,docker非官方镜像中有很多病毒,这一点大家需要注意

本文采用openresty版本的nginx,具体openresty、nginx和lua的说明大家可以百度一下

构建镜像之前需要先准备好nginx-module-vts模块和openresty-1.15.8.3的压缩包,这两个压缩包百度一下就能找到,我也不知道公众号文章能不能插外链,其中nginx-module-vts这个模块的作用是统计nginx的访问数据,如果自己用prometheus+grafana监控nginx,就需要安装这个模块,我们索性一起编译进来

在服务器上创建目录

cd /usr/local/dockermkdir -p nginx-lua/buildcd nginx-lua

搭建好之后的完整目录如下:

root@today2:/usr/local/docker/nginx-lua# tree.├── build│   ├── Dockerfile│   ├── nginx-module-vts.zip│   └── openresty-1.15.8.3.tar.gz├── docker-compose.yml├── lua│   ├── test.lua├── nginx.conf├── wwwroot│   ├── index.html

Dockerfile

Dockerfile文件放到build目录下,把下载好的nginx-module-vts.zip和openresty-1.15.8.3.tar.gz也放到build目录下

FROM ubuntu:xenial# 更新数据源WORKDIR /etc/aptRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > sources.listRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> sources.listRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> sources.listRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> sources.listRUN apt-get update# 安装依赖RUN apt-get install unzip make gcc libpcre3-dev libssl-dev perl build-essential curl zlib1g-dev --assume-yes# 复制工具包ADD openresty-1.15.8.3.tar.gz /usr/local/srcADD nginx-module-vts.zip /usr/local/src# nginx-module-vtsWORKDIR /usr/local/srcRUN unzip nginx-module-vts.zipWORKDIR /usr/local/src/openresty-1.15.8.3RUN rm -rf ./MakefileRUN ./configure --add-module=/usr/local/src/nginx-module-vtsRUN make && make install# 配置 Nginx,注释掉,在启动容器时挂载到容器中# ADD nginx.conf /usr/local/openresty/nginx/conf/WORKDIR /EXPOSE 80CMD ["/usr/local/openresty/nginx/sbin/nginx", "-c", "/usr/local/openresty/nginx/conf/nginx.conf", "-g", "daemon off;"]

nginx.conf

user root;worker_processes  auto;worker_rlimit_nofile 65535;events { worker_connections  102400; use epoll;}http { server_tokens off; include mime.types; default_type application/octet-stream; #access_log /var/log/nginx/access.log; access_log off; error_log /var/log/nginx/error.log; keepalive_timeout  65; client_max_body_size 10m;  gzip on; gzip_disable "msie6"; gzip_min_length 1000; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/xml application/javascript text/css application/x-javascript; # 下面3行是安装了nginx-module-vts模块后设置nginx流量统计,本文主要讲lua,所以下面3行可以注释掉 vhost_traffic_status_zone; vhost_traffic_status_filter_by_host on; vhost_traffic_status_filter_by_set_key $uri uri::$server_name; server {  listen 80;  root /usr/share/nginx/html;  # lua脚本是否开启缓存,在调试阶段设为off(修改lua文件后不用重启nginx),在正式环境一定要注释掉这一行,以提高性能  lua_code_cache off;  # 这个location是真正调用lua脚本的设置  location /lua/test {   # 指定返回的类型是json   default_type 'application/json';   # 指定访问/lua/test时由test.lua来返回内容,这个路径需要注意是容器中的路径,千万不要和宿主机搞混淆了   content_by_lua_file '/usr/local/lua/test.lua';  }  # 也是流量统计,可以注释掉  location /status {   vhost_traffic_status_display;   vhost_traffic_status_display_format html;  } }}

docker-compose.yml

version: '3.1'services:  nginx:    build: build # 左边build指的是当前容器需要构建镜像,右边build表示构建镜像的文件在build这个目录下    restart: always    container_name: nginx    network_mode: host # 不一定非要指定host模式,这里只是为了方便    volumes:      - ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf      - ./log:/var/log/nginx/      - ./wwwroot:/usr/share/nginx/html      - ./lua:/usr/local/lua

test.lua

在./lua目录下创建test.lua文件

ngx.say('{"code": 1, "msg": "hello world!"}')

启动容器后,访问IP:80/lua/test就可以看到输出了{"code": 1, "msg": "hello world!"},说明lua脚本已经生效

至此nginx+lua已经搭建完毕,在以后的文章中会再介绍一些常用的lua脚本,如:JWT验证、操作Redis、消息队列等,可以实现很多功能,只要你能想到都可以实现


下载地址:
解析Zabbix 5.0磁盘自动发现和读写监控的问题
如何解决Linux下Too many open files问题
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1