Comprehensive Nginx Guide: Introduction, Installation, Core Configuration, and Advanced Practices

This article provides a detailed overview of Nginx, covering its role as a high‑performance web server and reverse proxy, explains installation steps, walks through core configuration files—including global, events, and http blocks—and demonstrates practical setups for reverse proxy, load balancing, static‑dynamic separation, performance tuning, and high‑availability clustering.

Top Architect
Top Architect
Top Architect
Comprehensive Nginx Guide: Introduction, Installation, Core Configuration, and Advanced Practices

Nginx (engine x) is a high‑performance HTTP server and reverse proxy widely used by major Chinese sites such as Baidu, JD.com, and Tencent. It can serve static content, act as a reverse proxy for dynamic back‑ends, and provide load‑balancing and static‑dynamic separation.

1. Nginx Overview

Web Server : Serves static pages and supports CGI languages like Perl and PHP (but not Java, which requires Tomcat). It can handle up to 50,000 concurrent connections.

Reverse Proxy : Clients are unaware of the backend; Nginx forwards requests to backend servers, improving security and scalability.

Load Balancing : Distributes traffic across multiple backend servers, enabling easy scaling and IP address conservation.

Static‑Dynamic Separation : Improves parsing speed by serving static resources from a dedicated server.

2. Installation and Startup

Required packages: pcre-8.37.tar.gz, openssl-1.0.1t.tar.gz, zlib-1.2.8.tar.gz, nginx-1.11.1.tar.gz.

Installation steps (simplified):

# Install PCRE</code>
<code>tar -zxvf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure && make && make install</code>
<code># Install OpenSSL</code>
<code>tar -zxvf openssl-1.0.1t.tar.gz && cd openssl-1.0.1t && ./config && make && make install</code>
<code># Install Zlib</code>
<code>tar -zxvf zlib-1.2.8.tar.gz && cd zlib-1.2.8 && ./configure && make && make install</code>
<code># Install Nginx</code>
<code>tar -zxvf nginx-1.11.1.tar.gz && cd nginx-1.11.1 && ./configure && make && make install

Start Nginx with /usr/local/nginx/sbin/nginx, stop with /usr/local/nginx/sbin/nginx -s stop, and reload with /usr/local/nginx/sbin/nginx -s reload. To enable auto‑start, add the binary path to /etc/rc.d/rc.

3. Core Configuration File (nginx.conf)

The default configuration resides in /usr/local/nginx/conf/nginx.conf and consists of three main sections:

Global block : Sets worker processes, user, PID, and includes.

Events block :

events {</code>
<code>    worker_connections 1024;</code>
<code>}

Controls connection handling and event model.

http block (contains http‑global, server, and location blocks):

http {</code>
<code>    include mime.types;</code>
<code>    default_type application/octet-stream;</code>
<code>    keepalive_timeout 65;</code>
<code>    server {</code>
<code>        listen 80;</code>
<code>        server_name localhost;</code>
<code>        location / {</code>
<code>            root html;</code>
<code>            index index.html index.htm;</code>
<code>        }</code>
<code>    }</code>
<code>}

Handles MIME types, keep‑alive, and virtual hosts.

4. Practical Configurations

Reverse Proxy Example :

server {</code>
<code>    listen 80;</code>
<code>    server_name localhost;</code>
<code>    location / { proxy_pass http://localhost:8001; }</code>
<code>    location ~ /demo1 { proxy_pass http://localhost:8001; }</code>
<code>    location ~ /demo2 { proxy_pass http://localhost:8002; }</code>
<code>}

Load Balancing Example (upstream) :

http {</code>
<code>    upstream myserver {</code>
<code>        ip_hash;</code>
<code>        server localhost:8080 weight=1;</code>
<code>        server localhost:8081 weight=1;</code>
<code>    }</code>
<code>    server {</code>
<code>        listen 80;</code>
<code>        server_name localhost;</code>
<code>        location / { proxy_pass http://myserver; }</code>
<code>    }</code>
<code>}

Load‑balancing strategies include round‑robin (default), weight, ip_hash, and third‑party fair (based on response time).

5. Performance Tuning

Set worker_processes to match CPU cores; each worker handles connections asynchronously. Example:

# Set 4 workers</code>
<code>worker_processes 4;</code>
<code># Bind each worker to a CPU</code>
<code>worker_cpu_affinity 0001 0010 0100 1000;

Adjust worker_connections to control maximum concurrent connections (e.g., worker_connections 1024;). The total possible connections equal worker_processes × worker_connections.

6. High‑Availability Cluster

Combine Keepalived with Nginx for active‑passive or active‑active clusters. Diagrams (omitted) illustrate virtual IP failover and dual‑master setups.

The article concludes with a call for community discussion, QR‑code links for interview questions, and promotional messages.

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.

backend-developmentInstallation
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.