nginx配置反向代理后端

nginx配置反向代理后端

    • [1 环境](#1 环境)
      • [- 前端Vue项目,部署在服务器8088端口](#- 前端Vue项目,部署在服务器8088端口)
      • [- 后端Springboot项目,部署为8080端口](#- 后端Springboot项目,部署为8080端口)
      • [实现 后端接口/api/开头,前端访问本身ip+端口/api/xxx访问后端接口](#实现 后端接口/api/开头,前端访问本身ip+端口/api/xxx访问后端接口)
    • [2. 配置nginx](#2. 配置nginx)

1 环境

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

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

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

2. 配置nginx

2.1 带/方式

javascript 复制代码
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 不带/

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;
        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

前端地址访问后端接口
后端地址访问接口
相关推荐
碳基沙盒3 小时前
OpenClaw 多 Agent 配置实战指南
运维
蝎子莱莱爱打怪3 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
何中应3 天前
Nginx转发请求错误
前端·后端·nginx
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅4 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
不是二师兄的八戒4 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器
芝士雪豹只抽瑞克五4 天前
Nginx 高性能Web服务器笔记
服务器·nginx
zhangfeng11334 天前
趋动云 如何ssh登录 服务区 项目server
运维·人工智能·ssh
ZeroNews内网穿透4 天前
谷歌封杀OpenClaw背后:本地部署或是出路
运维·服务器·数据库·安全