Featured image of post webdav搭建

webdav搭建

简单几步利用Github开源项目搭建自己的webdav服务

webdav搭建

最近闲来无事,想利用一下自己的公网IP做点别的有意思的事,因为之前搭建的网盘都统统没有用了,可能是用不来吧,还是在用的samba.

就突然想到了为什么不搭建一个webdav呢!

因为不打算用apache或者nginx来搭建,就去GitHub找了一下,还真的有一个不错的项目,使用Golang。那就开干吧。

准备工作

首先需要下载这个webdav,我只贴了项目地址,因为可能到时候这个项目又更新了。

1
2
3
4
5
wget https://github.com/hacdias/webdav/releases/download/v4.1.1/linux-amd64-webdav.tar.gz
mkdir ~/.ENV/webdav	# ~/.ENV 是我个人习惯放的地方
mv linux-amd64-webdav.tar.gz ~/.ENV/webdav
tar -zxvf linux-amd64-webdav.tar.gz && rm linux-amd64-webdav.tar.gz
touch config.yml #config.{json, toml, yaml, yml} 都可以,而且它会在 ./ & /etc/webdav/下查找文件

配置文件

然后就是配置文件的内容了,我的是yaml格式的,大家可以参考一下。

1
2
3
4
5
6
7
8
9
address: 0.0.0.0
port: 8090
auth: true
tls: false
prefix: /
users:
  - username: username
    password: password
    scope: /home/username/xxx

下面的这个是官网给的详细介绍,比较全但是也有很多我用不到的配置。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
scope: .
modify: true
rules: []

# CORS configuration
cors:
  enabled: true
  credentials: true
  allowed_headers:
    - Depth
  allowed_hosts:
    - http://localhost:8080
  allowed_methods:
    - GET
  exposed_headers:
    - Content-Length
    - Content-Range

users:
  - username: admin
    password: admin
    scope: /a/different/path
  - username: encrypted
    password: "{bcrypt}$2y$10$zEP6oofmXFeHaeMfBNLnP.DO8m.H.Mwhd24/TOX2MWLxAExXi4qgi"
  - username: "{env}ENV_USERNAME"
    password: "{env}ENV_PASSWORD"
  - username: basic
    password: basic
    modify:   false
    rules:
      - regex: false
        allow: false
        path: /some/file
      - path: /public/access/
        modify: true

开机自启

这一步需要编写service文件 webdav.service.example,官网给了栗子了,我还是贴一下自己的吧。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[Unit]
Description=WebDAV server
After=network.target

[Service]
Type=simple
User=root
ExecStart=/home/frelon/.ENV/webdav/webdav --config /home/frelon/.ENV/webdav/config.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target
1
2
3
sudo systemctl daemon-reload
sudo systemctl start webdav
sudo systemctl enable webdav

然后就可以正常使用了,但是如果你觉得端口不如意,咱可以用nginx代理到80。

nginx代理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
server {
#webdav
        listen 80;
        server_name 192.168.31.100 jokeme.top;
        location / {
            proxy_pass http://127.0.0.1:8090;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
         }
}

proxy_pass下面的五个是必须的,要不然容易出事。

然后重启nginx就大功告成了。

Licensed under CC BY-NC-SA 4.0