Brotli/Gzip Broken On WebGL?

I’ve tried to get compression working on both Apache and Nginx on Debian but can’t get it to work. The only way it works is with “decompression fallback” enabled. All the files are being served by my server with Content-Encoding: gzip, br headers.

I’m using Unity 2022.1.7f1. Testing on Safari/Chromium/Firefox on a Macbook with the latest OS X. Does anyone actually have gzip and brotli working on an nginx/apache server without going through decompression fallback?

1 Like

Got unity webgl build working with brotli on AWS S3.
Three days trying to make the same build works on Ubuntu 20 with nginx+brotli plugin and using this config Unity - Manual: Server configuration code samples, but have no luck yet.

1 Like

Thank you very much for adding your experience. I was hoping I was the only one with this problem, someone had a fix. I’ve been trying to get this to work for weeks, but now it’s time to ship my game and I want players to have the fastest possible loading time.

2 Likes

Yes I’ve been going crazy struggling with this problem and cannot find a freaking solution I’m hosting my webgl game on a linode VPS, tried Nginx on Ubuntu, Nginx on CentOs, can’t get compression ever working, and my games 100 MB+ so fast load times and fast brotli decompression is a must :rage:

1 Like

I was trying nginx+ubuntu and nginx+centos too, it’s a miracle, but for some reason ubuntu+nginx works now, I didn’t change my last conf, it just started to work on the next day after I gave up on it.

Which version of Ubuntu and nginx ? Ubuntu 22.04 LTS + nginx 1.18.0? Also, did you use the conf file here, or did you make any changes to it: https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html

Ubuntu 22, nginx 1.22
Have “brotli_static on;” in my /etc/nginx/nginx.conf
And site config uses https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html example.

Idk what exactly must be done for the clean setting, because tried different guides and playing with configs for some time.
The main point is that you need to enable external nginx repo, install nginx from it, compile and install brotli module from the source. There few tutorials in the internet how to do that with ubuntu. Non of them completely worked for me, but somehow job was done.

Very interesting, that’s exactly what I did as well (compiling and installing brotli module from source) but with no success.

Hi all, I’m still having trouble wit this issue. Today I tried Nginx CentOS again from scratch, couldn’t get it worked . Does anyone have any more suggestions on what OS/Server combo might work? Other than resorting to Windows+IIS…

D

Looks like there is no correct nginx+brotly repo/package combination for Centos at the moment. Did you try to make things work from scratch again using Ubuntu? I had troubles when installing nginx and some of nginx-* packages from the default repo and when enabling brotli added external repo and installed some other packages from it. It is good to have all the Nginx-brotli things installed from one trustable repo.

Yes I just re-built my server yesterday, with Ubuntu 18.04, followed the instructions to server brotli and it still doesn’t work for the Unity build.

Getting the errors still:

You can reduce startup time if you configure your web server to add “Content-Encoding: br” response header when serving “Build/d74ca168565please7fe1a96effafeb.js.unityweb” file.

You can reduce startup time if you configure your web server to add “Content-Encoding: br” response header when serving “Build/6b99c05795ffix11ea765efcbab5f.wasm.unityweb” file.

You can reduce startup time if you configure your web server to add “Content-Encoding: br” response header when serving “Build/25ad12ec0fthisproblemunitydevsccff51cf67.data.unityweb” file.

Hey y’all. I’ve run into a very similar issue. I’m using a docker compose script to stand up a local nginx server. I’ve got the same configurations from the link to unity’s page (Unity - Manual: Server configuration code samples) set up. I’m using gzip instead of brotli as I’m not doing https locally. Similarly, I have decompression fallback off. I can’t seem to get it to work. The loader.js file makes an attempt to fetch the .data.gz file but then fails immediately even though the file download starts as expected and completes without error. I’m beginning to wonder if this is an issue in the loader.js being built with unity 2022? I’m going to try a 2021 version and see if the same issue persists.

1 Like

Thanks for commenting on this, let me know if you were able to eventually get it to work!

Can anyone from Unity comment on this?

I use an Apache 2.4 server and it works with with the htaccess file attached below, with the mime_module enabled in the config file. One of the things that helped me debugging this was using the “Network” tab in the browser’s developer tools to see what type the browser was seeing each file getting downloaded as. I was never able to get the htaccess file that Unity provides to work (from this page).

# 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>
1 Like

Added support for Brotli

<IfModule mod_headers.c>
<IfModule mod_env.c>

<FilesMatch "\.data\.(gz|br)$">
    Header set Content-Type "application/octet-stream"
</FilesMatch>

<FilesMatch "\.js\.(gz|br)$">
    Header set Content-Type "application/javascript"
</FilesMatch>

<FilesMatch "\.wasm\.(gz|br)$">
    Header set Content-Type "application/wasm"
</FilesMatch>


<FilesMatch "\.gz$">
    Header set Content-Encoding "gzip"
</FilesMatch>


<FilesMatch "\.br$">
    Header set Content-Encoding "br"
    # Disable automatic gzip compression as it conflicts with Brotli.
    SetEnv no-gzip 
</FilesMatch>


<FilesMatch "\.wasm$">
    Header set Content-Type "application/wasm"
</FilesMatch>

</IfModule>
</IfModule>
3 Likes

You guys are awesome, it works!! Now if only we had this for nginx!

Hello @vriog It works also for nginx. Problem is server configuration manual in Unity official tutorials.

  • First of all, maybe you tried, you DON’T HAVE TO install brotli module on nginx. Chrome and other popular browsers already have brotli decompression algorithm. Your server must only point “hey chrome, that file compressed with brotli, please decompress this file with brotli first to see whats inside”

  • In Unity - Manual: Server configuration code samples you have to put location codes into /etc/nginx/sites-available/[yoursite] file so when you open the game page, ngingx will tag that js.br files as “content-encoding: br” and our browsers will understand that “I need to decode them with brotli algorithm”

Oh it was octet-stream for data.br. That wasn’t in the docs… Unity - Manual: Deploy WebGL application

Yes, browsers today already can natively decompress brotli. The problem is serving them with the wrong content-type. It was the same for me in node.js, I’m pretty sure nginx has its own ways of resolving it. What @forestrf did should be the blueprint to solve any issue concerned with those silly mime-type errors in Unity webgl. As long as you have dominion over the server-side code…

1 Like