audiobookshelf/server/controllers
Lars Kiesow 08250e266e
Implement X-Accel Redirect
This patch implements [X-Accel](https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/)
redirect headers as an optional way for offloading static file delivery
from Express to Nginx, which is far better optimized for static file
delivery.

This provides a really easy to configure way for getting a huge
performance boost over delivering all files through Audiobookshelf.

How it works
------------

The way this works is basically that Audiobookshelf gets an HTTP request
for delivering a static file (let's say an audiobook). It will first
check the user is authorized and then convert the API path to a local
file path.

Now, instead of reading and delivering the file, Audiobookshelf will
return just the HTTP header with an additional `X-Accel-Redirect`
pointing to the file location on the file syste.

This header is picked up by Nginx which will then deliver the file.

Configuration
-------------

The configuration for this is very simple. You need to run Nginx as
reverse proxy and it must have access to your Audiobookshelf data
folder.

You then configure Audiobookshelf to use X-Accel by setting
`USE_X_ACCEL=/protected`. The path is the internal redirect path used by
Nginx.

In the Nginx configuration you then configure this location and map it
to the storage area to serve like this:

```
location /protected/ {
  internal;
  alias /;
}
```

That's all.

Impact
------

I just did a very simple performance test, downloading a 1170620819
bytes large audiobook file from another machine on the same network
like this, using `time -p` to measure how log the process took:

```sh
URL='https://url to audiobook…'

for i in `seq 1 50`
do
  echo "$i"
  curl -s -o /dev/null "${URL}"
done
```

This sequential test with 50 iterations and without x-accel resulted in:

```
real 413.42
user 197.11
sys 82.04
```

That is an average download speed of about 1080 MBit/s.

With X-Accel enabled, serving the files through Nginx, the same test
yielded the following results:

```
real 200.37
user 86.95
sys 29.79
```

That is an average download speed of about 2229 MBit/s, more than
doubling the previous speed.

I have also run the same test with 4 parallel processes and 25 downloads
each. Without x-accel, that test resulted in:

```
real 364.89
user 273.09
sys 112.75
```

That is an average speed of about 2448 MBit/s.

With X-Accel enabled, the parallel test also shows a significant
speedup:

```
real 167.19
user 195.62
sys 78.61
```

That is an average speed of about 5342 MBit/s.

While doing that, I also peaked at the system load which was a bit lower
when using X-Accel. Even though the system was delivering far more data.
But I just looked at the `load1` values and did not build a proper test
for that. That means, I cant provide any definitive data.

Supported Media
---------------

The current implementation works for audio files and book covers. There
are other media files which would benefit from this mechanism like feed
covers or author pictures.

But that's something for a future developer ;-)
2023-01-23 00:02:27 +01:00
..
AuthorController.js Fix:Manually updating author image path & realtime update author image #1317 2022-12-26 15:45:42 -06:00
BackupController.js Update:Backups API endpoints, add get all backups route, update socket init event payload 2022-11-24 13:14:29 -06:00
CacheController.js Update:Encode & embed metadata API endpoints, separate cache & search endpoints into controllers 2022-11-19 13:28:06 -06:00
CollectionController.js Remove invalid RSS feeds on init and remove feeds when associated entity is removed 2022-12-31 14:08:34 -06:00
EBookController.js Start of new epub reader 2023-01-01 18:09:00 -06:00
FileSystemController.js Update FileSystemController.js to respond with objects 2022-11-29 11:55:22 -06:00
LibraryController.js Add:Global library search also searches on podcast episode titles #1363 2023-01-04 17:43:15 -06:00
LibraryItemController.js Implement X-Accel Redirect 2023-01-23 00:02:27 +01:00
MeController.js Comments where user settings needs to be removed 2022-12-17 14:52:10 -06:00
MiscController.js Update:Remove RSS feeds from login response payload and include feeds from library items request 2022-12-31 10:59:12 -06:00
NotificationController.js
PlaylistController.js Add:Create playlist from a collection #1226 2022-12-17 17:31:19 -06:00
PodcastController.js Fix:Check if Windows before cleaning file path for POSIX separators #1254 2023-01-05 17:45:27 -06:00
RSSFeedController.js Add:RSS feed for series & cleanup empty series from db #1265 2022-12-31 16:58:19 -06:00
SearchController.js Add MusicBrainz provider 2023-01-07 13:05:33 -06:00
SeriesController.js Add:RSS feed for series & cleanup empty series from db #1265 2022-12-31 16:58:19 -06:00
SessionController.js
ToolsController.js Add:Option to disable backup of audio files in embed metadata tool #1370 2023-01-07 15:16:52 -06:00
UserController.js Server socket event fixes 2022-12-22 16:26:11 -06:00