docs.rs icon indicating copy to clipboard operation
docs.rs copied to clipboard

web-server: add compression

Open syphar opened this issue 2 years ago • 7 comments

In my mind, this could be solved via:

  • adding a reverse proxy to the container (NGINX, Caddy) and enabling compression
  • adding a compression middleware to axum

I assume axum is fine being exposed to the internet and doesn't need to be behind a reverse proxy (?).

Also in our current NGINX config: IP blocks, while it's unclear if we would handle this in AWS / CloudFront, or if we would have to handle this in our webserver (see https://github.com/rust-lang/simpleinfra/issues/353)

syphar avatar Oct 14 '23 09:10 syphar

We'd also need to have TLS in the container if it is exposed to the internet, but I assume we're still having it only accessed by cloudfront over a private network so that shouldn't be necessary.

Nemo157 avatar Oct 14 '23 11:10 Nemo157

Yep, that's my thinking, our webservers would only be accessed through CloudFront, so we don't have to do TLS

syphar avatar Oct 14 '23 11:10 syphar

And especially between the US & CF POPs in europe / asia compression definitely makes a difference

syphar avatar Oct 14 '23 11:10 syphar

Hi! I'd like to take a stab at solving this by adding a compression layer to the middleware stack in the web server. If that's ok, I have a couple of questions:

  1. Can this compression middleware be applied to all routes served by the web server, or is there a reason to target specific routes?
  2. I'm planning on using tower_http::compression, which supports brotli, deflate, gzip or zstd. I don't know enough about this field to make a really educated decision on which one to choose. Does anyone have advice? I know that gzip is widely supported/used, and performs reasonably well. Should we start there?

tverghis avatar Oct 17 '23 04:10 tverghis

Very good question!

  1. yes, all routes is fine
  2. yep, that's the middleware I also had in mind. gzip is fine, this will only compress between the webservers & the CDN. And adding more would probably add more dependencies to the project. You can just use the tower_http default

syphar avatar Oct 17 '23 08:10 syphar

When using tower-http compression, see the comments / change here : https://github.com/rust-lang/crates.io/pull/7330/

syphar avatar Oct 20 '23 16:10 syphar

Having read a little more about it I would say we should enable brotli, when easily possible.

We first would add this to our webserver, and then probably need to change CloudFront config to support this, or use it between the browser and the CDN POP.

syphar avatar Oct 24 '23 04:10 syphar