前言html
经常使用的部署方式有两种:vue
1.是把生成好的静态页面放到spring boot的static目录下,与打包后的spring boot项目一块儿发布,当spring boot运行起来后,天然而然就能访问到静态页面文件了。nginx
这种方法比较简单,适用于很是小型的系统。优势是:不须要复杂的配置。而缺点也很明显:须要二者一同发布。我在这里就不作赘述了。git
2.是经过http服务器发布,本文以nginx为例,重点介绍这种方式。github
1、生成静态页面spring
运行npm run buildnpm
生成的页面文件在dist目录下:windows
2、配置nginx后端
windows系统下载nginx:http://nginx.org/en/download.htmlapi
下载完解压后,找到conf/nginx.conf文件,并修改:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; server { listen 80; server_name localhost; # 静态文件夹路径 root 你的路径; index index.html index.htm; # 后端配置 location /api/ { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 后端url proxy_pass http://localhost:18080/; } } }
其中,root项目是配置静态网页文件所在的路径,配置成你本身的目录。
location是配置spring boot项目跨域,这里我配置了当匹配到有“/api/”开头的请求,就会转到spring boot的项目中处理。
运行效果以下图所示:
访问nginx的地址,就能访问到以前生成的静态页面和spring boot的后端。
而这就是先后端分离开发的魅力所在。
git代码地址:https://github.com/carter659/spring-boot-vue-element.git
若是你以为个人博客对你有帮助,能够给我点儿打赏,左侧微信,右侧支付宝。
有可能就是你的一点打赏会让个人博客写的更好:)