Optimizing resource downloads using a caching proxy

Originally published at: https://cookbook.fivem.net/2019/10/29/optimizing-resource-downloads-using-a-caching-proxy/

Server builds starting at pipeline ID 1679 added the ability to configure the fileServer field used for resource downloading using a set of commands: # Set the file server for the specified resource regex. # The URL should not end with a slash. fileserver_add “.*” “http://10.0.0.1/files” # Remove the file server associated with a resource…

18 Likes

Someone can do a littllllllle tutorial on how to use it Windows/Linux ^^ ? I have no clue how this shit works

Is this possible to setup it on windows server ?

1 Like

Each test I’ve done returns with corruption when trying to download the resource. I can see it connecting and such but it fails each time.

I am using the adhesive_cdnKey convar also but changed it from “someSecurePassphrase” as I expect that to be the thing to do.

I writed the exact code in CFG in my local instance (remote copy) and put as ipaddress my remote instance (“http://ip:port/files”) but the game uses 127.0.0.1 insted of my server ip for download the textures (i spawned some vehicles)

Where im doing wrong?

So I’ve actually managed to get this running, brilliant thing (as I’ve actually requested it sometime ago). Running 2 live 100/100 servers behind nginx.

Nice, care to share how you got it running? what issues you may of got into and what you did to get around/fix them?

I haven’t discovered any issues yet, the only thing I need to cover extra is cleaning up nginx proxy cache each time restarting cfx-server. Basically I’m running configuration posted in example.

mind if i ask whereas ur running it on? linux, windows debian?

Hi,

Thanks for the tutorial.

I wanted to setup this configuration on my Linux server but i’m stuck with the error :

[error] 8#8: *651 upstream prematurely closed connection while reading upstream

And on client side :

ResourceCacheDevice reporting failure: transfer closed with 90112 bytes remaining to read - CURL error code 18 (Transferred a partial file)resources::ResourceCacheDeviceV2::DoFetch downloading resource.rpf (hash 228eb29bd1ad23c7e90873fed04bc4eeb36183a1)

I’m running the docker configuration specified in tutorial on an Ubuntu 18.04 and my server on another machine on Ubuntu 18.04 too.

Does anyone got this error and have a workout ?

Thanks by advance for your help :slight_smile:

sddadxczxcsaczsfxzf

For an example of how this works:

In /etc/nginx/sites-enabled/reverse-proxy.conf:

proxy_cache_path /srv/cache levels=1:2 keys_zone=assets:48m max_size=10g ;

upstream backend {
    server ip.of.fivem.server:30120;
}
server {
    listen 443;

    ssl on;
    ssl_certificate /var/www/certs/proxy.pem;
    ssl_certificate_key /var/www/certs/proxy.key;

    server_name proxy.host.here;

    access_log /var/log/nginx/reverse-access.log;
    error_log /var/log/nginx/reverse-error.log;

    location /files/ {
        proxy_pass http://backend$request_uri;
        add_header X-Cache-Status $upstream_cache_status;
        proxy_cache_lock on;
        proxy_cache assets;
        proxy_cache_valid 1y;
        proxy_cache_key $request_uri$is_args$args;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 1;
    }

}

In server.cfg:

fileserver_add ".*" "https://proxy.host.here/files"

adhesive_cdnKey "randomKeyHere"

You DO NOT copy any files from your FiveM server when setting this up. Many people are copying their server cache folder to the proxy which is not the correct method.

Whenever you update a resource on the server, you need to fully clear the cache of the proxy server, like so:

rm -rf /srv/cache/*
service nginx restart

Again, the proxy takes care of caching everything for you. You never need to copy anything manually. Also, SSL is optional but advised. If done properly, you should see this on your proxy server:

[email protected]:/srv/cache# ls
0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

You may have less depending on how much is cached. Good luck.

8 Likes

Any recommended specs to set the VM to for being a Ubuntu build with this setup on it only? Mainly wondering if something like 4-6gb of ram will be sufficient?

I have it set up on a tiny VM (1 GB RAM, 20 GB disk) and it seems fine:

[email protected]:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            480M     0  480M   0% /dev
tmpfs            99M   12M   88M  12% /run
/dev/vda1        19G  1.8G   16G  10% /
tmpfs           494M     0  494M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           494M     0  494M   0% /sys/fs/cgroup
tmpfs            99M     0   99M   0% /run/user/0
[email protected]:~# free -m
              total        used        free      shared  buff/cache   available
Mem:            987          53         536          11         396         778
Swap:          1023           3        1020

Bandwidth and link speed will be more important as you can easily push 100 GB/day depending on resources.

Would there be any benefit of using a cache proxy on the same host box just different vm from the fxserver? Meaning is https faster than how fxserver transfers?

Not really much of a difference, but there might be some benefits to extremely busy servers as it won’t have to serve files in addition to hundreds of players worth of traffic, but it would still be subject to the same bandwidth limits.

will this help that some of the player might get connection interrupted because another player joining?

That’s really useful, i will use this

So i setup nginx but it wont create the cache?
the Folder is just an empty one.

But where is the windows nginx solution? Nginx dont support the UDP on windows :frowning:

Guys can you help me for nginx.conf? I cant figure it