Backend Development 12 min read

Introduction to Linux Socket Programming and OpenResty Deployment

This article explains Linux socket programming techniques such as non‑blocking I/O and epoll, discusses caching and asynchronous design for high‑performance servers, and provides a step‑by‑step guide to installing, configuring, and developing with OpenResty (Nginx + Lua) on Linux and Windows environments.

Architecture Digest
Architecture Digest
Architecture Digest
Introduction to Linux Socket Programming and OpenResty Deployment

Socket Programming

In Linux socket programming for handling many connections, non‑blocking I/O and multiplexing (select, poll, epoll) are used; epoll, introduced in Linux 2.6, is widely adopted by high‑performance servers such as Nginx.

For high‑performance services the focus is not language speed but caching and asynchronous non‑blocking I/O.

Cache

Understanding the speed hierarchy of storage and communication:

Memory > SSD > HDD

Local > Network

In‑process > Inter‑process

The goal of a cache system is to maximize in‑process hit rate, thereby maximizing overall efficiency. Asynchronous non‑blocking I/O lets the CPU serve other clients while waiting for slow resources.

Asynchronous Non‑Blocking

Use event‑driven mechanisms to be notified when I/O completes, freeing CPU cycles for other connections.

OpenResty

OpenResty is a high‑performance web platform built on Nginx and Lua, bundling Lua libraries and third‑party modules. It enables rapid construction of web applications handling 10K‑1M concurrent connections by running Lua scripts inside Nginx’s non‑blocking I/O model.

OpenResty combines Nginx with LuaJIT, allowing each worker process to host a separate Lua VM and coroutine per request, providing isolation of global variables.

Lua coroutines behave like lightweight threads with independent stacks, but only one runs at a time and switches only when explicitly yielded.

Benchmarks show OpenResty performance comparable to or exceeding Nginx’s C modules.

OpenResty Architecture

Load Balancing

LVS + HAProxy forward traffic to core Nginx instances, achieving load balancing.

Single‑Machine Closed Loop

All required data can be retrieved from the local server, avoiding network calls.

Distributed Closed Loop

Two main issues: data inconsistency (no master‑slave replication) and storage bottlenecks (disk or memory limits). Solutions include master‑slave or distributed storage and sharding by business key.

Access Gateway

The gateway receives traffic and performs tasks such as SSL termination, request routing, and rate limiting.

OpenResty Environment Setup

Download OpenResty from http://openresty.org and install required libraries (perl, libpcre, libssl).

# Check installed libraries
$ sudo ldconfig -v

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

Download and extract OpenResty, then configure, compile, and install:

$ wget https://openresty.org/download/ngx_openresty-1.13.6.1.tar.gz
$ tar -zxvf ngx_openresty-1.13.6.1.tar.gz
$ mv ngx_openresty-1.13.6.1 openresty
$ cd openresty
$ ./configure
$ sudo make && make install
$ sudo /usr/local/openresty/nginx/sbin/nginx

If Nginx fails to bind port 80, check which process occupies the port and stop it:

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

Edit the Nginx configuration to listen on both IPv4 and IPv6:

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

Test the server with curl or a browser at http://127.0.0.1/ .

Add OpenResty’s Nginx binaries to the user’s PATH:

$ sudo vim ~/.bashrc
export PATH=$PATH:/usr/local/openresty/nginx/sbin
source ~/.bashrc
https://www.nginx.com/resources/wiki/modules/lua/

content_by_lua

location /test {
  default_type text/html;
  content_by_lua 'ngx.say("hello openresty")';
}
# reload Nginx
$ /usr/local/openresty/nginx/sbin/nginx -s reload
# visit http://127.0.0.1/test

content_by_lua_file

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

lua_code_cache option

Disabling lua_code_cache off; hurts performance and should be enabled in production.

Summary

Developing with OpenResty involves two steps: modifying Nginx configuration and writing Lua scripts.

OpenResty Getting Started

Create Working Directory

Separate the working directory from the installation directory and create a custom Nginx configuration.

$ mkdir -p ~/openresty/test/{logs,conf}
$ vim ~/openresty/test/conf/nginx.conf
worker_processes 1;
error_log logs/error.log;
events { worker_connections 10224; }
http {
  server {
    listen 8001;
    location / {
      default_type text/html;
      content_by_lua_block {
        ngx.say("hello world");
      }
    }
  }
}
$ nginx -p ~/openresty/test
$ curl 127.0.0.1:8001
hello world

View Nginx Error Log

$ vim nginx/logs/error.log

Windows: View Nginx Processes

tasklist /fi "imagename eq nginx.exe"
taskkill /im nginx.exe /f
high concurrencyNginxluaSocket Programmingopenrestyasynchronous I/O
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login 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.