Boost Your Website Speed: Step‑by‑Step Nginx Installation and Configuration on Linux
This article explains how to install the high‑performance Nginx HTTP server on a Linux system, covers prerequisites, detailed installation commands, essential parameters, signal‑based control, practical configuration examples, static file handling, caching directives, and reverse‑proxy setup for dynamic content, enabling you to accelerate website access without altering existing site architecture.
Introduction
Nginx (pronounced "engine x") is a high‑performance HTTP and reverse‑proxy server that also supports IMAP/POP3/SMTP. Developed by Igor Sysoev for the Russian site Rambler.ru, it is released under a BSD‑style license and is known for stability, rich features, example configurations, and low resource consumption.
Market Share
According to the June 2008 NetCraft survey, over two million hosts were using Nginx, surpassing lighttpd and ranking fourth among web servers.
Prerequisites
Nginx does not support Windows; install on Linux, UNIX, BSD, etc.
It is only an HTTP/reverse‑proxy server; it does not process PHP, CGI, etc. directly.
Supports simple load balancing and fault tolerance.
Provides standard HTTP server features such as logging, compression, byte‑ranges, chunked responses, SSL, virtual hosts, and more.
Install PCRE (required library)
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7
./configure
make
make installInstall Nginx
Choose either the stable or development version and install to /opt/nginx:
wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
tar zxvf nginx-0.6.31.tar.gz
cd nginx-0.6.31
./configure --with-http_stub_status_module --prefix=/opt/nginx
make
make installThe --with-http_stub_status_module option enables the NginxStatus monitoring feature.
After installation, the /opt/nginx directory contains conf, html, logs, and sbin. The main configuration file is conf/nginx.conf. Ensure port 80 is free, then start Nginx with sbin/nginx. Accessing the server’s IP in a browser should display “Welcome to nginx!” indicating a successful installation.
Common Nginx Command‑Line Parameters
-c: Use an alternative configuration file. -t: Test configuration syntax without starting the server. -v: Show Nginx version. -V: Show version plus compile options.
Example test command:
sbin/nginx -t -c conf/nginx2.confControlling Nginx with Signals
Nginx can be controlled via Unix signals. Find the PID in logs/nginx.pid and send signals with kill -SIGNAL pid or killall -s SIGNAL nginx. For example, killall -s HUP nginx reloads the configuration.
Configuration Example
A practical nginx.conf is shown below (comments omitted for brevity):
user nobody;
worker_processes 4;
events {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
sendfile on;
tcp_nodelay on;
keepalive_timeout 65;
include gzip.conf;
upstream tomcats {
server 192.168.0.11:8080 weight=10;
server 192.168.0.11:8081 weight=10;
server 192.168.0.12:8080 weight=10;
server 192.168.0.12:8081 weight=10;
server 192.168.0.13:8080 weight=10;
server 192.168.0.13:8081 weight=10;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
location ~ ^/(WEB-INF)/ { deny all; }
location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
root /opt/webapp;
expires 24h;
}
location / {
proxy_pass http://tomcats;
include proxy.conf;
}
error_page 404 /html/404.html;
error_page 502 503 /html/502.html;
error_page 500 504 /50x.html;
location = /50x.html { root html; }
}
}Monitoring Nginx Status
With the stub_status location defined, accessing http://localhost/NginxStatus/ yields output such as:
Active connections: 70
server accepts handled requests 14553819 14553819 19239266
Reading: 0 Writing: 3 Waiting: 67Explanation:
Active connections – current active connections.
Server accepts handled requests – total connections, successful handshakes, and total requests.
Reading – connections where Nginx is reading client headers.
Writing – connections where Nginx is sending responses.
Waiting – idle keep‑alive connections.
Static File Handling
Use regular expressions to match static resources, e.g.: location ~ ^/images/ { root /opt/webapp/images; } For common file types:
location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ { root /opt/webapp; expires 24h; }The expires directive controls browser caching, with values such as expires 60s;, expires 24h;, expires max;, etc.
Dynamic Request Handling
Nginx forwards dynamic requests to backend servers via proxy_pass. A simple proxy example:
location / { proxy_pass http://localhost:8080; proxy_set_header X-Real-IP $remote_addr; }For a cluster of backend servers, define an upstream block (as shown earlier) and use proxy_pass http://tomcats;. Nginx distributes requests using a simple round‑robin algorithm and automatically handles node failures.
Conclusion
Although the Nginx package is small, it provides a complete set of modules for compression, hotlink protection, clustering, FastCGI, streaming, Memcached support, URL rewriting, and more. Combined with these modules, Nginx delivers performance that surpasses Apache and other HTTP servers, allowing you to accelerate website access without redesigning the existing site architecture.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
