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
相关推荐
zhz521410 小时前
Nginx + OpenSSL 自签名证书配置:优劣总结
运维·nginx
EnCi Zheng11 小时前
N3A-一个端口只能给一个程序使用吗?[特殊字符]
网络·nginx·docker
weixin_5450193213 小时前
Nginx 配置SSL实现HTTPS访问
nginx·https·ssl
章老师说2 天前
NGINX官方谈Lua风险:这其实是两条网关技术路线之争
运维·nginx·负载均衡·lua·openresty
xixingzhe23 天前
SpringBoot + Nginx 免鉴权接口安全防护方案
运维·nginx·安全
酷炫的水壶3 天前
使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
运维·nginx·缓存
智商偏低3 天前
Windows Nginx 完整安装 + 启动教程
运维·nginx
oscar9993 天前
3.4 Nginx 负载均衡——动态再平衡的反人性纪律
nginx·github·负载均衡·财富源代码
川石课堂软件测试4 天前
性能测试|Nginx中间件监控与调优
linux·python·nginx·中间件·单元测试·压力测试·harmonyos
m0_740859624 天前
Nginx进行配置文件拆分(以windows解压版为例)
nginx