Comprehensive Nginx Installation, Configuration, and Usage Guide
This article provides a step‑by‑step tutorial on preparing a CentOS environment, installing Nginx via the LNMP package, explaining core concepts such as epoll, lightweight design and CPU affinity, and detailing configuration examples for basic settings, static resources, caching, CORS, anti‑hotlinking, and proxy services.
Before installing Nginx, ensure the CentOS 7.2 server meets four prerequisites: network connectivity, functional yum, firewalld disabled, and SELinux set to permissive. You can verify and adjust these settings with the following commands:
# View firewalld status
systemctl status firewalld.service
# Temporarily stop firewalld
systemctl stop firewalld.service
# View SELinux status
getenforce
# Temporarily set SELinux to permissive
setenforce 0Install basic development tools using yum:
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vimFor a quick setup, the author recommends the LNMP integrated package. Download and install it with:
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmpThe default installation directory is /usr/local. After installation, edit the main configuration file /usr/local/nginx/conf/nginx.conf to set basic parameters:
user nginx;
worker_processes 4; # usually match CPU cores
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
access_log /usr/local/nginx/logs/access.log main;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
error_page 500 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
}
# additional server blocks can be added here
}Nginx is an open‑source, high‑performance HTTP server and reverse proxy. Its popularity stems from three main advantages:
IO multiplexing (epoll) : Handles many concurrent connections efficiently.
Lightweight design : Only core HTTP modules are built‑in; extra functionality is added via dynamic modules.
CPU affinity : Workers can be bound to specific CPU cores to reduce cache misses.
Static resource serving is configured in the http block. Example for serving images, CSS, JS, and enabling gzip compression:
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
location ~* \.(gif|jpg|jpeg|png)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss image/jpeg image/gif image/png;
root /opt/app/code;
}
location ~* \.(html|htm)$ {
expires 12h;
}
}Browser caching can be controlled with the expires directive, reducing server load and latency. When a resource is unchanged, the server returns a 304 Not Modified response based on ETag and Last-Modified headers.
Cross‑origin resource sharing (CORS) is enabled by adding response headers in a location block:
location ~* \.(html|htm)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
# add_header Access-Control-Allow-Credentials true; # requires a specific origin
}To prevent hotlinking of images, use the valid_referers directive and block invalid referers:
location ~* \.(jpg|gif)$ {
valid_referers none blocked 127.0.0.1;
if ($invalid_referer) {
return 403;
}
}Nginx can act as a reverse proxy. A minimal proxy configuration looks like this:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
}The article concludes that the presented Nginx configurations cover installation, basic settings, static file handling, caching, CORS, anti‑hotlinking, and proxying, with future posts planned for load balancing and advanced caching techniques.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
