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

2025-05-26 0 73

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单机并发内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 nginx+lua单机上万并发的实现 https://www.kuaiidc.com/53084.html

相关文章

发表评论
暂无评论