目前公司的Web项目是SPA应用,采用先后端分离开发,因此有时也会倒腾Vue框架。webpack
“先后端应用最终以容器形态、在k8s中部署, 为此我搭建了基于Gitlab flow的Devops流程。web
在Devops实践中,容器部署成为良方和事实标准。后端
可是在开发和自测阶段,不要滥打镜像,先后端团队还须要一个友好的联调+自测的验证环境,api
最友好、最顺手的web服务器当属IIS,(后端API已经使用WebDeploy部署到IIS),本文记录使用IIS托管Vue应用的姿式。服务器
前置条件:安装IIS、Url Rewrite Module !!!
1. 部署Vue应用
咱们以Github上Vue Todo应用为例,执行yarn build
框架
若是build成功,你会发现生成了一个dist
静态资源文件夹。asp.net
2. 建立web.config
将yarn生成的dist文件夹拷贝到C:\dist,并添加如下web.config文件, 这个文件实际是咱们在IIS Url-Rewrite module上配置的结果。前后端分离
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Handle History Mode and custom 404/500" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite> <httpErrors> <remove statusCode="404" subStatusCode="-1" /> <remove statusCode="500" subStatusCode="-1" /> <error statusCode="404" path="/survey/notfound" responseMode="ExecuteURL" /> <error statusCode="500" path="/survey/error" responseMode="ExecuteURL" /> </httpErrors> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
3. 在IIS上部署Vue应用
点击肯定工具
4.运行Vue应用

Nice!如今你的Vue静态应用就运行在IIS上。ui
But, 在先后端分离模式中,咱们的Vue应用不只有静态资源,还要发起动态api请求。
“通常状况下webpack打包后的api请求路径是
/
, 会尝试请求同域名下api资源, 实际并不存在。
咱们须要将对Vue应用的api请求代理到真实后端地址。
5. 反向代理动态api请求
Vue应用站点还要充当一部分反向代理服务器的做用。

假设真实后端api地址部署在10.200.200.157:8091地址上,api请求以/api
为前缀。
下面利用Url Rewrite Module 反向代理api请求到真实后端:
点击站点功能视图---> Url重写---> 添加入站规则
Url重写的结果其实就是下面的web.config文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- To customize the asp.net core module uncomment and edit the following p. For more info see https://go.microsoft.com/fwlink/?linkid=838655 --> <system.webServer> <rewrite> <rules> <clear /> <rule name="ReverseProxyInboundRule" stopProcessing="true"> <match url="api/([_0-9a-z/-]+)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="http://10.200.200.157:8091/{R:0}" /> </rule> <rule name="ResourceToIndex" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite> <httpErrors> <remove statusCode="404" subStatusCode="-1" /> <remove statusCode="500" subStatusCode="-1" /> <error statusCode="404" path="/survey/notfound" responseMode="ExecuteURL" /> <error statusCode="500" path="/survey/error" responseMode="ExecuteURL" /> </httpErrors> </system.webServer> </configuration> <!--ProjectGuid: 068855e8-9240-4f1a-910b-cf825794513b-->
注意:黄色背景行即是反向代理规则ReverseProxyInboundRule
, 注意反向代理规则要在静态资源规则ResourceToIndex
的前面。
这样咱们就完成了在先后端分离开发模式下,使用IIS托管Vue应用的全过程。
-----
可算解决了先后端团队开发、自测阶段一大痛点,我把这个问题定义为[效率工具]类,有兴趣的读者能够试一试。