Master Nginx on CentOS 6: Installation, Config, Multi‑Domain & 301 Redirects
This guide walks through installing Nginx on CentOS 6, configuring its main file, setting up multi‑domain virtual hosts, implementing 301 redirects, adding custom 404 pages, and blocking direct IP access, all with practical command examples and configuration snippets.
Nginx Installation
CentOS 6.x does not provide an Nginx package by default. Download the appropriate RPM from the official Nginx stable repository for CentOS 6 and install it: yum install nginx -y After installation, Nginx runs as a Linux service, controllable with commands such as service nginx start, service nginx stop, service nginx restart, and service nginx reload.
Nginx Configuration File
The default configuration file is /etc/nginx/nginx.conf. You can specify a different file with the -c option when starting Nginx.
To locate the active configuration file on an unfamiliar server, use commands like: nginx -t or
nginx -VMulti‑Domain Configuration
To serve multiple domains on different ports without exposing the ports to users, map them to port 80 using Nginx server blocks. Example: map www.525.life to internal port 8880 and admin.525.life to port 8881.
Configuration snippet (illustrated in the image) adds two server blocks listening on port 80 with proxy_pass directives to the respective internal ports.
After editing, reload Nginx:
nginx -s reloadSeparate Conf Files for Each Domain
Instead of a single file with many server blocks, place each domain’s configuration in its own .conf file (e.g., admin.conf, www.conf) under /data/nginx/conf/vhost and include them in nginx.conf: include /data/nginx/conf/vhost/*.conf; Make sure the include directive is inside the http { } block.
301 Redirect
To redirect non‑www requests to the www version (or any other canonical host), add a server block that returns a 301 status:
return 301 $scheme://www.example.com$request_uri;Add 404 Page
Define a custom 404 error page by adding error_page 404 /404.html; inside the appropriate server block and placing the 404.html file in the document root.
Block Direct IP Access
To prevent users from accessing the server via its raw IP address or unauthorized hostnames, place a server block at the top of the configuration that returns a 444 or 403 status for unmatched hosts: server { listen 80 default_server; return 444; } This ensures only defined domain names are served.
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.
