Nginx学习笔记(九)location转发后,proxy_pass结尾带 / 和不带 / 的区别

目录

一、知识回顾

之前使用过 Nginx 的小伙伴或许都了解,Nginx 是一款用于请求转发的高性能中间件,它的配置文件 conf/nginx.conf 中可以通过 location 去定义一些转发规则,nginx.conf 内容大概如下所示:

如果请求成功匹配到 location 的规则,不同的请求类型对应的转发配置不同:

  • 静态资源: 使用 alias、root 关键字。
  • API接口: 使用 proxy_pass 关键字。

那么 问题来了! Nginx 对 API接口进行转发时,下面这两种 proxy_pass 的配置方式对于请求的转发有没有区别呢?

  • proxy_pass http://localhost:8080/;
  • proxy_pass http://localhost:8080;

作为小白的我是觉得没有区别的,但 其实不然,下面我们就实战检验一下。


二、proxy_pass 结尾带 / 和不带 / 的区别

2.1 场景假设

在 Nginx 的配置文件 nginx.conf 中,proxy_pass 有以下两种使用场景:

  • 使用场景一: proxy_pass 结尾带/
nginx 复制代码
location /test/ {
    proxy_pass http://localhost:8080/;
}
  • 使用场景二: proxy_pass 结尾不带/
nginx 复制代码
location /test/ {
    proxy_pass http://localhost:8080;
}

下面我们来详细介绍一下完整的验证过程。

2.2 实战验证

在验证之前,我们先用 Java 编写两个测试 API接口:

DemoController.java

java 复制代码
import com.demo.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class DemoController {

    @GetMapping("/demo/findById")
    public Result<Object> demoFindById(Integer id, HttpServletRequest request) {
        String msg = ">>>>>>>>>>【INFO】请求URI:" + request.getRequestURI() + ",请求URL:" + request.getRequestURL() + ",id:" + id;
        return Result.succeed().setMessage(msg);
    }

    @GetMapping("/test/demo/findById")
    public Result<Object> testDemoFindById(Integer id, HttpServletRequest request) {
        String msg = ">>>>>>>>>>【INFO】请求URI:" + request.getRequestURI() + ",请求URL:" + request.getRequestURL() + ",id:" + id;
        return Result.succeed().setMessage(msg);
    }
}
验证1:结尾带/的场景

nginx.conf 中配置如下:

nginx 复制代码
location /test/ {
    proxy_http_version	1.1;
    proxy_pass			http://localhost:8080/;
    access_log			test-access.log;
    error_log			test-error.log;
}

启动 Nginx:

请求如下地址:

请求结果,如下所示:

可以看到,proxy_pass http://localhost:8080/; 配置会 截断匹配成功的location规则(/test),然后将剩余的 URI进行转发(/demo/findById)

验证2:不带/的场景

nginx.conf 中配置如下:

nginx 复制代码
location /test/ {
    proxy_http_version	1.1;
    proxy_pass			http://localhost:8080;
    access_log			test-access.log;
    error_log			test-error.log;
}

重载 Nginx 配置:

再次请求如下地址:

请求结果,如下所示:

可以看到,proxy_pass http://localhost:8080/; 配置会 保留匹配成功的location规则(/test),然后将完整的 URI进行转发(/test/demo/findById)

2.3 结论

最终验证后结论如下:

  • proxy_pass 结尾带/的场景中,会截断匹配成功的location规则,转发剩余的 URI

配置: proxy_pass http://localhost:8080/

转发前: {nginx地址}/test/demo/findById?id=1

转发后: http://localhost:8080/demo/findById?id=1

  • proxy_pass 结尾不带带/的场景中,会保留匹配成功的location规则,并且转发剩余的 URI

配置: proxy_pass http://localhost:8080

转发前: {nginx地址}/test/demo/findById?id=1

转发后: http://localhost:8080**/test**/demo/findById?id=1


补充:Nginx的相关命令(Windows)

start nginx -- 启动Nginx
nginx -c /usr/local/nginx/conf/nginx.conf -- 启动Nginx,-c参数指定了要加载的nginx配置文件路径
nginx -t -- 测试配置是否正确
nginx -t -c /path/to/nginx.conf -- 测试指定nginx配置文件是否正确
nginx -s reload -- 重新加载Nginx的配置信息(注意:修改配置文件后要清除temp文件夹下的文件,再重新加载)
nginx -s stop -- 停止Nginx(立即停止)
nginx -s quit -- 停止Nginx(优雅停止)
nginx -s reopen -- 重新打开日志(用于更改日志文件名之后,将日志写入到新的日志文件中)
tasklist /fi "imagename eq nginx.exe" -- 查看Nginx相关进程
taskkill /im nginx.exe /f -- 杀掉Nginx相关进程

相关推荐
前端小趴菜~时倾1 分钟前
自我提升-python爬虫学习:day03
爬虫·python·学习
Yu_Lijing2 分钟前
基于C++的《Head First设计模式》笔记——蝇量模式
c++·笔记·设计模式
Kal-Lai2 分钟前
学习笔记:UR5协作机器人正运动学计算
笔记
新缸中之脑2 分钟前
NotebookLM:最佳学习工具
人工智能·学习·chatgpt
LightYoungLee3 分钟前
大模型(七)Agent AI学习笔记
人工智能·笔记·学习
txh05077 分钟前
物联网esp8266小记
物联网·学习·esp8266
星轨初途10 分钟前
C++ 类和对象(下):初始化列表、static 成员与编译器优化深度剖析
android·开发语言·c++·经验分享·笔记
SteveSenna11 分钟前
模仿学习2.7:diffusion
学习
努力的lpp11 分钟前
小迪安全课程第五节复习笔记:渗透测试命令与反弹连接技术
笔记·安全
知识分享小能手12 分钟前
MongoDB入门学习教程,从入门到精通,MongoDB 知识点详解(1)
数据库·学习·mongodb