iOS Tips

Nginx支持WebDAV

2018-11-25  本文已影响359人  9c46ece5b7bd

要想Nginx支持WebDAV,需要增加http_dav_module模块的支持。

重新编译Nginx二进制文件

# 重新编译,增加--with-http_dav_module参数
bash-4.1# ./configure --prefix=/export/servers/nginx/ --user=admin --group=admin --with-http_dav_module

# 打包后的二进制文件【会备份之前的二进制文件】
bash-4.1# ll /export/servers/nginx/sbin/nginx*
-rwxr-xr-x 1 root  root  3729261 11月 24 13:30 /export/servers/nginx/sbin/nginx
-rwxr-xr-x 1 admin admin 3679011 11月 10 07:55 /export/servers/nginx/sbin/nginx.old

修改Nginx配置文件

     location /dav {
                # 注意这里用的是root,也就是最终内容会存放在/export/Data/www/dav/目录下
        root                  /export/Data/www;
                # 用来存放客户端请求体的临时内容
        client_body_temp_path /export/Data/client_temp;
                # 指定支持的HTTP和webdav方法
                # 默认第一次使用PUT上传文件到临时目录,再一次上传时仅remove
        dav_methods PUT DELETE MKCOL COPY MOVE;
                # webdav规定仅允许在已有的目录中创建文件。该指令允许创建所有需要的中间目录
        create_full_put_path  on;
                # 设置访问权限
        dav_access            group:rw  all:r;
        limit_except GET {
            allow 127.0.0.1;
            deny  all;
        }
        }

使用webdav

bash-4.1# hostname > name.txt
bash-4.1# curl -T name.txt http://localhost/dav
<html>
<head><title>409 Conflict</title></head>
<body bgcolor="white">
<center><h1>409 Conflict</h1></center>
<hr><center>nginx/1.14.1</center>
</body>
</html>

# 错误提示很明显,我们想把文件name.txt 上传到dav目录下,而上述操作其实是想把文件上传至dav,但是dav是已存在的目录。因此服务端返回409错误
bash-4.1# cat /export/servers/nginx/logs/error.log
2018/11/24 13:55:34 [error] 8838#0: *13 "/export/Data/www/dav" could not be created (21: Is a directory), client: 127.0.0.1, server: blog.bgops.com, request: "PUT /dav HTTP/1.1", host: "localhost"

# 这样上传就ok了
bash-4.1# curl -T name.txt http://localhost/dav/
bash-4.1# curl http://localhost/dav/name.txt
37f4f7fe8054
bash-4.1# cat /export/Data/www/dav/name.txt
37f4f7fe8054

bash-4.1# hostname -i >> name.txt
bash-4.1# curl -T name.txt http://localhost/dav/
bash-4.1# curl http://localhost/dav/name.txt
37f4f7fe8054
172.17.0.2

# 从日志里我们可以看到,第一次上传文件返回码是201;第二次是204【也就是文档名称没有变化,但其实文档内容变化了】
127.0.0.1 - - [24/Nov/2018:13:55:38 +0000] "PUT /dav/name.txt HTTP/1.1" 201 25 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
127.0.0.1 - - [24/Nov/2018:13:58:21 +0000] "GET /dav/name.txt HTTP/1.1" 200 13 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
127.0.0.1 - - [24/Nov/2018:13:58:56 +0000] "PUT /dav/name.txt HTTP/1.1" 204 25 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
127.0.0.1 - - [24/Nov/2018:13:59:00 +0000] "GET /dav/name.txt HTTP/1.1" 200 24 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
上一篇 下一篇

猜你喜欢

热点阅读