Implementation of tens of thousands of concurrent connections on a single machine with nginx+lua

Implementation of tens of thousands of concurrent connections on a single machine with nginx+lua

nginx is our most commonly used server, often used for content distribution and reverse proxy. Lua is a C-like scripting language, widely used in the gaming industry. Ten years ago when web games were popular, I bought the source code of a legendary game, and the server in the game was implemented using Lua. We often use it with nginx, envoy and redis to do some simple and practical functions, such as overselling and underselling, rankings, etc., to reduce the frequency of requests reaching the backend Java

Next, we will start to build the nginx+lua image. The reason for building it ourselves is that we are afraid that there are viruses in the images provided by others. There are many viruses in the unofficial docker images, which everyone needs to pay attention to.

This article uses the openresty version of nginx. For specific instructions on openresty, nginx, and lua, you can search on Baidu.

Before building the image, you need to prepare the compressed packages of nginx-module-vts module and openresty-1.15.8.3. You can find these two compressed packages by searching Baidu. I don’t know whether the official account articles can insert external links. The function of the nginx-module-vts module is to count the access data of nginx. If you use prometheus+grafana to monitor nginx, you need to install this module. Let’s compile it together.

Create a directory on the server

cd /usr/local/docker
mkdir -p nginx-lua/build
cd nginx-lua

The complete directory after construction is as follows:

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

Put the Dockerfile file in the build directory, and also put the downloaded nginx-module-vts.zip and openresty-1.15.8.3.tar.gz in the build directory

FROM ubuntu:xenial

# Update data source 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

# Install dependencies RUN apt-get install unzip make gcc libpcre3-dev libssl-dev perl build-essential curl zlib1g-dev --assume-yes

# Copy the tool package 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

# Configure Nginx, comment it out, and mount it to the container when starting the container # 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

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;

 # The following 3 lines are to set nginx traffic statistics after installing the nginx-module-vts module. This article mainly talks about lua, so the following 3 lines can be commented out 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;

  # Whether to enable cache for lua scripts. Set it to off in the debugging stage (no need to restart nginx after modifying lua files). Be sure to comment out this line in the formal environment to improve performance lua_code_cache off;

  # This location is the setting for actually calling the Lua script location /lua/test {
   #Specify the return type is json
   default_type 'application/json';
   #Specify that test.lua will return the content when accessing /lua/test. Note that this path is the path in the container and must not be confused with the host. content_by_lua_file '/usr/local/lua/test.lua';
  }

  # Also traffic statistics, you can comment out location /status {
   vhost_traffic_status_display;
   vhost_traffic_status_display_format html;
  }

 }
}

docker-compose.yml

version: '3.1'
services:
  nginx:
    build: build # The build on the left means that the current container needs to build an image, and the build on the right means that the image building file is in the build directory restart: always
    container_name: nginx
    network_mode: host # You don't have to specify the host mode, it's just for convenience 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

Create a test.lua file in the ./lua directory

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

After starting the container, visit IP:80/lua/test and you can see the output {"code": 1, "msg": "hello world!"}, indicating that the Lua script has taken effect.

At this point, nginx+lua has been built. In future articles, we will introduce some commonly used lua scripts, such as: JWT verification, Redis operation, message queue, etc., which can realize many functions, as long as you can think of it.

This is the end of this article about the implementation of tens of thousands of concurrent calls on a single machine with nginx+lua. For more relevant content on nginx lua single machine concurrency, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx+Lua+Redis builds high-concurrency web applications

<<:  Application of CSS3 animation effects in activity pages

>>:  The "3I" Standards for Successful Print Advertising

Recommend

Several methods of implementing two fixed columns and one adaptive column in CSS

This article introduces several methods of implem...

JavaScript closure details

Table of contents 1. What is a closure? 2. The ro...

Detailed explanation of the usage of DECIMAL in MySQL data type

Detailed explanation of the usage of DECIMAL in M...

Docker deployment RabbitMQ container implementation process analysis

1. Pull the image First, execute the following co...

MySQL 8.0.12 Simple Installation Tutorial

This article shares the installation tutorial of ...

Summary of basic SQL statements in MySQL database

This article uses examples to describe the basic ...

MySQL 8.0.11 installation tutorial with pictures and text

There are many tutorials on the Internet, and the...

Analysis of the Poor Performance Caused by Large Offset of LIMIT in MySQL Query

Preface We all know that MySQL query uses the sel...

Detailed explanation of mysql.user user table in Mysql

MySQL is a multi-user managed database that can a...

Summary of some of my frequently used Linux commands

I worked in operations and maintenance for two ye...

Solution to the failure of 6ull to load the Linux driver module

Table of contents 0x01 Failed to load the driver ...

The use of v-model in vue3 components and in-depth explanation

Table of contents Use two-way binding data in v-m...