Nc and mod_deflate

Nope I was replying to Tamsy who compresses archives and other binaries that will only cost additional CPU cycles and make the response larger than the original.

What is necessary is to “only” compress selected (mostly) text based mime types.

I am so sorry that I made parts of my post unable for you to just conveniently copy/paste.
It might has happened because I simply didn’t wanted to fiddle around with the automagic markdown-formatting here at this forum. How careless of me!
(Thank you so much for your priceless advise. Appointment with the neurologist to check on the fitness of my old brain is already fixed.)

However, here it comes again, this time for your convenient copy/paste-ing:

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE

# prefer to brotli over gzip if brotli is available
<IfModule mod_brotli.c>
# some assets have been compressed, so no need to do it again
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|web[pm]|woff2?)$ no-brotli

Certainly the SetEnvIfNoCase directive only excludes certain extensions from using brotli. As we all know SetEnvIfNoCase allows the setting of environment variables based on characteristics of the request. You are certainly free to add or remove some according to your needs. Very basic regex-knowledge should be sufficient already:

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|web[pm]|woff2|xyz|zip|tar\.gz|7z?)$ no-brotli

We certainly wouldn’t have implemented this if it is not a valid setup. So far, after a few weeks in use there haven’t been any issues emerged. We’ll see …

The cost of additional CPU cycles which might make the response on your high-volume server installation really noticable larger than the original can be minimized by serving brotli/gzip precompressed files instead of dynamically (on-the-fly) compressed files. This has the advantage of requiring almost no resource on the web server while allowing it to use the highest compression level available. Since this involves much more configuration steps than just droping a seven-liner into NC’s .htaccess-file I will not cover the latter with this thread. Just do a lookup how this can be done with your favorite search engine.

I wanted to select text, in this case SetEnvIfNoCase and hit reply, which automatically quotes it. Kind of hard with an image. And btw, when you have to parse log files for a certain error message, it is annoying (and error prone) to write a long passage of text, just because some idiot thinks a screenshot is the smart thing to do - especially cramped into a word doc. I’ve had to deal with that crap too many times and I’m sorry if I let my anger out on you.

I think you misunderstood. I certainly know that the env var no-brotli deactivates brotli compression. I also know regex very well.

My point was something else. You are basing the test on file extensions. File extensions can be faked or wrong or too restrictive. Adding a new extension and restart the server (in case of not using .htaccesss for perf) every time one figures out that they missed an extension is not the best way to handle it.
That’s why mime types have been invented in the first place: content based evaluation. Either way, use your file extensions.

Also, precompressing server data is out of the scope here. Or does Nextcloud come with precompressed files. No, it doesn’t. Furthermore, it is not possible for dynamic content.

What irked me was the sentence: “another way to easily implement brotli”. It’s misleading. People take your snippet and their perf goes down, unless their server doesn’t serve archives or other compressed data (besides the few image extensions).
But hey, why do I care? I’m done here. No more post from me in this topic.

@Tamsy thanks for the suggestion of using SetEnvIfNoCase. I’ve checked and it correctly excludes the filetypes listed. I also don’t think the overhead of using brotli on already compressed content is very high at all, if any. This is not where the bottlenecks are with Nextcloud.

I have added this directly in my section:

	<If "%{HTTP:Accept-encoding} =~  /br/">
		SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|web[pm]|woff2|xyz|zip|tar\.gz|7z?)$ no-brotli
	<ElseIf "%{HTTP:Accept-encoding} =~  /deflate/">
		SetOutputFilter DEFLATE
		SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|web[pm]|woff2|xyz|zip|tar\.gz|7z?)$ no-gzip
1 Like

@anon9582441 It’s worth to take a look here. Although the writing is a bit outdated (Apache supports Brotli through the official mod_brotli module since version 2.4.26) it describes quite well the pros and cons of dynamic (on-the-fly) compression vs. static compression and you will also find a link to how to add Brotli compression support to Apache for static assets and pages.

1 Like

@Tamsy. Thanks. I don’t think that nextcloud actually serves many static pages apart from images and such. Most is PHP generated on the fly because NC combines different CSS/JS depending on what user and what page is loaded. I am pretty happy with being able to enable Brotli/gzip at all because it was broken in NC before.

I really don’t think that is the case. Mostly the opposite because NC is usually self hosted and not high-traffic, and even at very high compression ratios it is probably enough to saturate most peoples uplink.

However, you still have a point. So if we take a look at the manual at we can see that default compression value is 5. Some quick benchmarks locally on a NAS box (not very fast machine) gives:

# brotli -1 -c test.pdf |mbuffer > /dev/null

No compression: test.pdf = 23.0 MiB
1: 20.7 MiByte in  0.1sec - average of  175 MiB/s
2: 20.7 MiByte in  0.2sec - average of  130 MiB/s
3: 20.6 MiByte in  0.2sec - average of 94.2 MiB/s
4: 20.4 MiByte in  0.8sec - average of 26.0 MiB/s
5: 20.4 MiByte in  0.8sec - average of 24.9 MiB/s
6: 20.3 MiByte in  1.6sec - average of 12.8 MiB/s
7: 20.2 MiByte in  3.6sec - average of 5766 kiB/s
8: 20.0 MiByte in  8.1sec - average of 2522 kiB/s
9: 19.7 MiByte in 37.6sec - average of  536 kiB/s

As we can see the overall speed goes down quickly with higher compression ratios.

Default value of 5 does not seem optimal in my case. I have chosen 1 instead.

BrotliCompressionQuality 1


I’d like to remind you of both, the NC-guidelines and the NC-Code of conduct.
This is a forum where we are discussing while staying nice to each others. Even if we don’t agree on the arguments of the other partipiciants.

Pls edit those two mentioned postings likewise as they have been tagged as inappropriate (and I agree they are).

Thanks for your understanding.
Happy NC-ing.