nginx:前后端分离常用配置

  • 前端:dist 放在 nginx/html/dist
  • 后端接口:/api/** 转发到 SpringBoot
XML 复制代码
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
相关推荐
东风破_14 小时前
Docker 基础:为什么需要容器?
前端·后端·nginx
东风破_14 小时前
Docker 实战:使用 Nginx 作为容器入口代理 Node 服务
前端·后端·nginx
qq10916069811 天前
nginx+TongWeb 配置非443端口的https
网络·nginx·https
晨曦花锦1 天前
Vim基础操作与Ollama模型管理,顺便配置Nginx反向代理
linux·nginx·编辑器·vim
難釋懷1 天前
Nginx-Lua
nginx·junit·lua
我滴老baby1 天前
多个内网服务怎么统一入口?部署 Nginx Proxy Manager 配置反向代理与 SSL
运维·nginx·ssl
fatcoder2 天前
玩转Nginx 09 — 运维排障:三场"火灾"实战演练
前端·后端·nginx
moMo2 天前
从“巨轮”到“本地开发”:用 Docker 和 Nginx 解决你的端口焦虑
nginx·docker
蜀道山老天师2 天前
Zabbix监控Apache与Nginx应用实践完整指南
linux·运维·nginx·apache·zabbix
北城笑笑2 天前
Server 18 ,Nginx + Vite 前端部署排错实战:从 `/api/gpt/chat` 404 到 9012 后端服务的完整定位过程
linux·运维·前端·nginx·ubuntu·vue