Master Nginx Reverse Proxy: Step‑by‑Step Setup with Real‑World Examples
This guide walks you through configuring Nginx as a reverse proxy on Linux, covering user creation, compilation, SSL support, proxy directives, troubleshooting, and a second example that routes traffic to two Tomcat services on different ports.
Introduction
In previous chapters we covered basic Nginx concepts. This article demonstrates two practical reverse‑proxy scenarios.
Nginx Reverse Proxy Example 1
1. Desired Result
Visiting www.test.com redirects to the Baidu homepage.
2. Preparation
(1) Install Nginx on a Linux system
1) Create a user for Nginx
useradd -M -s /sbin/nologin nginx
2) Download Nginx source
wget http://nginx.org/download/nginx-1.18.0.tar.gz
3) Extract
tar -xf nginx-1.18.0.tar.gz -C /usr/src
4) Configure
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module
5) Build and install
make -j 4 && make install
6) Create a symlink for easy access
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
7) Verify version
nginx -v(2) Edit the Nginx configuration file
vim /usr/local/nginx/conf/nginx.conf
server_name – the domain to listen for
proxy_pass URL – forward requests to the specified URL
(3) Restart Nginx
If an error appears, SSL support may be missing. Re‑configure with --with-http_ssl_module and rebuild.
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_moduleRestart Nginx:
nginx -s stop && nginx4. Access Test
If you encounter an error about a non‑existent IP address, enable non‑local bind:
echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
sysctl -p5. Process Analysis
Nginx Reverse Proxy Example 2
1. Desired Result
Use Nginx to route requests based on path to different backend services:
Port 9001 receives traffic.
Request http://10.43.187.251:9001/edu/ forwards to 127.0.0.1:8080.
Request http://10.43.187.251:9001/vod/ forwards to 127.0.0.1:8081.
2. Preparation
(1) Deploy two Tomcat servers
1) Download JDK
wget http://big.gxkjbg.com:8080/201704/tools/jdk-linux-x64.tar.gz
tar -zxvf jdk-linux-x64.tar.gz -C /usr/src/
2) Set environment variables in /etc/profile
export JAVA_HOME=/usr/src/jdk1.8.0_131
export JAVA_BIN=$JAVA_HOME/bin
export PATH=$JAVA_BIN:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
source /etc/profile
3) Verify Java version
java -version(2) Install and start two Tomcat instances
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.54/bin/apache-tomcat-8.5.54.tar.gz
mkdir /usr/src/tomcat8080 /usr/src/tomcat8081
tar -zxvf apache-tomcat-8.5.54.tar.gz -C /usr/src/tomcat8080
tar -zxvf apache-tomcat-8.5.54.tar.gz -C /usr/src/tomcat8081
# Start first Tomcat (port 8080)
cd /usr/src/tomcat8080/apache-tomcat-8.5.54/bin
./startup.sh
# Change second Tomcat port to 8081
vim /usr/src/tomcat8081/apache-tomcat-8.5.54/conf/server.xml # modify Connector port to 8081
# Start second Tomcat
cd /usr/src/tomcat8081/apache-tomcat-8.5.54/bin
./startup.sh(3) Verify both Tomcat services are running
3. Create Test Pages
# On Tomcat1
cd /usr/src/tomcat8080/apache-tomcat-8.5.54/webapps
mkdir edu
echo "<h1>8080!</h1>" > edu/a.thml
# On Tomcat2
cd /usr/src/tomcat8081/apache-tomcat-8.5.54/webapps
mkdir vod
echo "<h1>8081!</h1>" > vod/a.thml4. Edit Nginx Configuration
vim /usr/local/nginx/conf/nginx.conf
5. Final Test
The Nginx reverse‑proxy configuration successfully routes traffic to both Tomcat services.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
