百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

tiny-nginx

> 编程语言
开源

基于 nginx 的事件驱动架构而开发的轻量级非阻塞 HTTP 服务器

189 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

基于 nginx 的事件驱动架构而开发的轻量级非阻塞 HTTP 服务器

tiny-nginx

A lightweight, non-blocking, event-driven, multi-process HTTP server written in C, inspired by the core architecture of Nginx.

Overview

tiny-nginx is a minimalistic HTTP server that implements the core architectural patterns that make Nginx powerful and efficient:

  • Non-blocking I/O: Uses epoll in level-triggered mode for efficient I/O multiplexing
  • Event-driven architecture: Handles connections asynchronously to maximize throughput
  • Multi-process model: Creates worker processes equal to the number of CPU cores for optimal resource utilization

Features

  • Asynchronous I/O: Non-blocking sockets with epoll event handling
  • Efficient worker processes: Automatically spawns workers based on available CPU cores
  • CPU affinity: Each worker is bound to a specific CPU core
  • Static file serving: Efficiently serves static files from a configured directory
  • HTTP protocol support: Basic HTTP/1.1 implementation
  • Graceful shutdown: Properly handles termination signals
  • MIME type detection: Automatically sets appropriate Content-Type headers
  • URL decoding: Properly handles URL-encoded paths
  • TCP optimizations: Uses TCP_NODELAY to reduce latency

Architecture

…

The server uses a multi-process architecture:

  1. Master Process: Responsible for:

    • Initializing the listening socket
    • Spawning worker processes
    • Managing worker lifecycle
    • Handling shutdown signals
  2. Worker Processes: Each worker:

    • Is bound to a specific CPU core
    • Accepts connections from the shared listening socket
    • Handles HTTP requests asynchronously
    • Serves static files efficiently using sendfile
  3. Event System: Core of the non-blocking architecture:

    • Uses epoll (level-triggered mode) for efficient I/O multiplexing
    • Tracks connection states
    • Handles events from multiple file descriptors

Building

Note: This project only works on Linux systems as it relies on Linux-specific features like epoll.

bash
# Clone the repository
git clone https://github.com/gd-arnold/tiny-nginx.git
cd tiny-nginx

# Build the project
make

Usage

bash
# Start the server on port 8080
./build/tiny-nginx -p 8080

By default the server serves files from the ./public directory.

Project Structure

  • client.{c,h}: HTTP client connection handling
  • dbg.h: Debugging macros
  • event.{c,h}: Event system (epoll wrapper)
  • main.c: Entry point
  • master.{c,h}: Master process implementation
  • server.{c,h}: TCP server setup
  • worker.{c,h}: Worker process implementation

Implementation Details

Event Loop

The core of tiny-nginx is its event loop, which efficiently multiplexes I/O operations:

…

HTTP Request Processing

Each HTTP request follows a state machine approach:

  1. CLIENT_RECEIVING_REQUEST: Read and parse the HTTP request
  2. CLIENT_SENDING_HEADERS: Send HTTP response headers
  3. CLIENT_SENDING_FILE: Send the requested file using sendfile
  4. CLIENT_CLOSING: Close the connection

File Serving

The server efficiently serves static files using sendfile() to avoid unnecessary copying between kernel and user space:

c
ssize_t n = sendfile(client->event.fd, client->file_fd,
        &client->file_offset,
        client->file_size - client->file_offset);

Limitations

The server is designed as a learning tool and is NOT meant to be used in production

There are many areas where it can be improved like:

  • Client timeout handling (!)
  • Detached mode
  • Logging
  • HTTP keep-alive support
  • Configuration file

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言