vue 项目部署到iis后 浏览器刷新404

最新写了一个vue的demo,部署到iis后,正常router跳转都没问题,但是点击浏览器的刷新按钮后,就会出现404找不到文件的现象。

解决办法 :

1.首先需要确定iis是否下载了rewrite 模块,如果没有下载,需要下载一个安装上:
rewrite

2.vue项目下添加一个web.config文件:

csharp 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- 启用 URL Rewrite 模块 -->
    <rewrite>
      <rules>
        <!-- 排除实际文件和目录 -->
        <rule name="Static Files" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
          </conditions>
          <action type="None" />
        </rule>
        
        <rule name="Directories" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
          </conditions>
          <action type="None" />
        </rule>
        
        <!-- Vue Router History 模式:所有其他请求重定向到 index.html -->
        <rule name="Vue Router History Mode" 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="/index.html" />
        </rule>
      </rules>
    </rewrite>
    

    
    <!-- 压缩配置(可选但推荐) -->
    <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
      <dynamicTypes>
        <add mimeType="text/html" enabled="true" />
        <add mimeType="text/plain" enabled="true" />
        <add mimeType="text/css" enabled="true" />
        <add mimeType="text/javascript" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
      </dynamicTypes>
    </httpCompression>
    
    <!-- 安全头配置 -->
    <httpProtocol>
      <customHeaders>
        <add name="X-Content-Type-Options" value="nosniff" />
        <add name="X-Frame-Options" value="SAMEORIGIN" />
        <add name="X-XSS-Protection" value="1; mode=block" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

根据自己选择添加即可。配置完毕后,放到部署的dist文件夹里即可,再访问网址就可以随便刷新了。

相关推荐
qetfw20 分钟前
Bash 函数与流程控制:read、if、for、case 验证
linux·前端·javascript·bash
jason.zeng@15022071 小时前
Nginx 配置详解(静态 HTML、下载目录、API 反向代理三套场景)
java·vue.js·1024程序员节
鬼手点金1 小时前
从 API 消息到模型 Token:Chat Template
java·前端·javascript·数据库·人工智能·深度学习·1024程序员节
Shadow(⊙o⊙)1 小时前
LangChain 自定义工具全解析:从 @tool 到 bind_tools 的完整调用链路
java·服务器·前端·1024程序员节
小poop2 小时前
HTML 基础入门:从结构到常用标签详解
前端
jiangliushi62 小时前
GPT-6.1 Sol 免费用:PowerShell 装 Codex 报“禁止运行脚本 npm.ps1”?一条命令解决
前端·gpt·npm
计算机魔术师2 小时前
日均500万亿词元,国内推理速度翻倍却仍落后国外——差距卡在哪
前端
excel2 小时前
用 Node.js 搭建一套可持续获客的 SEO 内容系统:前端工程师 30 天实战方案
前端·后端
yma162 小时前
从零搭建 P2P 聊天室:yma16.cloud 在线工具实战
前端·1024程序员节
Mr.Pascal3 小时前
Javascript之什么是作用域?
javascript·1024程序员节