Operations 14 min read

Master Nginx: Installation, Configuration, and Essential Commands

This guide walks you through installing Nginx on CentOS, compiling from source, configuring core settings, managing workers, setting up virtual hosts, and using common directives such as gzip, proxy, and access control to optimize performance and security.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Nginx: Installation, Configuration, and Essential Commands

Nginx is a lightweight web server, reverse proxy, and mail proxy released under a BSD‑like license. It uses little memory and offers strong concurrency, which is why major Chinese sites such as Baidu, JD, Sina, NetEase, Tencent and Taobao rely on it.

Why Use Nginx?

Compared with Apache, Nginx consumes fewer resources, supports more concurrent connections, and is favored by virtual‑host providers. It can handle up to 50 000 concurrent connections and uses efficient event models like epoll and kqueue.

Nginx also serves as a load balancer, can proxy Rails, PHP, and other back‑end services, and is written in C, giving it lower CPU overhead than Perlbal.

Configuration Simplicity vs. Apache Complexity

Nginx starts easily and can run 24/7 without needing frequent restarts, allowing seamless software upgrades while serving traffic.

Static content handling is more than three times faster than Apache, while dynamic content typically requires a back‑end server.

Core Architectural Difference

Apache uses a synchronous multi‑process model (one process per connection); Nginx uses an asynchronous event‑driven model where many connections share a few worker processes.

Typical Use Cases

Nginx excels at serving static requests with low CPU and memory usage, while Apache is better suited for dynamic request processing. In practice, front‑ends use Nginx as a reverse proxy and Apache for dynamic back‑ends.

Basic Usage

System platform: CentOS 6.6 64‑bit.

Installation Steps

1. Install compilation tools and libraries.

2. Install PCRE (required for rewrite support).

3. Extract the PCRE package, enter the directory, and compile.

4. Download Nginx source (e.g., nginx‑1.6.2.tar.gz) and extract.

5. Compile and install Nginx.

6. Verify the installation with nginx -v.

Configuration

Create a dedicated user (e.g., www) for running Nginx.

Replace /usr/local/webserver/nginx/conf/nginx.conf with a custom configuration (shown in the original article).

Test the configuration with nginx -t.

Starting Nginx

Start the server using the appropriate command.

Common Directives

Global (main) settings worker_processes – number of worker processes, usually set to the CPU core count. worker_cpu_affinity – binds workers to specific CPU cores for performance. worker_connections – maximum simultaneous connections per worker. worker_rlimit_nofile – limits the number of open files. use epoll – enables the efficient epoll event model on Linux.

HTTP server settings sendfile on – enables zero‑copy file transmission. keepalive_timeout 65 – sets the keep‑alive timeout in seconds. client_max_body_size 10m – limits upload size. gzip on – enables gzip compression with tunable parameters such as gzip_min_length, gzip_buffers, and gzip_comp_level.

Proxy module

proxy_connect_timeout 60
proxy_read_timeout 60
proxy_buffer_size 4k
proxy_buffers 4 32k
proxy_busy_buffers_size 64k
proxy_max_temp_file_size 0

– disables temporary files.

Server block (virtual host) listen – defines the listening port. server_name – specifies the host name.

Location block root /var/www/html – sets the document root. index index.jsp index.html index.htm – defines default index files. proxy_pass http://backend – forwards requests to a back‑end server.

Access control directives allow / deny restrict IP access. autoindex on – enables directory listing, with optional autoindex_exact_size off and autoindex_localtime on.

This tutorial provides a complete workflow for installing, configuring, and optimizing Nginx on a Linux server.

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.

OperationsLinuxNGINXWeb serverServer Configuration
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.