Backend Development 12 min read

Comprehensive Nginx Configuration Guide: Basics, Optimization, and Deployment

This article provides a detailed walkthrough of Nginx configuration, explaining the global, events, and http blocks, offering simple deployment steps, and covering common optimizations such as history‑mode handling, reverse proxy setup, gzip compression, maintenance pages, multi‑site hosting, static‑dynamic separation, and essential command‑line operations.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Comprehensive Nginx Configuration Guide: Basics, Optimization, and Deployment

1. nginx.conf configuration – The core configuration file is divided into three sections: the global block, the events block, and the http block. Example configuration:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile       on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

The global block sets overall server parameters such as the number of worker processes, PID file location, and log paths.

The events block controls connection handling, e.g., worker_connections 1024; defines the maximum connections per worker.

The http block contains most functional directives, including MIME types, logging, proxy settings, and server definitions.

2. Overall understanding – A server block can contain multiple location blocks, each matching request URIs to handle static files, proxy to back‑ends, or return error pages. The configuration is visualized in accompanying diagrams.

3. Simplest deployment – To serve a static site, set server_name and root to point to the site directory; Nginx will return index.html for incoming requests.

4. Nginx optimizations

History‑mode 404 handling – Prevents page refresh 404 errors for SPA routes:

location / {
    try_files $uri $uri/ /index.html;
}

Reverse proxy – Forward API requests to a back‑end service:

# API endpoint
location /police/ {
    proxy_pass   http://192.168.1.182:8852/police/;
    proxy_redirect default;
    proxy_http_version 1.1;
    proxy_connect_timeout   60;
    proxy_send_timeout      60;
    proxy_read_timeout      90;
}

Gzip compression – Reduce bandwidth for static assets:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
gzip_static on;
gzip_comp_level 6;

Maintenance page – Enable a temporary maintenance page by uncommenting a rewrite rule.

# rewrite ^(.*)$ /maintainace.html break;

Multiple sites on one IP – Define separate server blocks with different ports:

# Blog site
server {
    listen 8080;
    root /data/www/hexo;
    index index.html;
    location / {
        try_files $uri $uri/ /index.html;
    }
}

# Live streaming site
server {
    listen 8081;
    root /data/www/geov;
    index index.html;
    location / {}
}

Static‑dynamic separation – Serve static resources directly and proxy dynamic requests to back‑ends, improving performance and allowing front‑end and back‑end development to proceed in parallel.

Example static location rules:

location /image/ { root /var/filecenter/; }
location /static/ { root /var/filecenter/; }
location ~ .*\\.(html|css|js|png|jpg|gif)$ { root /path/to/static; }

Other optimizations – Additional tuning can be applied based on performance testing, but the basics above cover most common scenarios.

5. Basic Nginx commands

Install: yum install nginx

Check process: netstat -anput | grep nginx

Start: nginx

Reload configuration: nginx -s reload

Stop quickly: nginx -s stop

Graceful shutdown: nginx -s quit

Test configuration: nginx -t

Understanding the structure and purpose of each block enables efficient configuration, troubleshooting, and scaling of web services with Nginx.

backendoptimizationDeploymentconfigurationnginxReverse Proxy
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.