设置 Apache 服务器配置文件以将 Apache 服务器与 Unity Web 构建结合使用。
Apache 是一个开源、跨平台 Web 服务器,可用于为使用 Unity Web 创建的应用程序提供服务。
有关 Apache 及其安装和使用方法的更多信息,请参阅 Apache 文档。
您可以使用两种方法之一在 Apache 中配置 Web 构建:
httpd.conf 中的虚拟主机。
.htaccess 中的虚拟主机。
建议的最佳做法是在 Apache 的 httpd.conf 中配置虚拟主机(如果可以访问该配置)。
httpd.conf 中使用虚拟主机方法要使用 httpd.conf 中的虚拟主机方法配置 Web 构建,请在 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,例如,由于 Web 托管的管理员权限不可用,但您的 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 压缩 Web 构建的加载性能,请在代码中添加以下行:
AddEncoding gzip .unityweb
要提高具有解压缩回退功能的 Brotli 压缩构建的加载性能,请在配置中添加以下代码行:
AddEncoding br .unityweb
如果在播放器设置中创建了启用了启用原生 C/C++ 多线程 (Enable Native C/C++ Multithreading) 的构建,请将以下代码添加到配置文件中:
<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 请求,请将以下代码添加到配置中:
Header add Access-Control-Allow-Origin "*"