Jump to content

How to move static files to CDN or another server in KVS


Recommended Posts

KVS serves and stores different types of static content, like video files, photo images, video screenshots, user avatars, theme design styles / scripts.

For video files and album photos KVS supports multi-server architecture and this is completely another story described here. This topic describes how to move static to CDN or another server for the rest of static content.

 

Theme design files

These are typically files located under /static folder on your main server.

In most cases you will need to move these design elements to CDN for faster load. You should do that manually following any guideline you have for CDN. Please note that you should also check with CDN guide / support about crossdomain security issues (for example fonts can be only loaded from another domain if they provide Access-Control-Allow-Origin "*" header and etc.)

From KVS side you only need to adjust one option in /admin/include/setup.php file:

$config['statics_url']="$config[project_url]";

By default it points to the project URL, you should change it to your CDN:

$config['statics_url']="//cdn.domain.com";

It will force all design static files to be loaded from this domain.

NOTE: If your project is running under HTTPS, you should use CDN which also supports HTTPS.

In most cases CDN will cache files from your original server for some time (can be forever). If you do any changes in these files on your server, your site will still continue using old versions. You may need to reset your CDN cache so that it pulled new versions. Another option is to increment version of the modified file when it is linked from template (not sure if all CDNs support this). For example templates use this tag to include CSS style file:

<link href="{{$config.statics_url}}/styles/all-responsive-white.css?v=5.0" rel="stylesheet" type="text/css"/>

If this file is loaded from CDN and you modify it, you can increment its version so that CDN will update it from your server again. The actual numbers don't matter, it should be different from the old one:

<link href="{{$config.statics_url}}/styles/all-responsive-white.css?v=5.0.1" rel="stylesheet" type="text/css"/>

 

Video screenshots from CDN

By default video screenshots are stored under this folder on your main server:

/contents/videos_screenshots

You can move them to CDN as well, however you should consider the problem of invalidation here. Screenshots can be changed from time to time in admin panel. For example if you create 20 screenshots per each video and then leave only 10 best screenshots, or if rotator automatically removes badly clickable screenshots, your CDN will still show the old ones incorrectly. There is no solution for this issue from KVS side, you should check with your CDN provider on which options are available. For example they can ping files from your server from time to time and check if they were changed or not. But if you don't commit any screenshots editing you will probably be fine with leaving this as it is, since in your case the files will unlikely be changed.

In order to switch screenshots to CDN you should first configure CDN per their guidelines and then in /admin/include/setup.php file of your KVS installation you should modify this URL:

$config['content_url_videos_screenshots']="$config[project_url]/contents/videos_screenshots";

Please make sure to modify the correct option, as there are some similarly spelling options around. Your change should be something like:

$config['content_url_videos_screenshots']="//cdn.domain.com/contents/videos_screenshots";

NOTE: If your project is running under HTTPS, you should use CDN which also supports HTTPS.

If you want to make screenshot loading even faster, you can define alternate URLs for screenshots via different subdomains. The reason is that browsers do have limits to the number of concurrent connections for a single server (~6 at the same time). If there are many screenshots on your page they will be loaded in portions, but you can make this happen in parallel by loading them from different subdomains. They should all be configured as aliases for the same server and should serve the same content. In order to do that there is another option in /admin/include/setup.php config file:

$config['alt_urls_videos_screenshots']=array();

You can list several alternate URLs there in the following format (make sure you retain the correct syntax or you may brake the code):

$config['alt_urls_videos_screenshots']=array("//cdn2.domain.com/contents/videos_screenshots", "//cdn3.domain.com/contents/videos_screenshots");

This will force KVS to randomly load screenshots from these 3 URLs:

//cdn.domain.com/contents/videos_screenshots (configured as main URL)
//cdn2.domain.com/contents/videos_screenshots (configured as alternate URL 1)
//cdn3.domain.com/contents/videos_screenshots (configured as alternate URL 2)

 

Video screenshots from another server but not CDN

If you want to use another server for serving video screenshots, you have 2 options:

 

Option 1. Use rsync to push updates in KVS /contents/videos_screenshots folder to another server. This should be configured by your server admins. In this case you need to modify screenshots URL (see Video screenshots from CDN section). However this approach is not recommended, because rsync works asynchronously with some delay, so the common problem is that for new videos the screenshots may not yet been synced to remote server. If you know the interval between each rsynс push, you can workaround this problem by configuring KVS to postpone new content for some time. You should then change this option in /admin/include/setup.php:

$config['post_dates_offset']="0";

It indicates the number of minutes all new content will be delayed. For example if rsync works every 1 hour you can configure it to be 70 or 80 minutes, to make sure that screenshots of new videos have been pushed to remote server in the moment when video appears on your site.

 

Option 2. The second option is to move the whole screenshots storage folder to a remote server using network filesystem (NFS mount). This can be a good solution if remote server is located with the same datacenter for stability reason. You should ask your admins to create writable NFS mount to the remote server and move all existing content from /contents/videos_screenshots there. Then you should modify these 2 options in /admin/include/setup.php file:

$config['content_path_videos_screenshots']="$config[project_path]/contents/videos_screenshots";
... there are some other options in between ...
$config['content_url_videos_screenshots']="$config[project_url]/contents/videos_screenshots";

The first option is a filesystem path and the second one is URL. Your changes should look like this:

$config['content_path_videos_screenshots']="/mnt/server2/contents/videos_screenshots"; // should be a real path of the mounted folder
...
$config['content_url_videos_screenshots']="//server2.domain.com/contents/videos_screenshots";

NOTE: If your PHP is configured with open_basedir restriction you should also update it to include /mnt/server2 path, otherwise KVS will not be able to write new screenshots into the mounted folder. You can check if this folder is writable or not with KVS audit plugin.

Using the 2nd approach is more preferred, as there won't be any delay in screenshots availability. But it needs stable connection between servers.

 

Other static files

Other static files are normally user avatars and images of categorization objects like categories, channels, models.

You can use the same approach for them as for screenshots, either move them to CDN (see Video screenshots from CDN), or move to another server (see Video screenshots from another server but not CDN).

The only difference is in what options you should change in /admin/include/setup.php. Here they are:

$config['content_url_categories']="$config[project_url]/contents/categories";               // URL for categories
$config['content_url_content_sources']="$config[project_url]/contents/content_sources";     // URL for content sources
$config['content_url_models']="$config[project_url]/contents/models";                       // URL for models
$config['content_url_dvds']="$config[project_url]/contents/dvds";                           // URL for channels / series / DVDs
$config['content_url_posts']="$config[project_url]/contents/posts";                         // URL for posts
$config['content_url_avatars']="$config[project_url]/contents/avatars";                     // URL for users
$config['content_url_referers']="$config[project_url]/contents/referers";                   // URL for referers, not used in most cases
$config['content_url_other']="$config[project_url]/contents/other";                         // URL for uncategorized static files

By default these all point to your main server, you can switch them to CDN or another server in the same way.

Link to comment
Share on other sites

  • Tech Support changed the title to How to move static files to CDN or another server in KVS
  • 9 months later...
On 8/7/2021 at 9:48 PM, Dasweb said:

If using CDN for screenshots, will KVS place new screenshots on the CDN?

KVS will put new screenshots to the path that is specified in /admin/include/setup.php in this option:

$config['content_path_videos_screenshots']="... path to screenshots storage directory ...";

If this path points to CDN, then KVS will put new screenshots on CDN. If this path points to local server, KVS will put new screenshots on local server.

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...