小知识:nginx+lua单机上万并发的实现

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,就需要安装这个模块,我们索性一起编译进来

在服务器上创建目录

?
1
2
3
cd /usr/local/docker
mkdir -p nginx-lua/build
cd nginx-lua

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

?
1
2
3
4
5
6
7
8
9
10
11
12
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目录下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
FROM ubuntu:xenial
# 更新数据源
WORKDIR /etc/apt
RUN echo deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse > sources.list
RUN echo deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse >> sources.list
RUN echo deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse >> sources.list
RUN echo deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse >> sources.list
RUN 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/src
ADD nginx-module-vts.zip /usr/local/src
# nginx-module-vts
WORKDIR /usr/local/src
RUN unzip nginx-module-vts.zip
WORKDIR /usr/local/src/openresty-1.15.8.3
RUN rm -rf ./Makefile
RUN ./configure –add-module=/usr/local/src/nginx-module-vts
RUN make && make install
# 配置 Nginx,注释掉,在启动容器时挂载到容器中
# ADD nginx.conf /usr/local/openresty/nginx/conf/
WORKDIR /
EXPOSE 80
CMD [“/usr/local/openresty/nginx/sbin/nginx”, “-c”, “/usr/local/openresty/nginx/conf/nginx.conf”, “-g”, “daemon off;”]

nginx.conf

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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

?
1
2
3
4
5
6
7
8
9
10
11
12
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文件

?
1
ngx.say({“code”: 1, “msg”: “hello world!”})

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

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

到此这篇关于nginx+lua单机上万并发的实现的文章就介绍到这了,更多相关nginx lua单机并发内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6967706020197957646

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Linux系统的文件传输方法

2023-3-24 3:02:50

建站知识

小知识:linux进行硬盘分区挂载的实现方法

2023-3-24 3:11:27

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索