nginx反向代理location和proxy_pass斜杠踩坑

简介: 当下前后端项目分离是一种大的趋势,那么前后端分离之后用什么来做它们之前的信息传递桥梁呢,使用最多的就是nginx的反向代理了。在进行nginx反向代理配置的时候,location和proxy_pass中的斜线会造成各种困扰,有时候多一个或少一个斜线,就会造成完全不同的结果。

背景

     当下前后端项目分离是一种大的趋势,那么前后端分离之后用什么来做它们之前的信息传递桥梁呢,使用最多的就是nginx的反向代理了。

问题描述

在进行nginx反向代理配置的时候,location和proxy_pass中的斜线会造成各种困扰,有时候多一个或少一个斜线,就会造成完全不同的结果。


环境准备

  • 下载nginx安装包并安装
  • 前端环境可以使用相关命令打一个静态包放到nginx安装之后的html文件夹下面
  • 配置nginx的conf的配置
  • 启动一个springboot的后端微服务


proxy_pass直接映射主机无其他路径

1:location结尾有斜杠proxy_pass无斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test/ {
          proxy_pass http://localhost:8080;
        } 
}

proxy_pass结尾无斜杠,nginx会将请求路径全部代理过去

例如我的请求是  http://localhost:8001/nginx-test/test
请求路径为/nginx-test/test
代理后的路径为 /nginx-test/test
代理后的请求为 http://localhost:8080/nginx-test/test

2:location结尾有斜杠proxy_pass有斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test/ {
          proxy_pass http://localhost:8080/;
        } 
}

proxy_pass结尾有斜杠,nginx会将请求路径匹配location的剔除后再代理过去

例如我的请求是  http://localhost:8001/nginx-test/test
请求路径为/nginx-test/test
代理后的路径为 test
代理后的请求为 http://localhost:8080/test

3:location结尾无斜杠proxy_pass无斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test {
          proxy_pass http://localhost:8080;
        } 
}

proxy_pass结尾无斜杠,nginx会将请求路径全部代理过去

例如我的请求是  http://localhost:8001/nginx-test/test
请求路径为/nginx-test/test
代理后的路径为 /nginx-test/test
代理后的请求为 http://localhost:8080/nginx-test/test

4:location结尾无斜杠proxy_pass有斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test {
          proxy_pass http://localhost:8080/;
        } 
}


proxy_pass结尾有斜杠,nginx会将请求路径匹配location的剔除后再代理过去

例如我的请求是  http://localhost:8001/nginx-test/test
请求路径为/nginx-test/test
代理后的路径为 /test
代理后的请求为 http://localhost:8080//test

proxy_pass直接映射主机有其他路径

5:location结尾有斜杠proxy_pass无斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test/ {
          proxy_pass http://localhost:8080/test;
        } 
}

nginx会将请求路径匹配location的剔除后再代理过去

例如我的请求是  http://localhost:8001/nginx-test/api
请求路径为/nginx-test/api
代理后的路径为 api
代理后的请求为 http://localhost:8080/testapi

6:location结尾有斜杠proxy_pass有斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test/ {
          proxy_pass http://localhost:8080/test/;
        } 
}

nginx会将请求路径匹配location的剔除后再代理过去

例如我的请求是  http://localhost:8001/nginx-test/api
请求路径为/nginx-test/api
代理后的路径为 api
代理后的请求为 http://localhost:8080/test/api

7:location结尾无斜杠proxy_pass无斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test {
          proxy_pass http://localhost:8080/test;
        } 
}

nginx会将请求路径匹配location的剔除后再代理过去

例如我的请求是  http://localhost:8001/nginx-test/api
请求路径为/nginx-test/test
代理后的路径为 /api
代理后的请求为 http://localhost:8080/test/api

8:location结尾无斜杠proxy_pass有斜杠情况

server {
        listen       8001;
        server_name  localhost;
        root D:/nginx-1.19.3/html/dist;
  location /nginx-test {
          proxy_pass http://localhost:8080/test/;
        } 
}

nginx会将请求路径匹配location的剔除后再代理过去

例如我的请求是  http://localhost:8001/nginx-test/api
请求路径为/nginx-test/test
代理后的路径为 /api
代理后的请求为 http://localhost:8080/test//api

总结

案例

location

proxy_pass

结果

1

/nginx-test/

http://localhost:8080

/nginx-test/test

2

/nginx-test/

http://localhost:8080/

/test

3

/nginx-test

http://localhost:8080

/nginx-test/test

4

/nginx-test

http://localhost:8080/

//test

案例

location

proxy_pass

结果

5

/nginx-test/

http://localhost:8080/test

/testapi

6

/nginx-test/

http://localhost:8080/test/

/test/api

7

/nginx-test

http://localhost:8080/test

/test/api

8

/nginx-test

http://localhost:8080/test/

/test//api

相关文章
|
4天前
|
负载均衡 应用服务中间件 API
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
45 4
|
4天前
|
域名解析 弹性计算 应用服务中间件
基于nginx反向代理实现OSS固定域名IP访问
本文基于阿里云OSS手册:https://help.aliyun.com/zh/oss/use-cases/use-an-ecs-instance-that-runs-centos-to-configure-a-reverse-proxy-for-access-to-oss,继续深入讨论如何利用nginx反向代理,实现固定的IP/域名访问OSS bucket。官方文档能够解决大部分的反向代理固定IP访问oss bucket的场景,但是对于必须使用域名作为endpoint的系统,会出现signatrue鉴权问题。本文继续在官方文档的基础上,将反向代理需要域名作为endpoint的场景补齐方案。
|
4天前
|
负载均衡 监控 Unix
[AIGC] Nginx:一个高性能的 Web 服务器和反向代理
[AIGC] Nginx:一个高性能的 Web 服务器和反向代理
|
4天前
|
负载均衡 JavaScript 前端开发
Nginx实现反向代理、负责均衡、动静分离
Nginx实现反向代理、负责均衡、动静分离
|
4天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
35 0
|
4天前
|
应用服务中间件 Nacos nginx
nacos 2.3.2模式 standalone 使用nginx 反向代理之后访问nacos控制台静
nacos 2.3.2模式 standalone 使用nginx 反向代理之后访问nacos控制台静
|
4天前
|
运维 前端开发 应用服务中间件
LNMP详解(七)——Nginx反向代理配置实战
LNMP详解(七)——Nginx反向代理配置实战
28 1
|
4天前
|
负载均衡 应用服务中间件 Linux
|
4天前
|
tengine Rust 负载均衡
反向代理学习笔记(一) Nginx与反向代理绪论
反向代理学习笔记(一) Nginx与反向代理绪论
|
4天前
|
负载均衡 Java 应用服务中间件