Master Nginx: From Basics to Advanced Configuration and High Availability

This article provides a comprehensive guide to Nginx, covering its core concepts, installation steps, key configuration sections, reverse‑proxy and load‑balancing examples, static‑dynamic separation, performance tuning, and high‑availability clustering for production environments.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Nginx: From Basics to Advanced Configuration and High Availability

1. Introduction to Nginx

Nginx ("engine x") is a high‑performance HTTP and reverse‑proxy server known for low memory usage and strong concurrency. Major Chinese sites such as Baidu, JD, Sina, NetEase, Tencent and Taobao use it.

1.1 Web Server

Nginx can serve static pages and supports CGI languages like Perl and PHP, but not Java directly; Java requires integration with Tomcat. It is optimized for performance and can handle up to 50 000 concurrent connections.

1.2 Reverse Proxy

Forward proxy: client configures proxy.

Reverse proxy: server handles proxy transparently.

1.3 Load Balancing

Nginx’s asynchronous architecture holds many concurrent requests and forwards them to backend servers, enabling hidden business hosts, IP savings, and easy scaling.

1.4 Static‑Dynamic Separation

Separating dynamic and static pages onto different servers speeds up parsing and reduces load on a single server.

Nginx serves static resources; Tomcat serves dynamic resources.

2. Installation and Startup

2.1 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

2.2 Installation Steps

1. Extract and compile PCRE, then ./configure && make && make install.

2. Extract and compile OpenSSL: ./config && make && make install.

3. Extract and compile zlib similarly.

4. Extract and compile Nginx: ./configure && make && make install.

Check open ports: firewall-cmd --list-all Open HTTP port: firewall-cmd --add-service=http –permanent and sudo firewall-cmd --add-port=80/tcp --permanent Reload firewall:

firewall-cmd –reload

2.3 Starting Nginx

Start: ./nginx in /usr/local/nginx/sbin Stop: ./nginx -s stop Reload: ./nginx -s reload To enable auto‑start, add /usr/local/nginx/sbin/nginx to /etc/rc.d/rc.

3. Core Configuration File (nginx.conf)

The main configuration resides in the conf directory. It consists of three sections: global, events, and http.

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

Global block sets overall server parameters such as user, worker processes, PID and log paths.

Events block controls connection handling, e.g., worker_connections 1024.

Http block contains most functional directives, including server and location blocks.

4. Reverse Proxy Example

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

Location directive syntax and modifiers (=, ~, ~*, ^~) are explained.

5. Load Balancing Example

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

Supported strategies: round‑robin (default), weight, ip_hash, and third‑party fair.

6. Static‑Dynamic Separation Example

Separate static files by domain or use Nginx location rules with expires to cache resources for a defined period.

7. Optimization Parameters

Master‑worker model avoids locking and isolates failures. Recommended worker count equals CPU cores. Example settings:

# Set worker number
worker_processes 4;
# Bind workers to CPUs
worker_cpu_affinity 0001 0010 0100 1000;

Connection limits: maximum connections = worker_processes * worker_connections. For reverse proxy, each request uses two connections.

8. High‑Availability Cluster

Keepalived + Nginx can be deployed in master‑slave or dual‑master modes.

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.

load balancingConfigurationNGINXreverse proxyWeb server
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.