Version: 2020.2
WebGL:压缩构建和服务器配置
WebGL 浏览器兼容性

WebGL:服务器配置代码示例

下面的代码示例说明如何在使用 WebGL 时配置服务器。以下示例适用于 IIS 和 Apache 服务器。有关 WebGL 服务器配置的更多信息,请参阅 WebGL:压缩构建和服务器配置

无解压缩回退的压缩 WebGL 构建的服务器配置 (IIS)

<?xml version="1.0" encoding="UTF-8"?>
<!--
 The following server configuration can be used for compressed WebGL builds without decompression fallback.
 This configuration file should be uploaded to the server as "<Application Folder>/Build/web.config"。
此配置已在 IIS 7.5、IIS 8.5 和 IIS 10.0 上托管的 Unity 2020.1 版本中进行了测试。
注意:要在无解压缩回退的情况下托管压缩的 WebGL 构建,
您需要在服务器上安装 "URL Rewrite" IIS 模块。
否则,IIS 在使用此配置文件时会抛出异常。
该模块可在 https://www.iis.net/downloads/microsoft/url-rewrite 获取。
-->


<configuration>
 <system.webServer>
   <!--
     Compressed Unity builds without decompression fallback can't be properly hosted on a server which
     has static compression enabled because this might result in the build files being compressed twice.
     The following line disables static server compression.
   -->
   <urlCompression doStaticCompression="false" />
   <!-- 要托管压缩的 Unity 构建,应为压缩的构建文件设置正确的 mimeType。-->
   <staticContent>
     <!--
       注意:如果为相同扩展名多次指定 mimeType,IIS 将抛出异常。
       要避免与服务器上已有的配置产生冲突的可能性,应使用 <remove> 元素为相应扩展名移除 mimeType,
       在使用 <mimeMap> 元素添加 mimeType 元素之前。
     -->
     <!-- 不包含解压缩回退 gzip 压缩构建需要以下代码行。-->
     <remove fileExtension=".data.gz" />
     <mimeMap fileExtension=".data.gz" mimeType="application/octet-stream" />
     <remove fileExtension=".wasm.gz" />
     <mimeMap fileExtension=".wasm.gz" mimeType="application/wasm" />
     <remove fileExtension=".js.gz" />
     <mimeMap fileExtension=".js.gz" mimeType="application/javascript" />
     <remove fileExtension=".symbols.json.gz" />
     <mimeMap fileExtension=".symbols.json.gz" mimeType="application/octet-stream" />
     <!-- 不包含解压缩回退的 Brotli 压缩构建需要以下代码行。-->
     <remove fileExtension=".data.br" />
     <mimeMap fileExtension=".data.br" mimeType="application/octet-stream" />
     <remove fileExtension=".wasm.br" />
     <mimeMap fileExtension=".wasm.br" mimeType="application/wasm" />
     <remove fileExtension=".js.br" />
     <mimeMap fileExtension=".js.br" mimeType="application/javascript" />
     <remove fileExtension=".symbols.json.br" />
     <mimeMap fileExtension=".symbols.json.br" mimeType="application/octet-stream" />
   </staticContent>

   <!--
     Hosting compressed Unity builds without decompression fallback relies on native browser decompression,
     therefore a proper "Content-Encoding" response header should be added for the compressed build files.
     NOTE: IIS will throw an exception if the following section is used without the "URL Rewrite" module installed.
     Download the "URL Rewrite" module from https://www.iis.net/downloads/microsoft/url-rewrite
   -->
   <rewrite>
     <outboundRules>
       <!--
         NOTE: IIS will throw an exception if the same rule name is used multiple times.
         To avoid possible conflicts with configurations that are already on the server, you should remove the mimeType for the corresponding extension using the <remove> 元素为相应扩展名移除 mimeType,
       在使用 <mimeMap> 元素添加 mimeType 元素之前。
       -->
       <!-- 不包含解压缩回退的 gzip 压缩构建需要以下代码。-->
       <remove name="Append gzip Content-Encoding header" />
       <rule name="Append gzip Content-Encoding header">
         <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
         <conditions>
           <add input="{REQUEST_FILENAME}" pattern="\.gz$" />
         </conditions>
         <action type="Rewrite" value="gzip" />
       </rule>
       <!-- 不包含解压缩回退的 Brotli 压缩构建需要以下代码。-->
       <remove name="Append brotli Content-Encoding header" />
       <rule name="Append brotli Content-Encoding header">
         <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
         <conditions>
           <add input="{REQUEST_FILENAME}" pattern="\.br$" />
         </conditions>
         <action type="Rewrite" value="br" />
       </rule>
     </outboundRules>
   </rewrite>
 </system.webServer>
</configuration>

未压缩 WebGL 构建的服务器配置 (IIS)

<?xml version="1.0" encoding="UTF-8"?>
<!--
 The following server configuration can be used for uncompressed WebGL builds.
 This configuration file should be uploaded to the server as "<Application Folder>/Build/web.config"
 此配置已在 IIS 7.5、IIS 8.5 和 IIS 10.0 上托管的 Unity 2020.1 版本中进行了测试。
-->
<configuration>
 <system.webServer>
   <!--
     IIS does not provide default handlers for .data and .wasm files (and in some cases .json files),
     therefore these files won’t be served unless their mimeType is explicitly specified.
   -->
   <staticContent>
     <!--
       NOTE: IIS will throw an exception if a mimeType is specified multiple times for the same extension.
       To avoid possible conflicts with configurations that are already on the server, you should remove the mimeType for the corresponding extension using the <remove> 元素为相应扩展名移除 mimeType,
       在使用 <mimeMap> 元素添加 mimeType 之前。
     -->
     <remove fileExtension=".data" />
     <mimeMap fileExtension=".data" mimeType="application/octet-stream" />
     <remove fileExtension=".wasm" />
     <mimeMap fileExtension=".wasm" mimeType="application/wasm" />
     <remove fileExtension=".symbols.json" />
     <mimeMap fileExtension=".symbols.json" mimeType="application/octet-stream" />
   </staticContent>
 </system.webServer>
</configuration>

WebGL 构建的服务器配置 (Apache)

# 这个配置文件应该以"<Application Folder>/Build/.htaccess"的形式上传到服务器
# 此配置已在 Apache/2.4 上托管的 Unity 2020.1 版本中进行了测试
# 注意:必须启用"mod_mime"Apache 模块,此配置才能正常运行。
<IfModule mod_mime.c>

# 对于无解压缩回退的 gzip 压缩构建,需要以下代码行
RemoveType .gz
AddEncoding gzip .gz
AddType application/octet-stream .data.gz
AddType application/wasm .wasm.gz
AddType application/javascript .js.gz
AddType application/octet-stream .symbols.json.gz

# 对于无解压缩回退的 Brotli 压缩构建,需要以下代码行
RemoveType .br
RemoveLanguage .br
AddEncoding br .br
AddType application/octet-stream .data.br
AddType application/wasm .wasm.br
AddType application/javascript .js.br
AddType application/octet-stream .symbols.json.br

# 以下代码行可改善未压缩构建的性能
AddType application/wasm .wasm

# 取消注释下面的代码行可改善带解压缩回退的 gzip 压缩构建的加载性能
# AddEncoding gzip .unityweb

# 取消注释下面的代码行可改善带解压缩回退的 brotli 压缩构建的加载性能
# AddEncoding br .unityweb

WebGL:压缩构建和服务器配置
WebGL 浏览器兼容性