Managing Static Files

Maxim Dounin mdounin at mdounin.ru
Wed Jan 31 03:14:34 UTC 2024


Hello!

On Tue, Jan 30, 2024 at 07:36:49AM +0000, bittnitt at gmail.com wrote:

> Hi...I read a few articles about managing static files and I'm a 
> bit confused!
> I use Nginx as the main server to host my website
> I enabled gzip and brotli
> I have also enabled gzip_static and brotli_static
> And I have pre-compressed all static files with gzip and brotli
> I read in an article that after compressing all files, I should 
> delete all uncompressed files to save memory and only gzip and 
> Brotli files remain.
> (Of course, I need to create an empty file called index.html for 
> it to work properly)
> Everything works fine now but my problem is when the browser 
> doesn't support compression and requires uncompressed files.
> In another article it was written that if gunzip is enabled for 
> browsers that do not support the compressed format, it 
> decompresses the gzip then sends it to the client.
> But after doing some testing, I found (I think) that gnuzip only 
> works if nginx is used as the proxy (between main server and 
> client) (due to the content encoding header requirement).
> Now, if I want to support gzip, brotli and non-compressed files, 
> do I have to have all three types of files? Is this method 
> correct? What method do you use? What method is suggested?Thanks

The gunzip module works perfectly fine without proxying, though 
you'll need to ensure that appropriate Content-Encoding is 
properly set on the response.  In particular, if you only have 
gzipped files, you can do:

   gzip_static always;
   gunzip on;

In this configuration gzip_static will respond with the compressed 
version of the file to all requests, and gunzip will uncompress it 
for clients which does not support gzip (see 
http://nginx.org/r/gzip_static for the documentation).

Not sure about brotli_static, but if the 3rd party module is 
implemented properly, it should be possible to do "brotli_static on;" 
in the same configuration to return brotli-compressed files 
to clients which support brotli.

It is not required to delete uncompressed files though.  While 
gunzip module makes it possible, this might be not the best 
approach available: uncompressing files on the fly certainly 
consumes additional CPU resources, and also no uncompressed files 
on disk might be suboptimal for other tasks.  Removing 
uncompressed files usually makes sense only if amount of static 
files is huge.

Hope this helps.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list