logo
0
0
WeChat Login

Nginx-C - A Lightweight Web Server in Pure C

A high-performance web server written in pure C, inspired by Nginx architecture.

Features

  • Event-driven architecture using epoll for high performance
  • Multi-threaded worker processes for concurrent connections
  • HTTP/1.1 protocol support
  • Static file serving with MIME type detection
  • Access and error logging
  • Configurable via command line options
  • Non-blocking I/O for scalability
  • Zero external dependencies - pure C implementation

Architecture

┌─────────────┐ │ Master │ │ Process │ └──────┬──────┘ │ ├─────────────┬─────────────┬─────────────┐ │ │ │ │ ┌───▼───┐ ┌───▼───┐ ┌───▼───┐ ┌───▼───┐ │Worker 1│ │Worker 2│ │Worker 3│ │Worker N│ └───┬───┘ └───┬───┘ └───┬───┘ └───┬───┘ │ │ │ │ ┌───▼─────────────▼─────────────▼─────────────▼───┐ │ Event Loop (epoll) │ └──────────────────────────────────────────────────┘

Building

# Build the project make # Clean build artifacts make clean # Rebuild from scratch make rebuild # Build with debug symbols make debug

Running

# Start server with default settings (port 8080, 4 workers) ./bin/nginx # Start server on custom port ./bin/nginx -p 9000 # Start with specific number of workers ./bin/nginx -w 8 # Run as daemon ./bin/nginx -d # Test configuration ./bin/nginx -t # Show help ./bin/nginx -h # Show version ./bin/nginx -v

Command Line Options

OptionDescriptionDefault
-p, --port PORTSet listen port8080
-w, --workers NUMNumber of worker processes4
-d, --daemonRun as daemonfalse
-t, --testTest configuration-
-v, --versionShow version-
-h, --helpShow help-

Project Structure

nginx-c/ ├── src/ │ ├── nginx.c # Main entry point │ ├── config.h/c # Configuration management │ ├── master.c/h # Master process management │ ├── worker.c/h # Worker process implementation │ ├── event.c/h # Event loop (epoll wrapper) │ ├── http.c/h # HTTP protocol parsing │ └── log.c/h # Logging system ├── html/ # Static files directory │ └── index.html # Default welcome page ├── logs/ # Log files directory ├── obj/ # Build objects ├── bin/ # Build output └── Makefile # Build configuration

Configuration

The server uses a simple configuration system:

typedef struct { int port; // Listen port int worker_processes; // Number of workers int worker_connections; // Connections per worker char access_log[MAX_PATH_LEN]; char error_log[MAX_PATH_LEN]; char pid_file[MAX_PATH_LEN]; int daemon; server_config_t server; } global_config_t;

Default settings:

  • Port: 8080
  • Workers: 4
  • Document Root: ./html
  • Index file: index.html

Testing

Start the server and access it in a browser:

./bin/nginx # Then visit: http://localhost:8080

Test with curl:

curl http://localhost:8080 curl -I http://localhost:8080

Logging

Logs are written to:

  • logs/access.log - HTTP access logs
  • logs/error.log - Error messages

Log format:

[2025-02-15 10:30:45.123] [INFO] Server listening on port 8080 127.0.0.1 - - [15/Feb/2025:10:30:50 +0000] "GET /index.html HTTP/1.1" 200 1234

Supported Features

  • HTTP Methods: GET, POST, HEAD, PUT, DELETE, OPTIONS
  • Keep-alive connections
  • Static file serving with MIME types
  • Directory listing with index files
  • Non-blocking I/O
  • Signal handling (SIGINT, SIGTERM, SIGHUP)

Performance

The server is designed for high performance:

  • Uses edge-triggered epoll
  • Non-blocking sockets
  • Multi-threaded workers
  • Minimal memory copying

Security Considerations

  • Input validation
  • Buffer overflow protection
  • Path traversal prevention (basic)
  • Resource limits

License

This is an educational project demonstrating web server implementation.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

TODO

  • Configuration file support (nginx.conf style)
  • HTTPS/TLS support
  • FastCGI support
  • gzip compression
  • Virtual hosts
  • URL rewriting
  • Reverse proxy
  • Load balancing
  • WebSocket support
  • Cache system
  • Rate limiting

Author

Created as a demonstration of web server implementation in pure C.

About

No description, topics, or website provided.
Language
C88.9%
Shell4.5%
HTML4.2%
Makefile2.5%