Version: 6000.3
语言: 中文
服务器配置代码示例
为 Web 生成设置 IIS 服务器配置

为 Web 构建设置 Apache 服务器配置

设置 Apache 服务器配置文件以将 Apache 服务器与 Unity Web 版本一起使用。

Apache 是一个开源的跨平台 Web 服务器,可用于为使用 Unity Web 创建的应用程序提供服务。

有关 Apache 以及如何安装和使用它的更多信息,请参阅 Apache 文档

选择您的 Apache Web 构建方法

您可以使用以下两种方法之一在 Apache 中配置 Web 构建:

  • 虚拟主机httpd.conf.

  • 虚拟主机.htaccess.

推荐的最佳实践是在 Apache 的httpd.conf,如果您有权访问该配置。

httpd.conf方法

要使用httpd.conf方法,请在 Apache 服务器配置中使用以下代码:

<Directory /var/www/html/root/path/to/your/unity/content/>
<IfModule mod_mime.c>
RemoveType .gz
AddEncoding gzip .gz
# The correct MIME type for .data.gz would be application/octet-stream, but due to Safari bug https://bugs.webkit.org/show_bug.cgi?id=247421, it is preferable to use MIME Type application/gzip instead.
#AddType application/octet-stream .data.gz
AddType application/gzip .data.gz
AddType application/wasm .wasm.gz
AddType application/javascript .js.gz
AddType application/octet-stream .symbols.json.gz

RemoveType .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

# Insert additional code here

</IfModule>
</Directory>

.htaccess方法

如果您无法修改httpd.conf,例如,由于您的网络托管的管理员权限不可用,但您的 Apache 支持.htaccess,则可以配置.htaccess按以下方式:

# NOTE: "mod_mime" Apache module must be enabled for this configuration to work.
<IfModule mod_mime.c>

# The following lines are required for builds without decompression fallback, compressed with gzip
RemoveType .gz
AddEncoding gzip .gz
AddType application/gzip .data.gz # The correct MIME type here would be application/octet-stream, but due to Safari bug https://bugs.webkit.org/show_bug.cgi?id=247421, it's preferable to use MIME Type application/gzip instead.
AddType application/wasm .wasm.gz
AddType application/javascript .js.gz
AddType application/octet-stream .symbols.json.gz

# The following lines are required for builds without decompression fallback, compressed with 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

# The following line improves loading performance for uncompressed builds
AddType application/wasm .wasm

# Insert additional code here

</IfModule>

提高 gzip 压缩构建的性能

要提高具有解压缩回退的 gzip 压缩 Web 构建的加载性能,请在代码中添加以下行:

AddEncoding gzip .unityweb

通过解压缩回退提高 Brotli 压缩构建的性能

要提高具有解压缩回退功能的 Brotli 压缩版本的加载性能,请将以下代码行添加到您的配置中:

 AddEncoding br .unityweb

启用本机 C/C++ 多线程

如果您在播放器设置中激活了 启用原生 C/C++ 多线程时构建了项目,则需要设置特定的服务器标头。这些标头包括跨域打开器策略 (COOP)、跨域嵌入式策略 (COEP) 和跨域资源策略 (CORP)。

将此代码添加到配置文件中:

 <FilesMatch "\.(htm|html|js|js.gz|js.br)$">
     Header add Cross-Origin-Opener-Policy "same-origin"
     Header add Cross-Origin-Embedder-Policy "require-corp"
     Header add Cross-Origin-Resource-Policy "cross-origin"
 </FilesMatch>

允许跨域资源共享 (CORS) 请求

若要允许 CORS 请求,请将以下代码添加到配置中:lang-apacheconf Header add Access-Control-Allow-Origin "*"

其他资源

服务器配置代码示例
为 Web 生成设置 IIS 服务器配置