nginx配置api转发时结尾带不带斜杠的区别是什么
更新时间:2026年08月30日 10:20:45 作者:博风
还在为nginx转发后接口路径不对而头疼吗?本文用实例讲清proxy_pass结尾带/和不带/的区别,帮你快速配置SpringBoot后端接口,避免404错误
nginx配置api转发时结尾带不带斜杠区别
- 前端:
dist放在nginx/html/dist - 后端接口:
/api/**转发到 SpringBoot
server {
listen 80;
server_name localhost;
# 前端静态资源
location / {
root html/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html; # Vue/React 路由刷新
}
# 后端接口代理
location /api/ {
proxy_pass http://127.0.0.1:8080/api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}结尾不带/的
location /api/{
proxy_pass http://127.0.0.1:8080
}| 请求地址 | 转发到后端地址 |
|---|---|
| /api/user/login | http://127.0.0.1:8080/api/user/login |
| /api/getInfo | http://127.0.0.1:8080/api/getInfo |
| /api/order/list | http://127.0.0.1:8080/api/order/list |
结尾带/的
location /api/{
proxy_pass http://127.0.0.1:8080/
}| 请求地址 | 转发到后端地址 |
|---|---|
| /api/user/login | http://127.0.0.1:8080/user/login |
| /api/getInfo | http://127.0.0.1:8080/getInfo |
| /api/order/list | http://127.0.0.1:8080/order/list |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
详解Nginx服务器的nginx-http-footer-filter模块配置
这篇文章主要介绍了Nginx服务器的nginx-http-footer-filter模块配置,nginx-http-footer-filter用作在请求的页面底部插入代码,需要的朋友可以参考下2016-01-01
linux安装nginx和前端部署vue项目全过程(实测react项目也可)
这篇文章主要介绍了如何将前端项目打包并部署到服务器上,包括使用nginx进行配置和启动等步骤,文中通过代码以及图文介绍的非常详细,需要的朋友可以参考下2024-11-11
Nginx中roxy_set_header与add_header区别举例浅析
proxy_set_header是一个 Nginx 配置指令,用于设置将要转发到后端服务器的 HTTP 请求头,这篇文章主要给大家介绍了关于Nginx中roxy_set_header与add_header区别的相关资料,需要的朋友可以参考下2024-04-04
Nginx中location proxy_pass加与不加/的区别说明
本文总结了Nginx配置中的location匹配规则和proxy_pass用法,包括前缀匹配、精确匹配、正则表达式匹配、通配符匹配以及proxy_pass的配置细节2024-12-12


最新评论