托管在 Microsoft IIS 服务器上的 Gatsby 站点为静态资产和其他文件提供 404 错误
Gatsby site hosted on Microsoft IIS server giving 404 errors for static assets and other files
我正在尝试在远程 Microsoft IIS 服务器上托管 Gatsby 站点。我已经把Gatsby的public
文件夹里的内容复制到IIS服务器的文件夹里了。该网站有效,但我在静态文件夹、网络清单文件和其他一些被引用的文件中的所有图像都找不到。我的控制台中有很多 404 错误。我该如何解决这个问题?
谢谢。
你安装IIS URL Rewrite了吗?
需要在web.config
中定义outboundRules
,应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="static">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=31536000, immutable" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<location path="page-data">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=0, must-revalidate" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
</staticContent>
<rewrite>
<outboundRules>
<rule name="AdjustCacheForDontCacheFiles" preCondition="IsDontCacheFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=0, must-revalidate" />
</rule>
<rule name="AdjustCacheForCachePermanentlyFiles" preCondition="IsCachePermanentlyFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=31536000, immutable" />
</rule>
<preConditions>
<preCondition name="IsDontCacheFile">
<add input="{REQUEST_FILENAME}" pattern="(.*\.html)|(sw\.js)|(app\-data\.json)|(page\-data\.json)" />
</preCondition>
<preCondition name="IsCachePermanentlyFile">
<add input="{REQUEST_FILENAME}" pattern="((.*\.js)|(.*\.css))$" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
查看部署到 Microsoft Internet Information Server (IIS) 了解更多详细信息。
对我来说,这看起来像是导致 404 错误的重定向问题。
我终于能够通过将 .webmanifest 和 .webp 的 MIME 类型添加到 web.config 文件来解决它:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="static">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=31536000, immutable" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<location path="page-data">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=0, must-revalidate" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
<mimeMap fileExtension=".webp" mimeType="image/webp" />
</staticContent>
<rewrite>
<outboundRules>
<rule name="AdjustCacheForDontCacheFiles" preCondition="IsDontCacheFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=0, must-revalidate" />
</rule>
<rule name="AdjustCacheForCachePermanentlyFiles" preCondition="IsCachePermanentlyFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=31536000, immutable" />
</rule>
<preConditions>
<preCondition name="IsDontCacheFile">
<add input="{REQUEST_FILENAME}" pattern="(.*\.html)|(sw\.js)|(app\-data\.json)|(page\-data\.json)" />
</preCondition>
<preCondition name="IsCachePermanentlyFile">
<add input="{REQUEST_FILENAME}" pattern="((.*\.js)|(.*\.css))$" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
我正在尝试在远程 Microsoft IIS 服务器上托管 Gatsby 站点。我已经把Gatsby的public
文件夹里的内容复制到IIS服务器的文件夹里了。该网站有效,但我在静态文件夹、网络清单文件和其他一些被引用的文件中的所有图像都找不到。我的控制台中有很多 404 错误。我该如何解决这个问题?
谢谢。
你安装IIS URL Rewrite了吗?
需要在web.config
中定义outboundRules
,应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="static">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=31536000, immutable" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<location path="page-data">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=0, must-revalidate" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
</staticContent>
<rewrite>
<outboundRules>
<rule name="AdjustCacheForDontCacheFiles" preCondition="IsDontCacheFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=0, must-revalidate" />
</rule>
<rule name="AdjustCacheForCachePermanentlyFiles" preCondition="IsCachePermanentlyFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=31536000, immutable" />
</rule>
<preConditions>
<preCondition name="IsDontCacheFile">
<add input="{REQUEST_FILENAME}" pattern="(.*\.html)|(sw\.js)|(app\-data\.json)|(page\-data\.json)" />
</preCondition>
<preCondition name="IsCachePermanentlyFile">
<add input="{REQUEST_FILENAME}" pattern="((.*\.js)|(.*\.css))$" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
查看部署到 Microsoft Internet Information Server (IIS) 了解更多详细信息。
对我来说,这看起来像是导致 404 错误的重定向问题。
我终于能够通过将 .webmanifest 和 .webp 的 MIME 类型添加到 web.config 文件来解决它:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="static">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=31536000, immutable" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<location path="page-data">
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="cache-control" />
<add name="cache-control" value="public, max-age=0, must-revalidate" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
<mimeMap fileExtension=".webp" mimeType="image/webp" />
</staticContent>
<rewrite>
<outboundRules>
<rule name="AdjustCacheForDontCacheFiles" preCondition="IsDontCacheFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=0, must-revalidate" />
</rule>
<rule name="AdjustCacheForCachePermanentlyFiles" preCondition="IsCachePermanentlyFile" stopProcessing="true">
<match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
<action type="Rewrite" value="public, max-age=31536000, immutable" />
</rule>
<preConditions>
<preCondition name="IsDontCacheFile">
<add input="{REQUEST_FILENAME}" pattern="(.*\.html)|(sw\.js)|(app\-data\.json)|(page\-data\.json)" />
</preCondition>
<preCondition name="IsCachePermanentlyFile">
<add input="{REQUEST_FILENAME}" pattern="((.*\.js)|(.*\.css))$" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>