Nginx 服务器反向代理实战指南
文章目录一、反向代理概述1. 正向代理为客户端赋能的中间层2. 反向代理为服务器守护的中间层3. 反向代理的技术定义4. 反向代理的核心能力二、Location 配置详解1. 配置语法基础2. 五大 Location 匹配规则规则一精确匹配规则二前缀匹配^~规则三正则匹配~ / ~*规则四普通前缀匹配无符号规则五通用匹配/3. proxy_pass 路径重构机制4. 综合配置示例5. 请求匹配流程三、反向代理实践环境搭建1. 环境架构2. 基础网络配置3. 配置 /etc/hosts4. 后端 Nginx 服务器部署5. 后端 Nginx 服务器部署五、反向代理基础实践代理本地实践六、总结一、反向代理概述1. 正向代理为客户端赋能的中间层正向代理Forward Proxy的核心职责是代理客户端发起请求其本质在于隐藏客户端的真实身份。生活化类比——代买奶茶假设你客户端因为某些原因无法亲自前往奶茶店服务器于是委托室友正向代理代为购买。奶茶店只知道是室友来买的并不知道背后真正的消费者是你。这就是正向代理的核心特征代理代表客户端。正向代理 替客户端办事隐藏客户端正向代理生活案例 代买奶茶你客户端室友正向代理奶茶店服务器你懒得下楼/小区不让出去让室友帮你去买奶茶。奶茶店只知道是你室友来买的 不知道背后真正买的人是你。◉这就是正向代理 代理代表客户端。2. 反向代理为服务器守护的中间层反向代理Reverse Proxy的核心职责是代理后端服务器接收请求其本质在于隐藏后端服务器的真实信息。生活化类比——餐厅前台点餐你走进餐厅只需在前台点餐客户端与前台交互无需关心后厨哪位厨师真实服务器为你烹饪。前台将订单传递给后厨做好后再由前台端给你。你全程只需面对前台无需知晓背后是哪位厨师在操作。这就是反向代理的核心特征代理代表服务器。反向代理生活案例 餐厅前台点餐你客户端前台小姐姐反向代理后厨厨师们真实服务器你进店只跟前台点餐不用管是哪个厨师做的。前台把单子传给后厨做好了再端给你。你全程只面对前台 不知道、也不用关心背后是哪个厨师在干活。②这就是反向代理 代理代表服务器。3. 反向代理的技术定义反向代理Reverse Proxy是指代理外部用户的请求到内部指定的后端服务器并将处理结果返回给用户。客户端不直接与后端服务器通信而是通过反向代理服务器进行中转从而有效隐藏后端服务器的 IP 地址。4. 反向代理的核心能力反向代理的主要价值体现在以下四个维度负载均衡Nginx 可将传入的请求智能分发至多个后端服务器均衡各节点的负载压力显著提升系统的整体性能与可靠性。缓存功能Nginx 可对静态文件或动态页面进行缓存有效减轻后端服务器的处理压力大幅缩短响应延迟。动静分离将动态生成的内容如 PHP、Python、Node.js 等与静态资源如 HTML、CSS、JavaScript、图片、视频等分别部署在不同的服务器或路径上实现资源的最优调度。多站点代理Nginx 支持代理多个域名或虚拟主机将不同请求转发至不同的后端服务器实现多站点共享同一端口的灵活部署方案。二、Location 配置详解1. 配置语法基础Nginx 通过location匹配规则与proxy_pass反向代理指令的配合实现完整的反向代理功能。其匹配本质可概括为URL 路径匹配 → 命中对应规则 → 转发至指定后端地址。location定义匹配路径proxy_pass指定后端服务地址http{# 后端服务可配置 upstream 集群推荐支持负载均衡upstream backend_nginx{nginx192.168.1.100:8080;# 后端服务1nginx192.168.1.101:8080;# 后端服务2多节点自动轮询负载均衡}server{listen80;# Nginx 监听端口server_name localhost;# 访问域名/IP# 1. 匹配所有请求兜底规则location /{proxy_pass http://backend_nginx;# 转发至 upstream 集群# 必加的反向代理核心参数传递客户端真实信息、适配后端服务proxy_set_header Host$host;# 传递客户端访问的域名proxy_set_header X-Real-IP$remote_addr;# 传递客户端真实IPproxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;#传递IP链路proxy_set_header X-Forwarded-Proto$scheme;# 传递请求协议 http/https}# 2. 匹配特定路径如 /api 开头的请求单独转发location /api/{proxy_pass http://192.168.1.102:9090/;# 后端地址末尾带 /会剔除匹配的/api/proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;}}}2. 五大 Location 匹配规则Nginx 的 location 匹配遵循以下优先级体系优先级匹配类型语法说明1最高精确匹配location /path { ... }URL 必须与路径完全一致2前缀匹配^~location ^~ /path { ... }URL 以指定路径开头即命中跳过正则匹配3正则匹配~ / ~*location ~ /regex { ... }URL 符合正则表达式即命中按定义顺序匹配4普通前缀匹配location /path { ... }URL 以指定路径开头即命中多规则按路径最长优先5最低通用匹配/location / { ... }兜底规则所有未命中请求均匹配后端匹配逻辑URL 路径 → 按 location 优先级命中规则 → 由规则内的 proxy_pass 转发至对应后端优先级精确匹配 前缀匹配^~ 正则匹配/* 普通前缀 兜底/URL 重构关键 proxy_pass 末尾是否带 /决定是否剔除 location 匹配的路径前缀。规则一精确匹配语法location /path { ... }逻辑仅当请求 URL 与/path完全一致时命中优先级最高。示例# 仅匹配 http://localhost/login不匹配 /login?a1、/login/xxxlocation/login{proxy_pass http://backend_login:8080;}规则二前缀匹配^~语法location ^~ /path { ... }逻辑URL 以/path开头即命中优先级仅次于精确匹配会跳过正则匹配。用途优先匹配静态资源如/static、/img或特定业务路径避免被正则规则拦截。示例# 匹配所有 /static 开头的请求如 /static/css/main.css、/static/img/1.jpglocation ^~ /static/{proxy_pass http://backend_static:80;}规则三正则匹配~ / ~*语法区分大小写location ~ /regex { ... }如/API不匹配/api规则不区分大小写location ~* /regex { ... }如/API、/api均匹配逻辑URL 符合正则表达式即命中优先级低于前缀匹配^~多个正则规则按定义顺序匹配先命中先生效。示例# 匹配所有 .jpg、.png、.gif 结尾的图片请求不区分大小写location ~*\.(jpg|png|gif)${proxy_pass http://backend_img:80;}规则四普通前缀匹配无符号语法location /path { ... }逻辑URL 以/path开头即命中优先级低于正则匹配多个普通前缀规则按路径最长优先命中。示例# 规则1匹配 /api/xxx路径长度3location /api/{proxy_pass http://backend_api:9090;}# 规则2匹配 /api/user/xxx路径长度7比规则1长优先命中location /api/user/{proxy_pass http://backend_user:9090;}规则五通用匹配/语法location / { ... }逻辑所有未被上述规则命中的请求都会匹配此规则兜底优先级最低。用途通常作为全局反向代理转发所有默认请求到主后端服务。3. proxy_pass 路径重构机制proxy_pass末尾是否携带/会直接改变转发到后端的 URL 路径这是后端匹配后URL 重构的核心机制。场景一proxy_pass 末尾带/逻辑转发时会剔除 location 匹配的路径前缀将剩余路径拼接在后端地址后。示例# location 匹配 /api/proxy_pass 末尾带 /location /api/{proxy_pass http://192.168.1.102:9090/;}# 实际转发逻辑# 客户端请求 http://localhost/api/user/list# → 后端接收 http://192.168.1.102:9090/user/list场景二proxy_pass 末尾不带/逻辑转发时会保留 location 匹配的路径前缀直接拼接在后端地址后。示例# location 匹配 /api/proxy_pass 末尾不带 /location /api/{proxy_pass http://192.168.1.102:9090;}# 实际转发逻辑# 客户端请求 http://localhost/api/user/list# → 后端接收 http://192.168.1.102:9090/api/user/list4. 综合配置示例以下是一个完整的 Nginx 反向代理配置文件涵盖了五种匹配规则的综合运用http{upstream backend_main{server192.168.1.200:8080;}upstream backend_api{server192.168.1.201:9090;}upstream backend_static{server192.168.1.202:80;}upstream backend_login{server192.168.1.203:8080;}server{listen80;server_name localhost;# 1. 精确匹配仅 /login → 后端 login 服务location/login{proxy_pass http://backend_login;proxy_set_header Host$host;}# 2. 前缀匹配/static/ 开头 → 后端静态服务跳过正则location ^~ /static/{proxy_pass http://backend_static/;proxy_set_header Host$host;}# 3. 正则匹配图片后缀 → 后端静态服务location ~*\.(jpg|png|gif)${proxy_pass http://backend_static;proxy_set_header Host$host;}# 4. 普通前缀/api/ 开头 → 后端 api 服务location /api/{proxy_pass http://backend_api/;proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;}# 5. 兜底匹配所有未命中的请求 → 主后端服务location /{proxy_pass http://backend_main;proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;}}}5. 请求匹配流程客户端请求 URL命中的 Location 规则转发至后端的 URL对应后端服务http://localhost/login /loginhttp://192.168.1.203:8080/loginbackend_loginhttp://localhost/static/css/main.css^~ /static/http://192.168.1.202:80/css/main.cssbackend_statichttp://localhost/img/1.jpg~* .(jpgpnggif)$http://localhost/api/user/info/api/http://192.168.1.201:9090/user/infobackend_apihttp://localhost/index/http://192.168.1.200:8080/indexbackend_main三、反向代理实践环境搭建1. 环境架构本实践环境基于 CentOS 7 搭建包含以下节点主机名IP 地址服务器角色client.laogao.cloud10.1.8.11客户端测试服务器proxy.laogao.cloud10.1.8.20Nginx 代理服务器nginx1.laogao.cloud10.1.8.21Nginx Web 服务器nginx2.laogao.cloud10.1.8.22Nginx Web 服务器nginx3.laogao.cloud10.1.8.23Nginx Web 服务器2. 基础网络配置在各节点上执行以下命令完成主机名与 IP 配置# client 节点hostnamectl set-hostname client.laogao.cloud nmcli connection modify ens33 ipv4.method manual ipv4.addresses10.1.8.11/24 ipv4.gateway10.1.8.2 ipv4.dns10.1.8.2 autoconnectyesnmcli connection up ens33# proxy 节点hostnamectl set-hostname client.laogao.cloud nmcli connection modify ens33 ipv4.method manual ipv4.addresses10.1.8.20/24 ipv4.gateway10.1.8.2 ipv4.dns10.1.8.2 autoconnectyesnmcli connection up ens33# nginx1 节点hostnamectl set-hostname client.laogao.cloud nmcli connection modify ens33 ipv4.method manual ipv4.addresses10.1.8.21/24 ipv4.gateway10.1.8.2 ipv4.dns10.1.8.2 autoconnectyesnmcli connection up ens33# nginx2 节点hostnamectl set-hostname client.laogao.cloud nmcli connection modify ens33 ipv4.method manual ipv4.addresses10.1.8.22/24 ipv4.gateway10.1.8.2 ipv4.dns10.1.8.2 autoconnectyesnmcli connection up ens33# nginx3 节点hostnamectl set-hostname client.laogao.cloud nmcli connection modify ens33 ipv4.method manual ipv4.addresses10.1.8.23/24 ipv4.gateway10.1.8.2 ipv4.dns10.1.8.2 autoconnectyesnmcli connection up ens333. 配置 /etc/hosts在所有节点上执行以下配置# 所有节点[root所有节点 ~]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6############ proxy ##################10.1.8.11 client.laogao.cloud client10.1.8.20 www.laogao.cloud www10.1.8.20 proxy.laogao.cloud proxy10.1.8.21 nginx1.laogao.cloud nginx110.1.8.22 nginx2.laogao.cloud nginx210.1.8.23 nginx3.laogao.cloud nginx34. 后端 Nginx 服务器部署在 proxy、nginx1、nginx2、nginx3 节点上执行以下操作# 安装 EPEL 源[rootproxy,nginx1,nginx2,nginx3 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo# 安装 Nginx[rootproxy,nginx1,nginx2,nginx3 ~]# yum -y install nginx# 启动并启用 Nginx 服务[rootproxy,nginx1,nginx2,nginx3 ~]# systemctl enable nginx --now# 配置防火墙[rootproxy,nginx1,nginx2,nginx3 ~]# firewall-cmd --add-servicehttp --permanent[rootproxy,nginx1,nginx2,nginx3 ~]# firewall-cmd --add-servicehttp为各后端节点准备主页内容# nginx1 节点[rootnginx1 ~]# echo Welcome to $(hostname) /usr/share/nginx/html/index.html# nginx2 节点[rootnginx2 ~]# echo Welcome to $(hostname) /usr/share/nginx/html/index.html# nginx3 节点[rootnginx3 ~]# echo Welcome to $(hostname) /usr/share/nginx/html/index.html客户端验证各后端节点[rootnginx-client ~]# curl http://nginx1.laogao.cloud/Welcome to nginx1.laogao.cloud[rootnginx-client ~]# curl http://nginx2.laogao.cloud/Welcome to nginx2.laogao.cloud[rootnginx-client ~]# curl http://nginx3.laogao.cloud/Welcome to nginx3.laogao.cloud5. 后端 Nginx 服务器部署# 准备主页-代理节点[rootproxy ~]# echo Welcome to www.laogao.cloud /usr/share/nginx/html/index.html[rootproxy ~]# mkdir /var/nginx[rootproxy ~]# echo Hello, Nginx /var/nginx/index.html[rootproxy ~]# echo Hello, laogao /var/nginx/test.txt[rootproxy ~]# cp /usr/share/nginx/html/nginx-logo.png /var/nginx/ [rootproxy ~]# ls /var/nginx/index.html nginx-logo.png test.txt[rootproxy ~]# vim /etc/nginx/conf.d/proxy.confserver{listen80;server_name www.laogao.cloud;# 匹配根位置location /{root /var/nginx;index index.html;}}# 重新加载nginx配置[rootproxy ~]# nginx -s reload# 测试[rootnginx-client ~]# curl http://www.laogao.cloud/ Hello, Nginx[rootnginx-client ~]# curl http://www.laogao.cloud/test.txt Hello, laogao访问 www.laogao.cloud/nginx-logo.png系统会返回以下页面五、反向代理基础实践代理本地实践环境准备[rootproxy ~]# mkdir /var/nginx/nginx{1,2}[rootproxy ~]# echo Hello, Im here /var/nginx/nginx1 /var/nginx/nginx1/index.html[rootproxy ~]# echo Hello, Im here /var/nginx/nginx2 /var/nginx/nginx2/index.html[rootproxy ~]# mkdir /var/nginx{1,2}[rootproxy ~]# echo Hello, Nginx1 /var/nginx1/index.html[rootproxy ~]# echo Hello, Nginx2 /var/nginx2/index.html [rootproxy ~]# tree /var/nginx*/var/nginx ├── index.html ├── nginx1 │ └── index.html ├── nginx2 │ └── index.html ├── nginx-logo.png └── test.txt /var/nginx1 └── index.html /var/nginx2 └── index.html2directories,7files[rootproxy ~]# \forpath1inwww{1..2}doforpath2innginx{1..2}domkdir-p/var/$path1/$path2echoHello, Im here /var/$path1/$path2/var/$path1/$path2/index.htmldonedone[rootproxy ~]# tree /var/www*/var/www1 ├── nginx1 │ └── index.html └── nginx2 └── index.html /var/www2 ├── nginx1 │ └── index.html └── nginx2 └── index.html4directories,4files基本测试[rootnginx-client ~]# curl http://www.laogao.cloud/ Hello, Nginx# 显示结果是目录/var/nginx/nginx1中内容[rootnginx-client ~]# curl http://www.laogao.cloud/nginx1/ Hello, Im here /var/nginx/nginx1# 显示结果是目录/var/nginx/nginx2中内容[rootnginx-client ~]# curl http://www.laogao.cloud/nginx2/ Hello, Im here /var/nginx/nginx2实践 1无符号匹配访问测试[rootproxy ~]# vim /etc/nginx/conf.d/proxy.confserver{listen80;server_name www.laogao.cloud;# 匹配根位置location /{root /var/nginx;index index.html;}# 匹配/nginx1时/var目录下找nginx1完整路径是/var/nginx1location /nginx1{root /var;# 等效于下面的 alias 语句必须使用绝对路径# alias /var/nginx1;index index.html;}}# 重新加载nginx配置[rootproxy ~]# nginx -s reload# 访问测试# nginx1 后面必须添加 / 符号[rootnginx-client ~]# curl http://www.laogao.cloud/nginx1/ Hello, Nginx1# 显示结果是目录/var/nginx1中内容# nginx2 后面必须添加 / 符号[rootnginx-client ~]# curl http://www.laogao.cloud/nginx2/ Hello, Im here /var/nginx/nginx2# 显示结果是目录/var/nginx/nginx2中内容实验结果无符号匹配的优先级高于默认的/。实践 2正则表达式匹配访问测试实验结果正则表达式匹配的优先级高于无符号匹配。[rootproxy ~]# vim /etc/nginx/conf.d/proxy.confserver{listen80;server_name www.laogao.cloud;# 匹配根位置location /{root /var/nginx;index index.html;}# 匹配/nginx1时/var目录下找nginx1完整路径是/var/nginx1location /nginx1{root /var;# 等效于下面的 alias 语句必须使用绝对路径# alias /var/nginx1;index index.html;}# 正则表达式匹配 /nginx.*location ~ /nginx.*{root /var/www1;index index.html;}}# 重新加载nginx配置[rootproxy ~]# nginx -s reload# nginx1 后面必须添加 / 符号[rootnginx-client ~]# curl http://www.laogao.cloud/nginx1/ Hello, Im here /var/www1/nginx1# 显示结果是目录/var/www1/nginx1中内容# nginx2 后面必须添加 / 符号[rootnginx-client ~]# curl http://www.laogao.cloud/nginx2/ Hello, Im here /var/www1/nginx2# 显示结果是目录/var/www1/nginx2中内容实践 3精确匹配访问测试实验结果精确匹配的优先级高于正则表达式。[rootproxy ~]# vim /etc/nginx/conf.d/proxy.confserver{listen80;server_name www.laogao.cloud;# 匹配根位置location /{root /var/nginx;index index.html;}# 匹配/nginx1时/var目录下找nginx1完整路径是/var/nginx1location /nginx1{root /var;# 等效于下面的 alias 语句必须使用绝对路径# alias /var/nginx1;index index.html;}# 正则表达式匹配 /nginx.*location ~ /nginx.*{root /var/www1;index index.html;}# 精确匹配location/nginx2/index.html{root /var/www2;index index.html;}}# 重新加载nginx配置[rootproxy ~]# nginx -s reload# nginx1 后面必须添加 / 符号[rootnginx-client ~]# curl http://www.laogao.cloud/nginx1/ Hello, Im here /var/www1/nginx1# 显示结果是目录/var/www1/nginx1中内容# nginx2 后面必须添加 / 符号[rootnginx-client ~]# curl http://www.laogao.cloud/nginx2/ Hello, Im here /var/www2/nginx2# 显示结果是目录/var/www2/nginx2中内容六、总结Nginx 作为业界领先的高性能 HTTP 服务器与反向代理工具凭借其事件驱动的异步架构在处理高并发请求时展现出卓越的性能表现。本文系统性地梳理了反向代理的核心概念从正向代理与反向代理的本质差异出发深入剖析了 Nginx 在负载均衡、缓存加速、动静分离及多站点代理等关键场景中的工程实践。通过对 Location 五大匹配规则的优先级体系与 proxy_pass 路径重构机制的详细解读结合综合配置示例与匹配流程表帮助读者建立起完整的请求转发逻辑认知。在实践层面本文从零搭建了一套包含五台节点的完整反向代理实验环境涵盖网络配置、Nginx 部署、代理规则验证等全链路操作为读者提供了可复现的实战参考。掌握 Nginx 反向代理技术不仅是构建高可用 Web 架构的基石更是迈向现代云原生基础设施运维的关键一步。