【问题标题】:Web Server "Content-Type" not configuring correctly on ApacheWeb 服务器“内容类型”未在 Apache 上正确配置
【发布时间】:2021-11-08 22:19:00
【问题描述】:

对网络服务器非常陌生:

我正在尝试配置我的 Web 服务器以将 WebAssemblies(编辑:.wasm)作为应用程序/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:

当然,问题是为什么应用程序/wasm MIME 类型不提供“Build/WebGL Build.wasm.gz”?

【问题讨论】:

  • 您的服务器上是否启用了mod_mime?你能删除IfModule 行,看看没有这些行是否有效?
  • @StephenOstermiller,如果我删除 IfModule 行,它将不再起作用...
  • 我忘了添加我的第二个 .htaccess。我在刚才的问题中添加了它...
  • “不再有效”是什么意思?您收到 500 错误吗?发生这种情况时,error_log 中有信息吗?
  • 2 个错误:未捕获的 SyntaxError:无效或意外令牌和 WebGL Build.loader.js:1 无法解析 Build/WebGL Build.framework.js.gz!如果启用了构建压缩但托管内容的 Web 服务器被错误配置为不提供带有 HTTP 响应标头“Content-Encoding: gzip”的文件,则可能会发生这种情况。检查浏览器控制台和 Devtools 网络选项卡进行调试。

标签: apache webserver mime-types webassembly gzipstream


【解决方案1】:

似乎我的第一个答案有缺陷,即使它仍然有效。这是不可接受的,因为它为尚未发现的问题以及与 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 错误,并且页面加载速度也一样快。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    相关资源
    最近更新 更多