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
相关推荐
linmengmeng_131411 小时前
【总结】Docker 容器重建后 Nginx 502 问题排查与解决
nginx·docker·容器
難釋懷12 小时前
Nginx-Https证书配置
运维·nginx·https
风向决定发型丶13 小时前
Logrotate配置nginx日志切割
运维·nginx
難釋懷13 小时前
Nginx自签名-OpenSSL
运维·chrome·nginx
五阿哥永琪1 天前
Nginx入门教学+实战
运维·nginx
热爱Liunx的丘丘人1 天前
Docker Compose 实现 Nginx 与 MySQL 多容器服务编排
mysql·nginx·docker
tang7451639621 天前
Ubuntu 24.04 安装 Nginx 1.29.6 完整版教程20260320
linux·nginx·ubuntu
小糖学代码1 天前
LLM系列:环境搭建:4.Nginx使用教程
运维·python·神经网络·nginx
sg_knight1 天前
负载均衡配置:使用 Nginx 反向代理 MinIO 集群的实战教程
运维·nginx·负载均衡·文件管理·minio·cos·oss
ruanyongjing1 天前
元数据驱动开发 - 面向对象编程思想的补充 (十二)
nginx·macos·docker