Master Nginx: From Installation to Advanced Configuration on Linux, Docker, and macOS

This guide walks you through installing and configuring Nginx on CentOS, Docker, and macOS, explains core concepts like event‑driven architecture, compares Nginx with Tomcat, and clarifies the roles of traffic gateways, API gateways, and service registries, providing practical commands and Q&A for developers.

Lin is Dream
Lin is Dream
Lin is Dream
Master Nginx: From Installation to Advanced Configuration on Linux, Docker, and macOS

Brief Introduction

Nginx (pronounced Engine‑X) is an HTTP server created in 2004 by Russian developers, written in C. It can serve as a request gateway for HTTP and WebSocket services.

Its high performance, low resource consumption, and ability to handle massive concurrency make it widely used; it follows an event‑driven design similar to Netty.

Nginx is open‑source and can be installed in various ways.

CentOS 7 Installation

Download a stable version (e.g., 1.26.2) from nginx.org .

Recommended configure command:

./configure --prefix=/usr/local/nginx \
    --with-threads \
    --with-http_ssl_module \
    --with-pcre \
    --user=nginx \
    --group=nginx \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-openssl=/home/recharge/openssl/openssl-1.1.1d

Configuration details: --with-pcre requires

sudo yum install pcre pcre-devel
--with-zlib

requires

sudo yum install zlib zlib-devel
--with-http_image_filter_module

requires

sudo yum install gd gd-devel
--with-http_geoip_module

requires sudo yum install geoip geoip-devel After installation, you can start, test, and restart Nginx:

# Start
/home/user/nginx/sbin/nginx
# Test configuration
/home/user/nginx/sbin/nginx -t
# Reload
/home/user/nginx/sbin/nginx -s reload
# Show compiled modules
/home/user/nginx/sbin/nginx -V
# Check running processes
ps -aux | grep nginx

Docker Installation of Nginx

# Pull image and run container
docker run -p 80:80 -d --name mynginx nginx:1.18.0
# Copy configuration files
docker cp mynginx:/etc/nginx/nginx.conf /home/recharge/nginx1.18.0/nginx.conf
docker cp mynginx:/etc/nginx/conf.d /home/recharge/nginx1.18.0/conf
docker cp mynginx:/usr/share/nginx/html /home/recharge/nginx1.18.0/html
# Run container with volume mounts
docker run -d --name mynginx \
  -v /home/recharge/nginx1.18.0/nginx.conf:/etc/nginx/nginx.conf \
  -v /home/recharge/nginx1.18.0/logs:/var/log/nginx \
  -v /home/recharge/nginx1.18.0/html:/usr/share/nginx/html \
  -v /home/recharge/nginx1.18.0/conf/conf.d:/etc/nginx/conf.d \
  --privileged=true --net=host nginx:1.18.0

Basic Docker Commands for Nginx

# Restart container
docker restart my-nginx
# Reload Nginx inside container
docker exec -it my-nginx nginx -s reload

Common Command List

Pull image: docker pull nginx Run container: docker run -d -p 80:80 nginx Enter container: docker exec -it <em>container_name</em> /bin/bash View logs: docker logs -f <em>container_name</em> Restart container: docker restart <em>container_name</em> Remove container:

docker rm -f <em>container_name</em>

macOS Local Installation

# Using Homebrew
brew info nginx
brew install nginx
brew services restart nginx

With Homebrew, installing Nginx on macOS takes only three steps.

Q&A

Q1: Tomcat vs Nginx

Tomcat uses a blocking I/O (BIO) thread‑pool model, suitable for complex application logic. Nginx uses an event‑driven model, ideal for handling a large number of short‑lived, high‑concurrency requests such as static content and reverse proxy.

In the event‑driven model, a thread issues an I/O request and immediately proceeds to the next connection, avoiding thread blockage and reducing resource consumption.

Q2: Traffic Gateway, API Gateway, Service Registry

Traffic Gateway : e.g., Nginx, processes HTTP requests and forwards them to backend servers; essentially a web server.

API Gateway : Sits between the traffic gateway and services, performs routing based on paths, handles authentication, rate limiting, and can act as a unified entry point for APIs.

Service Registry : Used in micro‑service architectures; services register their IP and port, enabling client‑side load balancing without an HTTP gateway.

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.

DockerOperationsinstallation
Lin is Dream
Written by

Lin is Dream

Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.

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.