diff --git a/server/Server.js b/server/Server.js index 32ece3ac..a5a0b8b5 100644 --- a/server/Server.js +++ b/server/Server.js @@ -48,6 +48,7 @@ class Server { global.ConfigPath = fileUtils.filePathToPOSIX(Path.normalize(CONFIG_PATH)) global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH)) global.RouterBasePath = ROUTER_BASE_PATH + global.XAccel = process.env.USE_X_ACCEL if (!fs.pathExistsSync(global.ConfigPath)) { fs.mkdirSync(global.ConfigPath) diff --git a/server/controllers/LibraryItemController.js b/server/controllers/LibraryItemController.js index 98cf2d25..f630c2dc 100644 --- a/server/controllers/LibraryItemController.js +++ b/server/controllers/LibraryItemController.js @@ -201,6 +201,10 @@ class LibraryItemController { return res.sendStatus(404) } + if (global.XAccel) { + Logger.debug(`Use X-Accel to serve static file ${libraryItem.media.coverPath}`) + return res.status(204).header({'X-Accel-Redirect': global.XAccel + libraryItem.media.coverPath}).send() + } return res.sendFile(libraryItem.media.coverPath) } diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index 948d152f..ebf02aff 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -51,6 +51,11 @@ class CacheManager { // Cache exists if (await fs.pathExists(path)) { + if (global.XAccel) { + Logger.debug(`Use X-Accel to serve static file ${path}`) + return res.status(204).header({'X-Accel-Redirect': global.XAccel + path}).send() + } + const r = fs.createReadStream(path) const ps = new stream.PassThrough() stream.pipeline(r, ps, (err) => { @@ -72,6 +77,11 @@ class CacheManager { // Set owner and permissions of cache image await filePerms.setDefault(path) + if (global.XAccel) { + Logger.debug(`Use X-Accel to serve static file ${writtenFile}`) + return res.status(204).header({'X-Accel-Redirect': global.XAccel + writtenFile}).send() + } + var readStream = fs.createReadStream(writtenFile) readStream.pipe(res) } diff --git a/server/routers/StaticRouter.js b/server/routers/StaticRouter.js index dcfc8d32..936054a0 100644 --- a/server/routers/StaticRouter.js +++ b/server/routers/StaticRouter.js @@ -1,5 +1,6 @@ const express = require('express') const Path = require('path') +const Logger = require('../Logger') const { getAudioMimeTypeFromExtname } = require('../utils/fileUtils') class StaticRouter { @@ -13,13 +14,18 @@ class StaticRouter { init() { // Library Item static file routes this.router.get('/item/:id/*', (req, res) => { - var item = this.db.libraryItems.find(ab => ab.id === req.params.id) + const item = this.db.libraryItems.find(ab => ab.id === req.params.id) if (!item) return res.status(404).send('Item not found with id ' + req.params.id) - var remainingPath = req.params['0'] - var fullPath = null - if (item.isFile) fullPath = item.path - else fullPath = Path.join(item.path, remainingPath) + const remainingPath = req.params['0'] + const fullPath = item.isFile ? item.path : Path.join(item.path, remainingPath) + + // Allow reverse proxy to serve files directly + // See: https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/ + if (global.XAccel) { + Logger.debug(`Use X-Accel to serve static file ${fullPath}`) + return res.status(204).header({'X-Accel-Redirect': global.XAccel + fullPath}).send() + } var opts = {}