Exploring Nginx Source: Directory Structure, Compilation Steps, and Module Overview
This guide walks through Nginx's source tree, explains the main modules, shows how to compile it on CentOS with required dependencies, and examines the resulting configuration and object files to help developers understand the server's inner workings.
In the previous article "Nginx初探" we introduced Nginx's basic features, Windows installation, and a simple load‑balancing example. This article focuses on the Nginx source code structure and how to compile it.
Nginx source src directory layout (without compilation)
Environment: nginx‑1.8.0 on CentOS 7.0. Install the tree command (e.g., yum install tree) to view the directory tree. The source contains 10 directories and 265 files. The main modules are Core, Event, Http, Mail, Misc (miscellaneous utilities), and OS.
It is recommended to download the source and explore it to better understand Nginx's functional components.
For example, the first file under the Core module, nginx.c, contains the following snippet (shown in the image).
The array ngx_core_commands[] defines all configuration directives used by the Core module. The event‑modules directory lists several event models that will be covered later. The author notes limited explanation due to limited shell and C knowledge.
Compiling the source
On CentOS, install the following prerequisite packages:
1. PCRE library (required for rewrite support)
2. OpenSSL (required for SSL support; skip if not needed)
3. gzip library
(On Ubuntu you can simply run sudo apt-get install nginx.)
4. Extract the source: tar -zxvf nginx-1.8.0.tar.gz 5. Configure, compile, and install:
Typical configure options include: --with-http_stub_status_module (status monitoring) --with-http_ssl_module (HTTPS support) --with-http_spdy_module (Google SPDY, requires SSL) --with-pcre (rewrite support)
After configuring, run: make and then: make install The binary is located under /usr/local/nginx-1.8.0/sbin (or /usr/sbin on Ubuntu).
Analyzing the compiled files
Under /usr/local/nginx-1.8.0 you will find the generated configuration files. The conf directory contains several files, with nginx.conf being the main configuration file.
Each configuration directive’s purpose can be inferred from the file, and will be explained in detail in later module‑specific tutorials.
After compilation, an objs directory is created inside the original source tree. The generated ngx_modules.c file declares all compiled modules using extern, allowing the configure script to include or exclude modules as needed.
Modules are declared with extern so they can be accessed globally. For example, the ngx_core_module is defined and statically initialized in src/core/nginx.c as a global ngx_module_t structure, and other modules follow the same pattern.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
