Today's article will be a short how-to guide regarding a common problem with horizontally scaled NodeBB installations:
In a deployment environment with more than one NodeBB server, locally uploaded files (post images, avatars, etc) are split between the application servers, and
n-1/n images will always be missing, where
n is the number of servers
The typical solution to this would be to install a third-party plugin to offload and/or centralise the hosting of the images. For example, the Imgur plugin allows uploaded images to be hosted on Imgur, thus neatly resolving this problem.
In some scenarios, however, using a third-party image hosting service may not be preferable or ideal. For example, a healthcare-related business may have strict regulations that disallow off-site hosting of images for security and confidentiality purposes.
In this case, setting up a simple network file system to expose a shared folder will allow uploaded data to stay within your server control.
Step 1: Prepare the shared folder
In the separate server, install the appropriate dependencies:
$ sudo apt-get install nfs-kernel-server
Once this is done, expose the shared folder (in our example,
/usr/local/nodebb-uploads) via the configuration file
/etc/exports. Open the file in your favourite editor and add this line:
Then start the NFS server:
$ sudo service nfs-kernel-server start
Step 2: Prepare the NodeBB servers
NodeBB expects uploaded files to be in
public/uploads, relative to the installation directory, so to mount the shared folder into your NodeBB install, run this command:
$ sudo mount example.hostname.com:/usr/local/nodebb-uploads /path/to/nodebb/public/uploads
example.hostname.com is the hostname of the other server (configured in step 1)
This temporarily mounts the shared folder into your local file system, but to make these changes persist upon reboot, you can also add a line into
example.hostname.com:/usr/local/nodebb-uploads /path/to/nodebb/public/uploads nfs rsize=8192,wsize=8192,timeo=14,intr
Step 3: Testing/Permissions
$ cd /path/to/nodebb/public/uploads $ ls
Ensure that the NodeBB server can write to the shared folder, and you should be good to go!
For more information, consult the NFS documentation here: https://help.ubuntu.com/lts/serverguide/network-file-system.html