Merge pull request #829 from SkynetLabs/v2-skylinks-cache

V2 skylinks cache
This commit is contained in:
Christopher Schinnerl 2021-06-04 12:29:43 +02:00 committed by GitHub
commit b35ea89ff6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -417,6 +417,13 @@ server {
proxy_pass http://siad; proxy_pass http://siad;
} }
location /skynet/resolve {
include /etc/nginx/conf.d/include/cors;
proxy_set_header User-Agent: Sia-Agent;
proxy_pass http://siad;
}
location ~ "^/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" { location ~ "^/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" {
include /etc/nginx/conf.d/include/cors; include /etc/nginx/conf.d/include/cors;
include /etc/nginx/conf.d/include/proxy-buffer; include /etc/nginx/conf.d/include/proxy-buffer;
@ -435,13 +442,25 @@ server {
set $skylink $2; set $skylink $2;
set $path $3; set $path $3;
# v2 support
set $skylink_v1 $skylink;
set $skylink_v2 $skylink;
access_by_lua_block { access_by_lua_block {
-- disable cache if this is skylink v2 -- disable cache if this is skylink v2
local isBase32v2 = string.len(ngx.var.skylink) == 55 and string.sub(ngx.var.skylink, 0, 2) == "04" local isBase32v2 = string.len(ngx.var.skylink) == 55 and string.sub(ngx.var.skylink, 0, 2) == "04"
local isBase64v2 = string.len(ngx.var.skylink) == 46 and string.sub(ngx.var.skylink, 0, 2) == "AQ" local isBase64v2 = string.len(ngx.var.skylink) == 46 and string.sub(ngx.var.skylink, 0, 2) == "AQ"
if isBase32v2 or isBase64v2 then if isBase32v2 or isBase64v2 then
ngx.var.nocache = 1 # nginx expects 0/1 for boolean local res = ngx.location.capture("/skynet/resolve/" .. ngx.var.skylink_v2)
if res.status == ngx.HTTP_OK then
local json = require('cjson')
local resolve = json.decode(res.body)
ngx.var.skylink_v1 = resolve.skylink
else
ngx.say(res.body)
ngx.exit(res.status)
end
end end
-- this block runs only when accounts are enabled -- this block runs only when accounts are enabled
@ -455,6 +474,17 @@ server {
end end
} }
# in case siad returns location header and we have skylink v2, we need to replace the skylink v1 with v2
header_filter_by_lua_block {
if ngx.var.skylink_v1 ~= ngx.var.skylink_v2 then
if ngx.header.location then
local path = string.match(ngx.header.location, "[^/?]+(.*)");
ngx.header.location = ngx.var.skylink_v2 .. path
end
ngx.header["Skynet-Skylink"] = ngx.var.skylink_v2
end
}
# register the download in accounts service (cookies should contain jwt) # register the download in accounts service (cookies should contain jwt)
log_by_lua_block { log_by_lua_block {
-- this block runs only when accounts are enabled -- this block runs only when accounts are enabled
@ -478,7 +508,7 @@ server {
proxy_read_timeout 600; proxy_read_timeout 600;
proxy_set_header User-Agent: Sia-Agent; proxy_set_header User-Agent: Sia-Agent;
# proxy this call to siad /skynet/skylink/ endpoint (make sure the ip is correct) # proxy this call to siad /skynet/skylink/ endpoint (make sure the ip is correct)
proxy_pass http://siad/skynet/skylink/$skylink$path$is_args$args; proxy_pass http://siad/skynet/skylink/$skylink_v1$path$is_args$args;
} }
location @base32_subdomain { location @base32_subdomain {

View File

@ -3,7 +3,7 @@ proxy_cache skynet; # cache name
slice 1m; slice 1m;
proxy_http_version 1.1; # upgrade if necessary because 1.0 does not support byte-range requests proxy_http_version 1.1; # upgrade if necessary because 1.0 does not support byte-range requests
proxy_set_header Range $slice_range; # pass slice range to proxy proxy_set_header Range $slice_range; # pass slice range to proxy
proxy_cache_key $uri$arg_format$arg_attachment$slice_range; # use just the uri path, format and attachment args and slice range proxy_cache_key $skylink_v1$path$arg_format$arg_attachment$slice_range; # use just the uri path, format and attachment args and slice range
proxy_cache_min_uses 3; # cache responses after 3 requests of the same file proxy_cache_min_uses 3; # cache responses after 3 requests of the same file
proxy_cache_valid 200 206 24h; # cache 200 and 206 responses for 24 hours proxy_cache_valid 200 206 24h; # cache 200 and 206 responses for 24 hours
proxy_cache_lock on; # queue cache requests for the same resource until it is fully cached proxy_cache_lock on; # queue cache requests for the same resource until it is fully cached