Skip to content

Instantly share code, notes, and snippets.

@akhilman
Last active March 12, 2024 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akhilman/c3542309d07081226de455ff6160ab50 to your computer and use it in GitHub Desktop.
Save akhilman/c3542309d07081226de455ff6160ab50 to your computer and use it in GitHub Desktop.
Fixes for nginx's webdav to work with gvfs clients.
# Define lua functions
init_by_lua_block {
function is_dir(path)
local f = io.open(path, "r")
if not f then
return false
end
local ok, err, code = f:read(1)
f:close()
return code == 21
end
}
server {
listen 80;
listen [::]:80;
server_name servak.local 192.168.16.90;
root /var/www/html;
sendfile on;
sendfile_max_chunk 1m;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
location /webdav {
alias /srv/webdav;
client_body_temp_path /srv/webdav/.tmp;
client_max_body_size 100G;
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_ext_methods PROPFIND OPTIONS LOCK;
create_full_put_path on;
dav_access group:rw all:rw;
autoindex on;
charset UTF-8;
allow 192.168.16.0/24;
deny all;
rewrite_by_lua_block {
local directory_requested = is_dir(ngx.var.request_filename)
-- URL should end with "/" if directory requested
if (directory_requested or ngx.req.get_method() == "MKCOL")
and not ngx.re.match(ngx.var.uri, "^.*/$") then
local uri = ngx.re.sub(ngx.var.uri, "^(.*?)/?$", "$1/")
ngx.req.set_uri(uri, true)
end
local dst = ngx.req.get_headers()["Destination"]
if dst then
-- Remove hostname from destination
dst = ngx.re.sub(dst, "^(https?://.+?)?(/.*)$", "$2")
-- Rename the folder Destination does not end with a /, it is necessary headers-more-nginx-module
if directory_requested then
dst = ngx.re.sub(dst, "^(.*?)/?$", "$1/")
end
ngx.req.set_header("Destination", dst)
end
-- PROPPATCH no instruction processing PROPFIND.
if ngx.req.get_method() == "PROPPATCH" then
ngx.req.set_method(ngx.HTTP_PROPFIND)
end
}
# Stop upload, copy, move, mkdir hidden files
access_by_lua_block {
local method = ngx.req.get_method()
local file = nil
if (method == "COPY" or method == "MOVE") then
file = ngx.req.get_headers()["Destination"]
elseif (method == "PUT" or method == "MKCOL") then
file = ngx.var.uri
end
if (file and ngx.re.match(file, "/\\.[^/]*/?$")) then
ngx.status = ngx.HTTP_FORBIDDEN
ngx.exit(ngx.HTTP_FORBIDDEN)
end
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment