Nginx 服务器

Nginx 是一款高性能的HTTP和反向代理服务器。在高连接并发的情况下,能够支持高达5万个并发连接数的响应,而内存、CPU等系统资源消耗却非常低,运行非常稳定。

Nginx 部署

# 安装 nginx
[root@www ~]# yum -y install nginx

# 启动 nginx
[root@www ~]# systemctl enable nginx --now

# 准备主页
[root@www ~]# mv /usr/share/nginx/html/index.html{,.ori}
[root@www ~]# echo Hello World From Nginx > /usr/share/nginx/html/index.html

# 防火墙
[root@www ~]# firewall-cmd --add-service=http --permanent
[root@www ~]# firewall-cmd --reload

[root@client ~]# curl http://www.laoma.cloud

# windows客户端修改 C:\Windows\System32\drivers\etc\hosts
# Linux或Unix修改 /etc/hosts
# 添加如下记录
10.1.8.10 www.laoma.cloud

动态站点

静态站点:站点的内容是固定不变的。

动态站点:内容是通过程序生成的,每次都可能提供新的东西。

使用 PHP

客户端访问php网页流程:

  1. 请求php页面发送给web服务器。
  2. web服务器发现访问的内容是php,则将php的页面交给php-fpm服务处理。
  3. php-fpm服务将php页面交给php程序解析执行,执行结果返回给php-fpm。
  4. php-fpm服务将结果返回给web服务器。
  5. web服务器将结果返回给客户端。
# 安装PHP和php-fpm,建议把其他的扩展包一起安装
[root@server ~ 09:45:01]# yum install -y php php-fpm
# php-fpm: 负责接收web程序发来的php代码
# php:负责解析和执行php代码,并将结果返回给php-fpm

# 当客户端访问 php 站点时,web站点接收用户请求
# 并转发 php 代码给php-fpm服务
# php-fpm 服务调用php解析php网页,然后将结果返回给web程序
# web 程序将结果返回给客户端

# 启用并启动php-fpm服务
[root@server ~ 09:46:42]# systemctl enable php-fpm.service --now

# 建议把其他的扩展包一起安装
[root@server ~ 09:46:42]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt

# 查看 php 版本
[root@server ~ 09:55:41]# php -v
PHP 5.4.16 (cli) (built: Apr  1 2020 04:07:17) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

# 测试 php 是否正常
[root@server ~ 09:55:46]# echo "<?php echo 'PHP Test Page'.\"\n\"; ?>" > php_test.php
[root@server ~ 09:56:24]# php php_test.php
PHP Test Page

# 准备测试页,使用phpinfo查看详细信息
[root@server ~ 09:56:33]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/phpinfo.php 

配置虚拟机主机支持php

#按照默认配置修改
[root@server ~ 09:57:27]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-www.ggg.cloud-php.conf
# 修改配置文件
#直接弄一个最简单的配置(不要证书和重定向)
[root@server certs 10:09:07]# vim /etc/nginx/conf.d/vhost-www.ggg.cloud-php.conf
server {

   listen       80;
   listen       [::]:80;
   server_name  www.ggg.cloud;
   root         /usr/share/nginx/html;
   location ~ \.php$ {
        try_files $uri =404;
        # fastcgi_pass:指定FastCGI服务地址,将PHP请求转发到本地9000端口的PHP-FPM进程
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

还可以将php的配置与虚拟主机配置分离配置

[root@server certs 10:35:50]# cat /etc/nginx/conf.d/vhost-www.ggg.cloud-php.conf 
server {

   listen       80;
   listen       [::]:80;
   server_name  www.ggg.cloud;
   root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
}


[root@server certs 10:36:07]# cat /etc/nginx/default.d/vhost-www.ggg.cloud-php.conf
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

配置完成后,务必要重启nginx服务

[root@server certs 10:35:45]# systemctl restart nginx

客户端测试

在这里插入图片描述

反向代理

反向代理介绍

反向代理(reverse proxy),指的是**代理外网用户的请求到内部的指定的服务器,并将数据返回给用户。**客户端不直接与后端服务器进行通信,而是与反向代理服务器进行通信,隐藏了后端服务器的 IP 地址。

反向代理的主要作用是提供负载均衡和高可用性:

  • 负载均衡:Nginx可以将传入的请求分发给多个后端服务器,以平衡服务器的负载,提高系统性能和可靠性。

  • 缓存功能:Nginx可以缓存静态文件或动态页面,减轻服务器的负载,提高响应速度。

  • 动静分离:将动态生成的内容(如 PHP、Python、Node.js 等)和静态资源(如 HTML、CSS、JavaScript、图片、视频等)分别存放在不同的服务器或路径上。

  • 多站点代理:Nginx可以代理多个域名或虚拟主机,将不同的请求转发到不同的后端服务器上,实现多个站点的共享端口。

反向代理模块

Location 配置

Location 配置语法

Nginx 使用 location 匹配规则 + proxy_pass 反向代理指令实现反向代理功能,匹配本质是 “URL 路径匹配 → 命中对应规则 → 转发至指定后端地址”。

  • location 定义匹配路径
  • proxy_pass 指定后端服务地址
http {
    # 后端服务可配置 upstream 集群(推荐,支持负载均衡)
    upstream backend_nginx {
        nginx 192.168.1.100:8080;  # 后端服务1
        nginx 192.168.1.101:8080;  # 后端服务2(多节点自动轮询负载均衡)
    }

    server {
        listen 80;  # 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;    # 传递客户端真实IP
            proxy_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;
        }
    }
}
Location 匹配规则
  1. 后端匹配逻辑:URL 路径 → 按 location 优先级命中规则 → 由规则内的 proxy_pass 转发至对应后端
  2. 优先级:精确匹配(=)> 前缀匹配(^~)> 正则匹配(/*)> 普通前缀 > 兜底(/);
  3. URL 重构关键:proxy_pass 末尾是否带 /,决定是否剔除 location 匹配的路径前缀。
1. 精确匹配(=)
  • 语法:location = /path { ... }

  • 逻辑:仅当请求 URL 与 /path 完全一致时命中,优先级最高。

  • 示例:

    # 仅匹配 http://localhost/login,不匹配 /login?a=1、/login/
    location = /login {
        proxy_pass http://backend_login:8080;
    }
    
2. 前缀配(^~)
  • 语法:location ^~ /path { ... }

  • 逻辑:URL 以 /path 开头即命中,优先级仅次于精确匹配,会跳过正则匹配

  • 用途:优先匹配静态资源(如 /static、/img)或特定业务路径,避免被正则规则拦截。

  • 示例:

    # 匹配所有 /static 开头的请求(如 /static/css/main.css、/static/img/1.jpg)
    location ^~ /static/ {
        proxy_pass http://backend_static:80;
    }
    
3. 正则匹配(~ / ~*)
  • 语法:

    • 区分大小写:location ~ /regex { ... }(如 /API 不匹配 /api 规则)
    • 不区分大小写:location ~* /regex { ... }(如 /API、/api 均匹配)
  • 逻辑:URL 符合正则表达式即命中,优先级低于前缀匹配(^~),多个正则规则按定义顺序匹配,先命中先生效

  • 示例:

    # 匹配所有 .jpg、.png、.gif 结尾的图片请求(不区分大小写)
    location ~* \.(jpg|png|gif)$ {
        proxy_pass http://backend_img:80;
    }
    
4. 普通前缀匹配(无符号)
  • 语法:location /path { ... }

  • 逻辑:URL 以 /path 开头即命中,优先级低于正则匹配,多个普通前缀规则按 “路径最长” 优先命中

  • 示例:

    # 规则1:匹配 /api/xxx(路径长度3)
    location /api/ {
        proxy_pass http://backend_api:9090;
    }
    # 规则2:匹配 /api/user/xxx(路径长度7,比规则1长,优先命中)
    location /api/user/ {
        proxy_pass http://backend_user:9090;
    }
    
5. 通用匹配(/)
  • 语法:location / { ... }
  • 逻辑:所有未被上述规则命中的请求,都会匹配此规则(兜底),优先级最低。
  • 用途:通常作为全局反向代理,转发所有默认请求到主后端服务。
proxy_pass 后端地址细节

proxy_pass 末尾是否带 /,会直接改变转发到后端的 URL 路径,这是后端匹配后 “URL 重构” 的核心,分 2 种场景:

场景 1: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
    
场景 2: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/list
    
综合示例

配置文件

http {
    upstream backend_main { nginx 192.168.1.200:8080; }
    upstream backend_api { nginx 192.168.1.201:9090; }
    upstream backend_static { nginx 192.168.1.202:80; }
    upstream backend_login { nginx 192.168.1.203:8080; }

    server {
        listen 80;
        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;
        }
    }
}

匹配流程

客户端请求 URL 命中的 location 规则 转发至后端的 URL 对应后端服务
http://localhost/login = /login http://192.168.1.203:8080 backend_login
http://localhost/static/css/main.css ^~ /static/ http://192.168.1.202:80/css/main.css backend_static
http://localhost/img/1.jpg ~* .(jpg png gif)$ http://192.168.1.202:80/img/1.jpg backend_static
http://localhost/api/user/info /api/ http://192.168.1.201:9090/user/info backend_api
http://localhost/index / http://192.168.1.200:8080/index backend_main

反向代理实践环境

环境架构
服务器 主机名 IP地址 服务器角色
客户端 client.laoma.cloud 10.1.8.11 测试服务器
Nginx 服务器 proxy.laoma.cloud 10.1.8.20 代理服务器
Nginx 服务器 nginx1.laoma.cloud 10.1.8.21 Web 服务器
Nginx 服务器 nginx2.laoma.cloud 10.1.8.22 Web 服务器
Nginx 服务器 nginx3.laoma.cloud 10.1.8.23 Web 服务器

/etc/hosts

# 所有节点
[root@proxy ~ 14:23:35]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

############ proxy ##################
10.1.8.11 client.laoma.cloud client
10.1.8.20 www.laoma.cloud www
10.1.8.20 proxy.laoma.cloud proxy
10.1.8.21 nginx1.laoma.cloud nginx1
10.1.8.22 nginx2.laoma.cloud nginx2
10.1.8.23 nginx3.laoma.cloud nginx3
后端 nginx 服务器配置
# 除了客户端,所有节点安装nginx并启动nginx服务。
# 用client批量处理
[root@client ~ 14:49:33]# for host in 10.1.8.{20,21,22,23}; do echo ;ssh root@$host yum install -y nginx; done

#查询是否安装成功
[root@client ~ 14:49:48]# for host in 10.1.8.{20,21,22,23}; do echo ;ssh root@$host rpm -q nginx; done 

nginx-1.20.1-10.el7.x86_64

nginx-1.20.1-10.el7.x86_64

nginx-1.20.1-10.el7.x86_64

nginx-1.20.1-10.el7.x86_64

# 启动并启用服务
[root@client ~ 14:52:22]# for host in 10.1.8.{20,21,22,23}; do ssh root@$host systemctl enable nginx --now; done

# 防火墙设置(所有节点全都关)
[root@client ~ 14:52:22]# firewall-cmd --add-service=http --permanent
[root@client ~ 14:52:22]# firewall-cmd --add-service=http

# 准备主页-其他节点
[root@client ~ 14:57:27]# for host in nginx{1..3}.ggg.cloud; do ssh root@$host "echo Welcome to $host> /usr/share/nginx/html/index.html"; done

# 客户端测试
[root@client ~ 14:57:48]# curl http://nginx1/
Welcome to nginx1.ggg.cloud
[root@client ~ 14:57:53]# curl http://nginx2/
Welcome to nginx2.ggg.cloud
[root@client ~ 14:57:55]# curl http://nginx3/
Welcome to nginx3.ggg.cloud
前端 proxy 服务器配置
# 准备主页-代理节点
[root@proxy ~ 14:35:23]# yum -y install nginx
[root@proxy ~ 14:36:55]# echo Welcome to www.ggg.cloud > /usr/share/nginx/html/index.html

[root@proxy ~ 15:06:48]# mkdir /var/nginx
[root@proxy ~ 15:06:52]# echo "Hello, Nginx" > /var/nginx/index.html
[root@proxy ~ 15:06:56]# echo "Hello, ggg" > /var/nginx/test.txt
[root@proxy ~ 15:07:04]# cp /usr/share/nginx/html/nginx-logo.png /var/nginx/
[root@proxy ~ 15:07:10]# ls /var/nginx/
index.html  nginx-logo.png  test.txt


[root@proxy ~ 15:07:13]# vim /etc/nginx/conf.d/proxy.conf
server {
	listen  80;
	server_name www.ggg.cloud;

    # 匹配根位置
    location / {
      root /var/nginx;
      index index.html;
    }
}
# 重新加载nginx配置
[root@proxy ~ 15:07:53]# nginx -s reload
测试
[root@proxy ~ 15:07:57]# curl http://www.ggg.cloud
Hello, Nginx

[root@proxy ~ 15:08:07]# curl http://www.ggg.cloud/test.txt
Hello, ggg

访问 www.ggg.cloud/nginx-logo.png,系统会返回以下页面:

在这里插入图片描述

反向代理基础实践-代理本地

环境准备
[root@proxy ~ 15:08:51]# mkdir /var/nginx/nginx{1,2}
[root@proxy ~ 15:16:05]# echo "Hello, I'm here /var/nginx/nginx1" > /var/nginx/nginx1/index.html
[root@proxy ~ 15:17:15]# echo "Hello, I'm here /var/nginx/nginx2" > /var/nginx/nginx2/index.html

[root@proxy ~ 15:17:26]# mkdir /var/nginx{1,2}
[root@proxy ~ 15:17:33]# echo "Hello, Nginx1" > /var/nginx1/index.html
[root@proxy ~ 15:17:37]# echo "Hello, Nginx2" > /var/nginx2/index.html
[root@proxy ~ 15:18:31]# 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.html

2 directories, 7 files

[root@proxy ~ 15:19:08]# \
for path1 in www{1..2}
do
  for path2 in nginx{1..2}
  do
    mkdir -p /var/$path1/$path2
	echo "Hello, I'm here /var/$path1/$path2" > /var/$path1/$path2/index.html
  done
done

[root@proxy ~ 15:21:26]# tree /var/www*
/var/www1
├── nginx1
│   └── index.html
└── nginx2
    └── index.html
/var/www2
├── nginx1
│   └── index.html
└── nginx2
    └── index.html

4 directories, 4 files

基本测试

[root@proxy ~ 15:21:31]# curl http://www.ggg.cloud/
Hello, Nginx

# 显示结果是目录/var/nginx/nginx1中内容
[root@proxy ~ 15:21:56]# curl http://www.ggg.cloud/nginx1/
Hello, I'm here /var/nginx/nginx1

# 显示结果是目录/var/nginx/nginx2中内容
[root@proxy ~ 15:22:05]# curl http://www.ggg.cloud/nginx2/
Hello, I'm here /var/nginx/nginx2
实践1:无符号匹配
[root@proxy ~ 15:37:23]# vim /etc/nginx/conf.d/proxy.conf
server {
	listen  80;
	server_name www.ggg.cloud;

    # 匹配根位置
    location / {
        root /var/nginx;
        index index.html;
    }

    # 匹配/nginx1时,/var目录下找nginx1,完整路径是/var/nginx1
    location /nginx1 {
        root /var;
        # 等效于下面的 alias 语句,必须使用绝对路径
        # alias /var/nginx1;
        index index.html;
    }
}
# 重新加载nginx配置
[root@proxy ~ 15:37:58]# nginx -s reload

访问测试

# nginx1 后面必须添加 / 符号
[root@client ~ 14:59:05]# curl http://www.ggg.cloud/nginx1/
Hello, Nginx1
# 显示结果是目录/var/nginx1中内容

# nginx2 后面必须添加 / 符号
[root@client ~ 15:39:50]# curl http://www.ggg.cloud/nginx2/
Hello, I'm here /var/nginx/nginx2
# 显示结果是目录/var/nginx/nginx2中内容

实验结果:无符号匹配优先级高于默认的/。

实践2:正则表达式匹配
[root@proxy ~ 15:38:24]# vim /etc/nginx/conf.d/proxy.conf
server {
	listen  80;
	server_name www.ggg.cloud;

    # 匹配根位置
    location / {
        root /var/nginx;
        index index.html;
    }

    # 匹配/nginx1时,/var目录下找nginx1,完整路径是/var/nginx1
    location /nginx1 {
        root /var;
        # 等效于下面的 alias 语句,必须使用绝对路径
        # alias /var/nginx1;
        index index.html;
    }

    # 正则表达式匹配 /nginx.*
    location ~ /nginx.* {
        root /var/www1;
        index index.html;
    }
}
# 重新加载nginx配置
[root@proxy ~ 15:40:42]# nginx -s reload

访问测试

# nginx1 后面必须添加 / 符号
[root@client ~ 15:40:01]# curl http://www.ggg.cloud/nginx1/
Hello, I'm here /var/www1/nginx1
# 显示结果是目录/var/www1/nginx1中内容

# nginx2 后面必须添加 / 符号
[root@client ~ 15:41:02]# curl http://www.ggg.cloud/nginx2/
Hello, I'm here /var/www1/nginx2
# 显示结果是目录/var/www1/nginx2中内容

实验结果:正则表达式匹配优先级高于无符号。

实践3:精确匹配
[root@proxy ~ 15:40:45]# vim /etc/nginx/conf.d/proxy.conf
server {
	listen  80;
	server_name www.ggg.cloud;

    # 匹配根位置
    location / {
        root /var/nginx;
        index index.html;
    }

    # 匹配/nginx1时,/var目录下找nginx1,完整路径是/var/nginx1
    location /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配置
[root@proxy ~ 15:41:26]# nginx -s reload

访问测试

# nginx1 后面必须添加 / 符号
[root@client ~ 15:41:09]# curl http://www.ggg.cloud/nginx1/
Hello, I'm here /var/www1/nginx1
# 显示结果是目录/var/www1/nginx1中内容

# nginx2 后面必须添加 / 符号
[root@client ~ 15:41:36]# curl http://www.ggg.cloud/nginx2/
Hello, I'm here /var/www2/nginx2
# 显示结果是目录/var/www2/nginx2中内容

实验结果:精确匹配优先级高于正则表达式。

反向代理基础实践-代理远端

实践1:无符号匹配
[root@proxy ~ 15:56:40]# vim /etc/nginx/conf.d/proxy.conf
server {
    listen  80;
    server_name www.ggg.cloud;
    
    # 匹配根位置
    location / {
        root /var/nginx;
        index index.html;
    }

    # 匹配 /nginx1/ 开头,代理到nginx1.ggg.cloud,/nginx1/不组合到后端服务器
    # 访问 /nginx1/ 开头,相当于直接访问http://nginx1.ggg.cloud/
    location /nginx1/ {
        # 后端服务
        proxy_pass http://nginx1.ggg.cloud/; # 注意:代理后端后面有 /。
        index index.html;
    }
}
# 重新加载nginx配置
[root@proxy ~ 15:57:25]# nginx -s reload

访问测试

# nginx1 后面必须添加 / 符号
[root@client ~ 15:46:48]# curl http://www.ggg.cloud/nginx1/
Welcome to nginx1.ggg.cloud
# 显示结果是服务器 nginx1.ggg.cloud 内容

# nginx2 后面必须添加 / 符号
[root@client ~ 16:09:39]# curl http://www.ggg.cloud/nginx2/
Hello, I'm here /var/nginx/nginx2
# 显示结果是目录/var/nginx/nginx2中内容

实验结果:无符号匹配优先级高于默认的/。

实践2:正则表达式匹配
[root@proxy ~ 16:09:30]# vim /etc/nginx/conf.d/proxy.conf
server {
    listen  80;
    server_name www.ggg.cloud;
    
    # 匹配根位置
    location / {
        root /var/nginx;
        index index.html;
    }

    # 匹配 /nginx1/ 开头,代理到nginx1.ggg.cloud,/nginx1/不组合到后端服务器
    # 访问 /nginx1/ 开头,相当于直接访问http://nginx1.ggg.cloud/
    location /nginx1/ {
        # 后端服务
        proxy_pass http://nginx1.ggg.cloud/; # 注意:代理后端后面有 /。
        index index.html;
    }
 
    # 正则表达式匹配 /nginx.*
    location ~ /nginx[123].* {
        # 手动重写路径:去掉 /nginx 前缀,转发到目标服务器
        # ^/nginx[123](.*)$ 匹配 /nginx[123] 开头的完整路径,$1表示 /nginx[123] 后的所有内容
        # break 表示重写后不再匹配其他 rewrite 规则
        rewrite ^/nginx[123](.*)$ $1 break;
         
        # proxy_pass 不带 URI(无末尾的 /),配合 rewrite 实现路径替换
        proxy_pass http://nginx2.ggg.cloud;  
        index index.html;
    }
}
# 重新加载nginx配置
[root@proxy ~ 16:10:35]# nginx -s reload

访问测试

# nginx1 后面必须添加 / 符号
[root@client ~ 16:11:54]# curl http://www.ggg.cloud/nginx1/
Welcome to nginx2.ggg.cloud
# 显示结果是服务器 nginx2.ggg.cloud 内容

# nginx2 后面必须添加 / 符号
[root@client ~ 16:12:02]# curl http://www.ggg.cloud/nginx2/
Welcome to nginx2.ggg.cloud
# 显示结果是服务器 nginx2.laoma.cloud 内容

# nginx3 后面必须添加 / 符号
[root@client ~ 16:12:07]# curl http://www.ggg.cloud/nginx3/
Welcome to nginx2.ggg.cloud
# 显示结果是服务器 nginx2.laoma.cloud 内容

实验结果:正则表达式匹配优先级高于无符号。

实践3:精确匹配
[root@proxy ~ 16:11:51]# vim /etc/nginx/conf.d/proxy.conf
server {
    listen  80;
    server_name www.ggg.cloud;
    
    # 匹配根位置
    location / {
        root /var/nginx;
        index index.html;
    }

    # 匹配 /nginx1/ 开头,代理到nginx1.ggg.cloud,/nginx1/不组合到后端服务器
    # 访问 /nginx1/ 开头,相当于直接访问http://nginx1.ggg.cloud/
    location /nginx1/ {
        # 后端服务
        proxy_pass http://nginx1.ggg.cloud/; # 注意:代理后端后面有 /。
        index index.html;
    }
 
    # 正则表达式匹配 /nginx.*
    location ~ /nginx[123].* {
        # 手动重写路径:去掉 /nginx 前缀,转发到目标服务器
        # ^/nginx[123](.*)$ 匹配 /nginx[123] 开头的完整路径,$1表示 /nginx[12] 后的所有内容
        # break 表示重写后不再匹配其他 rewrite 规则
        rewrite ^/nginx[123](.*)$ $1 break;
         
        # proxy_pass 不带 URI(无末尾的 /),配合 rewrite 实现路径替换
        proxy_pass http://nginx2.ggg.cloud;  
        index index.html;
    }

    # 精确匹配
    location = /nginx3/ {
        proxy_pass http://nginx3.ggg.cloud/;  
        index index.html;
    }

}
# 重新加载nginx配置
[root@proxy ~ 16:13:18]# nginx -s reload

访问测试

# nginx1 后面必须添加 / 符号
[root@client ~ 16:12:46]# curl http://www.ggg.cloud/nginx1/
Welcome to nginx2.ggg.cloud
# 显示结果是服务器 nginx2.laoma.cloud 内容

# nginx2 后面必须添加 / 符号
[root@client ~ 16:13:40]# curl http://www.ggg.cloud/nginx2/
Welcome to nginx2.ggg.cloud
# 显示结果是服务器 nginx2.laoma.cloud 内容

# nginx3 后面必须添加 / 符号
[root@client ~ 16:13:43]# curl http://www.ggg.cloud/nginx3/
Welcome to nginx3.ggg.cloud
# 显示结果是服务器 nginx3.laoma.cloud 内容

实验结果:精确匹配优先级高于正则表达式。

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐