Realization of nginx+lua Single Machine with Ten Thousand Concurrency

  • 2021-11-01 05:29:25
  • OfStack

nginx is our most commonly used server, which is often used for content distribution and reverse proxy. lua is a scripting language of C, which is widely used in the game industry. When page games were popular 10 years ago, I once bought the source code of legendary games, and the server in the game was implemented with lua. We often use nginx, envoy and redis to do some simple and practical functions, such as oversold and undersold, ranking, etc., to reduce the frequency of requests arriving at the back-end java

The following start to build nginx+lua mirror image, the reason for their own construction is that there are viruses in the mirror image provided by others, and there are many viruses in the unofficial mirror image of docker, which we need to pay attention to

In this paper, openresty version of nginx is adopted. The specific explanations of openresty, nginx and lua can be found under Baidu 1

Before building the mirror image, you need to prepare the nginx-module-vts module and the compressed package of openresty-1. 15.8.3. These two compressed packages can be found under Baidu 1. I don't know if the WeChat official account article can be inserted into the external chain. The function of nginx-module-vts module is to count the access data of nginx. If you monitor nginx with prometheus+grafana, you need to install this module. We simply compile it once.

Create a directory on the server


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

The complete directory after building is as follows:


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

Dockerfile

The Dockerfile file is placed in the build directory, and the downloaded nginx-module-ES50zip and openresty-1. 15.8.3. tar. gz are also placed 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

#  Replication Toolkit 
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 out and mount into the container when the container is started 
# 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;

 #  Below 3 The line is installed nginx-module-vts Setup after module nginx Traffic statistics, this article mainly talks about lua , so the following 3 Rows 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;

  # lua Whether the script turns on caching is set to during debugging off (Modification lua No need to restart after file nginx ), in a formal environment 1 Be sure to comment out this 1 Row to improve performance 
  lua_code_cache off;

  #  This location Is the real call lua Settings of scripts 
  location /lua/test {
   #  Specifies that the type returned is json
   default_type 'application/json';
   #  Specify access /lua/test Time by test.lua To return the content, this path should be noted to be the path in the container, and should not be confused with the host 
   content_by_lua_file '/usr/local/lua/test.lua';
  }

  #  It is also traffic statistics, which can be commented out 
  location /status {
   vhost_traffic_status_display;
   vhost_traffic_status_display_format html;
  }

 }
}

docker-compose.yml


version: '3.1'
services:
  nginx:
    build: build #  Left side build Means that the current container needs to build an image, and the right side build Represents the file that builds the image in the build Under this directory 
    restart: always
    container_name: nginx
    network_mode: host #  No 1 You must specify host Mode, here 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 the test. lua file in the./lua directory


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

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

So far nginx + lua has been built, in the following article will introduce a number of commonly used lua scripts, such as: JWT verification, operation Redis, message queue, etc., can achieve a lot of functions, as long as you can think of can be achieved


Related articles: