Web 服务器 "Content-Type" 未在 Apache 上正确配置

Web Server "Content-Type" not configuring correctly on Apache

网络服务器的新手:

我正在尝试配置我的网络服务器来为 WebAssemblies(编辑:.wasm)提供服务 aplication/wasm 据我所知,我在使用 Apache 的 Hostinger 主机上。我也在使用 gzip

(编辑 #3 该网页是 Unity 'WebGL' 构建的,正在流式传输 WebAssemblies)

我的网页在子域上,这是我在服务器上的目录结构:

(编辑 #2)

这是我的 public_html .htaccess 文件:

# BEGIN LSCACHE
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
<IfModule LiteSpeed>
RewriteEngine on
CacheLookup on
RewriteRule .* - [E=Cache-Control:no-autoflush]
RewriteRule \.litespeed_conf\.dat - [F,L]

### marker CACHE RESOURCE start ###
RewriteRule wp-content/.*/[^/]*(responsive|css|js|dynamic|loader|fonts)\.php - [E=cache-control:max-age=3600]
### marker CACHE RESOURCE end ###

### marker FAVICON start ###
RewriteRule favicon\.ico$ - [E=cache-control:max-age=86400]
### marker FAVICON end ###

### marker DROPQS start ###
CacheKeyModify -qs:fbclid
CacheKeyModify -qs:gclid
CacheKeyModify -qs:utm*
CacheKeyModify -qs:_ga
### marker DROPQS end ###

</IfModule>
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
# END LSCACHE
# BEGIN NON_LSCACHE
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
# END NON_LSCACHE
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

(结束编辑#2)

这是我在 Build 文件夹中的 .htaccess 文件:

# This configuration file should be uploaded to the server as "<Application Folder>/Build/.htaccess"
# This configuration has been tested with Unity 2020.1 builds, hosted on Apache/2.4
# NOTE: "mod_mime" Apache module must be enabled for this configuration to work.

# The following lines are required for builds without decompression fallback, compressed with gzip


<IfModule mod_mime.c>
    AddEncoding gzip .unityweb
    AddEncoding gzip .wasm
    AddType application/wasm .wasm
</IfModule>


<IfModule mod_mime.c>
    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
</IfModule>

根据控制台错误,我的 'Build/WebGL Build.wasm.gz' 文件似乎没有正确的 MIME 类型:

根据“网络”选项卡,它的 MIME 类型为 text/plain:

所以问题当然是为什么 'Build/WebGL Build.wasm.gz' 没有使用 application/wasm MIME 类型提供服务?

我的第一个答案似乎有缺陷,即使它仍然有效。这是不可接受的,因为它为尚未发现的问题和与 Unity 和 WebXR Exporter 更新相关的新问题敞开了大门。

有人向我指出某些文件的类型仍然错误(即 'WebGL Framework.js.gz' 应该是 JavaScript 类型,但实际上是 wasm 类型。)

所以,事实证明这里至少有几个问题:

#1 分机之间需要空格。例如,如果您使用:

'AddType application/javascript .data.gz'

该命令被忽略(我假设是因为它是语法错误。)但是如果您使用:

'AddType application/javascript .data .gz'

它会起作用。

#2 第二个问题似乎是旧的 Unity 文档。文档中的“.htaccess”似乎不再有效。

非常感谢来自 WebXR Discord 服务器的 m2! m2 为我提供了 .htaccess 文件,似乎可以完美运行!

# This configuration file should be uploaded to the server as "<Application Folder>/Build/.htaccess"
# This configuration has been tested with Unity 2020.1 builds, hosted on Apache/2.4
# NOTE: "mod_mime" Apache module must be enabled for this configuration to work.

# The following lines are required for builds without decompression fallback, compressed with gzip


<IfModule mod_mime.c>

<FilesMatch "[^.]+\.data.gz$">
  Header set Content-Type "application/octet-stream"
  Header set Content-Encoding "gzip"
</FilesMatch>

<FilesMatch "[^.]+\.js.gz$">
  Header set Content-Type "application/javascript"
  Header set Content-Encoding "gzip"
</FilesMatch>

<FilesMatch "[^.]+\.wasm.gz$">
  Header set Content-Type "application/wasm"
  Header set Content-Encoding "gzip"
</FilesMatch>

<FilesMatch "[^.]+\.gz$">
  Header set Content-Encoding "gzip"
</FilesMatch>

<FilesMatch "[^.]+\.wasm$">
  # Header set Content-Encoding "gzip"
  Header set Content-Type "application/wasm"
</FilesMatch>

</IfModule>

(*** 编辑 #2 ***)

我发现我的服务器实际上是 LightSpeed Web 6.0.9 Enterprise,它是“Apache 替代品”,不确定这是否相关...

(*** 结束编辑#2 ***)

现在所有文件都有正确的类型,如网络面板所示:

WebAssembly 流式处理现在可以正常工作的证据是不再有 wasm 错误,并且页面加载速度是原来的两倍。

我对服务器非常陌生,所以如果有人能证实我的分析,我将不胜感激!!!