Master High‑Performance Linux Servers with OpenResty, Epoll, and Lua

Learn how Linux’s non‑blocking I/O mechanisms such as select, poll, and epoll enable high‑concurrency servers, explore OpenResty’s Lua‑based architecture, and follow step‑by‑step instructions for installing, configuring, and troubleshooting a performant OpenResty/Nginx environment on Linux.

21CTO
21CTO
21CTO
Master High‑Performance Linux Servers with OpenResty, Epoll, and Lua

Socket Programming

Linux socket programming for handling massive connection requests uses non‑blocking I/O and I/O multiplexing APIs such as select, poll and epoll. Since Linux 2.6, epoll is widely adopted in high‑performance servers; Nginx uses it for high concurrency.

For high‑performance back‑ends the focus is not language speed but caching and asynchronous non‑blocking support.

Cache

Cache hierarchy (fastest to slowest): memory > SSD > mechanical disk; local machine > network; intra‑process > inter‑process.

The goal is to keep the hit rate inside the process as high as possible, maximizing overall efficiency.

Asynchronous Non‑Blocking

When accessing databases, networks, or slow I/O devices, use event‑driven notifications so the CPU can serve other client connections instead of waiting.

OpenResty

OpenResty is a high‑performance web platform built on Nginx and Lua. It bundles a rich Lua library and third‑party modules, allowing developers to build dynamic web applications, services, or gateways that handle 10K‑1M concurrent connections. Lua scripts can call C and Lua modules inside Nginx, leveraging Nginx’s non‑blocking I/O model for consistent high‑performance responses.

OpenResty essentially combines Nginx with LuaJIT.

OpenResty Architecture

Nginx uses a master‑worker model: a master process manages multiple worker processes. Workers handle events; the master only performs initialization and worker management. In OpenResty each worker runs a Lua VM; each request creates a Lua coroutine with its own global table _G. Coroutines are isolated but share global variables, similar to threads.

Load Balancing and Deployment

Load balancing: LVS + HAProxy forward traffic to core Nginx instances.

Single‑machine closed loop: all data resides on the same server, avoiding network latency.

Distributed closed loop: solves data inconsistency and storage bottlenecks by using master‑slave replication and sharding.

Gateway

The access gateway receives traffic and performs tasks such as routing, TLS termination, and request preprocessing.

OpenResty Installation

Prerequisites: perl, libpcre, libssl.

# Check required libraries
sudo ldconfig -v

# Install required libraries
sudo apt install libpcre3-dev libssl-dev perl make build-essential curl libreadline-dev libncurses5-dev

Download, extract, configure, and compile:

wget https://openresty.org/download/ngx_openresty-1.13.6.1.tar.gz
tar -zxvf ngx_openresty-1.13.6.1.tar.gz
mv openresty-1.13.6.1 openresty
cd openresty
./configure
sudo make && make install   # installs to /usr/local/openresty

Start Nginx:

sudo /usr/local/openresty/nginx/sbin/nginx
ps -ef | grep nginx
service nginx status

If port 80 is already in use, free it:

sudo netstat -ntlp | grep 80
sudo killall -9 nginx

Edit nginx.conf to listen on both IPv4 and IPv6, then reload.

sudo vim /etc/nginx/conf/nginx.conf
listen 80;
listen [::]:80 ipv6only=on default_server;

Test with curl 127.0.0.1 or a browser.

Lua Content Handlers

Example using content_by_lua:

location /test {
    default_type text/html;
    content_by_lua 'ngx.say("hello openresty")';
}

Using content_by_lua_file with an external script:

location /test {
    content_by_lua_file 'html/test.lua';
}
-- html/test.lua
ngx.say("hello lua")

Disable lua_code_cache during development, but enable it in production for performance.

server {
    lua_code_cache off;   # turn off only while debugging
    location /test {
        content_by_lua_file 'html/test.lua';
    }
}

Summary

Developing with OpenResty involves editing Nginx configuration and writing Lua scripts. The platform provides a powerful, event‑driven environment for building high‑concurrency web services.

References

OpenResty best practices

Nginx Lua module documentation

NGINX API for Lua

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxhigh concurrencyNGINXLuaepollsocket programmingOpenResty
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.