欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > nginx配置反向代理后端

nginx配置反向代理后端

2025/5/17 23:19:02 来源:https://blog.csdn.net/weixin_42202992/article/details/147925111  浏览:    关键词:nginx配置反向代理后端

nginx配置反向代理后端

    • 1 环境
      • - 前端Vue项目,部署在服务器8088端口
      • - 后端Springboot项目,部署为8080端口
      • 实现 后端接口/api/开头,前端访问本身ip+端口/api/xxx访问后端接口
    • 2. 配置nginx
      • 2.1 带/方式
      • 2.2 不带/
      • 总结
        • 前端地址访问后端接口
        • 后端地址访问接口

1 环境

- 前端Vue项目,部署在服务器8088端口

- 后端Springboot项目,部署为8080端口

实现 后端接口/api/开头,前端访问本身ip+端口/api/xxx访问后端接口

2. 配置nginx

2.1 带/方式

shell
server {listen 8088;server_name your_domain.com;location / {root /path/to/your/vue/dist/;  # 静态资源目录try_files $uri $uri/ /index.html;}location /api/ {proxy_pass http://localhost: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;proxy_set_header X-Forwarded-Proto $scheme;}# 其他 location 块或配置保持不变
}

解释: 访问http://localhost:8088/api/xxx 会请求到http://localhost:8080/api/xxx

2.2 不带/

server {listen 8088;server_name your_domain.com;location / {root /path/to/your/vue/dist/;  # 静态资源目录try_files $uri $uri/ /index.html;}location /api/ {proxy_pass http://localhost:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}# 其他 location 块或配置保持不变
}

解释: 访问http://localhost:8088/api/xxx 会请求到http://localhost:8080/api/xxx

总结

当配置proxy_pass不加/时, uri会追加到 proxy_pass后面
当配置proxy_pass加/时,不会在proxy_pass后添加uri

前端地址访问后端接口

在这里插入图片描述

后端地址访问接口

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词