Master Nginx: From Basics to Advanced Configuration and Optimization

This comprehensive guide walks you through Nginx fundamentals, its performance advantages, installation methods, detailed configuration file structure, virtual host setup, logging strategies, and an overview of essential modules, empowering you to deploy, tune, and manage a high‑performance web server.

Raymond Ops
Raymond Ops
Raymond Ops
Master Nginx: From Basics to Advanced Configuration and Optimization

Nginx Basics

Nginx is a lightweight, high‑performance web server and reverse‑proxy that excels at handling massive concurrent connections, making it a popular choice for sites like Baidu, JD.com, and Tencent.

Advantages of Nginx

Low memory footprint

Excellent concurrency handling

Built‑in reverse‑proxy and load‑balancing

Supports HTTP, HTTPS, SMTP, and IMAP/POP3

Installation

You can install Nginx via package managers (yum) or compile from source to add custom modules.

# yum install nginx
# rpm -ql nginx | grep log

Configuration File Structure

The main configuration /etc/nginx/nginx.conf is divided into global, events, and http sections. Inside http you define server blocks, each containing location blocks.

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    keepalive_timeout 65;
    include /etc/nginx/conf.d/*.conf;
}

Virtual Hosts

Each server block defines a virtual host with directives such as listen, server_name, root, and index. Multiple virtual hosts can coexist by placing separate .conf files in /etc/nginx/conf.d/.

server {
    listen 80;
    server_name www.example.com;
    root /var/www/example;
    index index.html index.htm;
}

Logging

Nginx records access and error information using access_log and error_log. The log_format directive defines the log fields (IP, user, time, request, status, bytes, referrer, user‑agent, forward‑for). Log rotation is handled by /etc/logrotate.d/nginx.

# tail -f /var/log/nginx/access.log
# tail -f /var/log/nginx/error.log

Key Modules Overview

ngx_http_stub_status_module – provides connection statistics via stub_status.

ngx_http_random_index_module – serves a random index file for “micro‑updates”.

ngx_http_sub_module – on‑the‑fly content replacement with sub_filter.

ngx_http_gzip_module – compresses responses to save bandwidth.

ngx_http_limit_req_module and ngx_http_limit_conn_module – rate‑limit and connection‑limit per IP.

ngx_http_referer_module – basic hotlink protection.

ngx_http_auth_basic_module – simple username/password protection.

ngx_http_autoindex_module – directory listing.

Common Management Commands

# nginx -t            # test configuration syntax
# systemctl restart nginx   # reload changes
# nginx -V            # show compile options
# kill -USR2 <pid>    # graceful upgrade (for compiled installs)

By mastering these concepts you can deploy Nginx, fine‑tune performance, secure your sites, and extend functionality with additional modules.

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.

ConfigurationloggingModules
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

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.