Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Comprehensive Guide to Nginx: Concepts, Installation, Configuration, Load Balancing, and High Availability

Comprehensive Guide to Nginx: Concepts, Installation, Configuration, Load Balancing, and High Availability

This article provides a detailed overview of Nginx, covering its core concepts, installation steps on Linux, configuration files, common commands, reverse proxy and load‑balancing setups, static‑dynamic separation, and high‑availability solutions using Keepalived, offering practical examples and code snippets throughout.

This guide introduces Nginx as a high‑performance HTTP and reverse‑proxy server, explaining its memory‑efficient design, ability to handle up to 50,000 concurrent connections, and the distinction between forward and reverse proxy.

It describes fundamental concepts such as forward proxy (used when LAN users cannot directly access the Internet) and reverse proxy (transparent to clients, forwarding requests to backend servers), and illustrates load‑balancing principles where requests are distributed across multiple servers.

The article explains static‑dynamic ("动静分离") separation, showing how static resources can be served by Nginx while dynamic content is processed by application servers like Tomcat.

Installation on Linux

1. Install PCRE and other dependencies:

# pcre-config --version
# yum -y make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

2. Extract and configure Nginx:

tar zxf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure \
  --prefix=/usr/local/nginx \
  --pid-path=/var/run/nginx/nginx.pid \
  --lock-path=/var/lock/nginx.lock \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/temp/nginx/client \
  --http-proxy-temp-path=/var/temp/nginx/proxy \
  --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
  --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
  --http-scgi-temp-path=/var/temp/nginx/scgi

3. Create missing temp directories if needed:

# mkdir -p /var/temp/nginx/client
# mkdir -p /var/temp/nginx/proxy
# mkdir -p /var/temp/nginx/fastcgi
# mkdir -p /var/temp/nginx/uwsgi
# mkdir -p /var/temp/nginx/scgi

4. Build and install:

# make
# make install

5. Start, stop, reload, and check Nginx:

# ./nginx          # start
# ./nginx -s stop  # stop
# ./nginx -s quit  # graceful quit
# ./nginx -s reload # reload configuration
# ps -ef | grep nginx   # view processes

Common Nginx commands

./nginx -v          # view version
./nginx              # start
./nginx -s stop      # stop
./nginx -s quit      # graceful quit
./nginx -s reload    # reload config

Configuration file structure

The nginx.conf file consists of three main blocks: the global block (overall server settings), the events block (network connection handling), and the http block (virtual hosts, reverse proxy, load balancing, etc.).

Location directive syntax:

location [ = | ~ | ~* | ^~ ] url {
    # configuration
}

Reverse‑proxy practical example

Requests to www.123.com are resolved to the server IP, Nginx listens on port 80, and forwards traffic to a Tomcat instance on port 8080. Another example forwards http://192.168.25.132:9001/edu/ to port 8080 and /vod/ to port 8081 using regular expressions.

Load‑balancing practical example

Modify nginx.conf to define an upstream block and choose a method (round‑robin, weight, fair, ip_hash). The article shows screenshots of each method and explains their behavior.

Static‑dynamic separation practical example

Static files are served directly by Nginx, while dynamic requests are proxied to Tomcat. Configuration snippets illustrate how to set up separate root directories and proxy_pass rules.

High‑availability with Keepalived

Two Nginx nodes are configured with Keepalived to provide a virtual IP (e.g., 192.168.25.50). The Keepalived configuration includes a health‑check script ( /usr/local/src/nginx_check.sh), VRRP settings, and authentication. Commands to install and start Keepalived are shown:

# yum install keepalived -y
# systemctl start keepalived.service

When the master node fails, the backup takes over the virtual IP, ensuring continuous service.

The article concludes with key takeaways: match the number of Nginx workers to CPU cores, use a master‑worker architecture for hot deployment, and leverage load balancing and high‑availability techniques for robust web services.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
High Availabilitynginxreverse-proxyLinux Installation

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion