Master Nginx: Install, Configure, and Optimize Reverse Proxy on CentOS
This guide walks you through installing Nginx on CentOS 7.5, configuring it as a high‑performance reverse proxy, setting up environment variables, managing common commands, and implementing practical scenarios such as request forwarding, Tomcat integration, load balancing, static file handling, and sub‑domain routing.
Nginx Overview
Nginx (pronounced "engine‑x") is a high‑performance web server and reverse‑proxy written in C by Igor Sysoev. It also functions as an IMAP/POP3/SMTP proxy. Thanks to its stability, rich feature set, example configuration files, and low resource consumption, Nginx powers about 12.18% of active websites worldwide (≈22.2 million sites).
What is a reverse proxy? It receives client requests from the Internet, forwards them to internal servers, and returns the server responses to the clients, effectively hiding the real backend servers.
Operating Environment
CentOS 7.5 64‑bit
nginx‑1.12.1.tar.gz (download URL provided)
apache‑tomcat‑8.5.15.tar.gz (download URL provided)
Nginx Installation
1. Install compilation tools and libraries
yum -y install make zlib zlib-devel gcc gcc++ libtool openssl openssl-develNote: zlib provides compression; gcc is required to compile the source code; OpenSSL enables HTTPS support.
Install PCRE
yum -y install pcre pcre-develPCRE is used by Nginx’s HTTP module for regular‑expression processing.
2. Install Nginx
1. Download Nginx wget http://nginx.org/download/nginx-1.12.1.tar.gz 2. Extract the package tar -zxvf nginx-1.12.1.tar.gz 3. Enter the source directory cd nginx-1.12.1 4. Compile and install
# ./configure --prefix=/usr/local/nginx # make # make installThe simple compilation method installs most common modules; additional modules can be enabled via ./configure --help.
Key configure options: --prefix=PATH: installation directory (default /usr/local/nginx). --conf-path=PATH: location of nginx.conf (default prefix/conf/nginx.conf).
Verify installation: /usr/local/nginx/sbin/nginx -v →
nginx version: nginx/1.12.13. Configure system environment variable (optional)
Edit /etc/profile and add: export PATH=/usr/local/nginx/sbin:$PATH Then reload: source /etc/profile Now the nginx command is available globally.
Nginx Configuration
nginx.conf structure
The main blocks are events, http, and multiple server blocks (virtual hosts).
Example directives: worker_processes 1; – number of worker processes (usually equal to CPU cores). worker_connections 1024; – maximum connections per worker. listen 80; – port to listen on. server_name localhost; – host name. root /usr/local/nginx/html; – document root. index index.html index.htm; – default files. proxy_pass http://backend; – forward requests.
Common optional settings (commented out in the source): error logging, access logging, gzip compression, SSL configuration, and load‑balancing parameters.
Check configuration and start Nginx
Check syntax: nginx -t Start Nginx: nginx Verify the process: ps -ef | grep nginx Result screenshots are shown below:
Access the site in a browser – if the page does not load, ensure port 80 is open.
Common Nginx Commands
nginx– start nginx -v – show version nginx -t – test configuration nginx -s reload – reload config nginx -s reopen – restart nginx -s stop – fast stop nginx -s quit – graceful stop
Practical Use Cases
1. Simple request forwarding
Assume the external IP is 192.168.1.5 . Configure a server block:
server {
listen 80;
server_name 192.168.1.5;
location / {
root html;
index index.html index.htm;
proxy_pass http://www.baidu.com;
}
}Reload with nginx -s reload. Browsing to 192.168.1.5 redirects to Baidu.
2. Forward requests to Tomcat
Install and start Tomcat (port 8080). Then modify Nginx:
server {
listen 80;
server_name 192.168.1.5;
location / {
root html;
index index.html index.htm;
proxy_pass http://192.168.1.5:8080;
}
}After nginx -s reload, accessing 192.168.1.5 shows Tomcat’s homepage.
3. Load balancing
Run two Tomcat instances on different ports (e.g., 8080 and 8081). Define an upstream block:
http {
upstream tomcat_server {
server localhost:8080 weight=1;
server 192.168.1.5:8081 weight=5;
}
server {
listen 80;
server_name 192.168.1.5;
location / {
proxy_pass http://tomcat_server;
}
}
}The weight attribute controls traffic distribution; higher weight means more requests.
4. Separate static files from dynamic content
Use location blocks with regex to send JSP to Tomcat while serving static assets via Nginx:
location ~\.jsp$ {
proxy_pass http://tomcat_server;
}
location / {
root html;
index index.html index.htm;
}Now http://192.168.1.5/index.jsp is handled by Tomcat, while other files are served directly.
5. Sub‑domain configuration
Create a conf.d directory and add a file news.yourdomain.com.conf:
upstream news.yourdomain.com {
server localhost:2020;
}
server {
listen 80;
server_name news.yourdomain.com;
location / {
proxy_pass http://news.yourdomain.com;
}
}Reload Nginx and the sub‑domain will route to the specified backend.
These examples cover the most basic Nginx installation, configuration, and usage scenarios.
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.
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.
