很多小伙伴们不会Nginx反代SeaFile,抽空写个教程吧。这里教大家如何使用Nginx反代理海文SeaFile开源网盘,并且启用HTTPS开启HTTP2模式加速SeaFile,Nginx反代理有个好处是可以使用CDN加速,速度与安全肩并肩!

安装 SeaFile

具体教程请看我的上一篇教程:树莓派自搭建家庭云储存服务,海文SeaFile安装全攻略
或者查看官方英文文档(中文文档更新慢):Deploying Seafile under Linux

建立 SeaFile 启动文件

官方教程
我们直接使用启动文件运行SeaFile,官方有提供启动文件
Start Seafile at System Bootup
请查看 For systems using another init system than systemd 下的 Other Debian based Distributions

本文教程
建立 /etc/init.d/seafile 并修改下方关键信息复制到 /etc/init.d/seafile (需要把 fastcgi=true 设为 true)并赋予执行权限 chmod 775 /etc/init.d/seafile
这里是我自己的启动文件,有稍微修改,可单独启动停止 seahub/seafile 服务

1
#!/bin/sh
2
3
### BEGIN INIT INFO
4
# Provides:          seafile
5
# Required-Start:    $local_fs $remote_fs $network
6
# Required-Stop:     $local_fs
7
# Default-Start:     2 3 4 5
8
# Default-Stop:      0 1 6
9
# Short-Description: Starts Seafile Server
10
# Description:       starts Seafile Server
11
### END INIT INFO
12
13
# 请将 user 改为你的Linux用户名
14
user=root
15
16
# 请将 script_path 改为你的 Seafile 文件安装路径
17
seafile_dir=/data/ser/sof/seafile
18
script_path=${seafile_dir}/seafile-server-latest
19
seafile_init_log=${seafile_dir}/logs/seafile.init.log
20
seahub_init_log=${seafile_dir}/logs/seahub.init.log
21
22
# 若使用 fastcgi, 请将其设置为true
23
fastcgi=true
24
# fastcgi 端口, 默认为 8000. 
25
fastcgi_port=8801
26
27
case "$1" in
28
        start)
29
                sudo -u ${user} ${script_path}/seafile.sh start >> ${seafile_init_log}
30
                if [  $fastcgi = true ];
31
                then
32
                        sudo -u ${user} ${script_path}/seahub.sh start-fastcgi ${fastcgi_port} >> ${seahub_init_log}
33
                else
34
                        sudo -u ${user} ${script_path}/seahub.sh start >> ${seahub_init_log}
35
                fi
36
        ;;
37
        webstart)
38
                if [  $fastcgi = true ];
39
                then
40
                        sudo -u ${user} ${script_path}/seahub.sh start-fastcgi ${fastcgi_port} >> ${seahub_init_log}
41
                else
42
                        sudo -u ${user} ${script_path}/seahub.sh start >> ${seahub_init_log}
43
                fi
44
        ;;
45
        filestart)
46
                sudo -u ${user} ${script_path}/seafile.sh start >> ${seafile_init_log}
47
        ;;
48
        restart)
49
                sudo -u ${user} ${script_path}/seafile.sh restart >> ${seafile_init_log}
50
                if [  $fastcgi = true ];
51
                then
52
                        sudo -u ${user} ${script_path}/seahub.sh restart-fastcgi ${fastcgi_port} >> ${seahub_init_log}
53
                else
54
                        sudo -u ${user} ${script_path}/seahub.sh restart >> ${seahub_init_log}
55
                fi
56
        ;;
57
        stop)
58
                sudo -u ${user} ${script_path}/seafile.sh $1 >> ${seafile_init_log}
59
                sudo -u ${user} ${script_path}/seahub.sh $1 >> ${seahub_init_log}
60
        ;;
61
        webstop)
62
		sudo -u ${user} ${script_path}/seahub.sh $1 >> ${seahub_init_log}
63
        ;;
64
        filestop)
65
		sudo -u ${user} ${script_path}/seafile.sh $1 >> ${seafile_init_log}
66
        ;;
67
        *)
68
                echo "Usage: /etc/init.d/seafile {start|stop|restart|webstart|filestart|webstop|filestop}"
69
                exit 1
70
        ;;
71
esac

修改 SeaFile 文件服务端口

启动文件修改的端口只是 SeaFile 的网页服务还要修改文件服务的端口
修改安装目录 conf 文件夹下的 seafile.conf 文件,将服务地址修改为 127.0.0.1,端口修改为 8802,其他参数适当修改

1
[fileserver]
2
#Seafile 服务地址
3
host = 127.0.0.1
4
#Seafile 服务端口
5
port = 8802
6
#上传文件最大为多少.MB
7
max_upload_size       = 256
8
#最大下载目录限制为多少.MB
9
max_download_dir_size = 512
10
#文件索引线程
11
max_indexing_threads  = 8
12
#文件分割为多少.MB
13
fixed_block_size      = 2
14
#当用户上传文件时,文件服务器分配一个令牌来授权上传操作。
15
#该令牌默认有效1小时。通过WAN上传大型文件时,上传时间可能会超过1小时。
16
#您可以将令牌到期时间更改为更大的值。
17
web_token_expire_time = 3600
18
[quota]
19
#默认每个用户的最大空间.GB
20
#在社区版5.0.5以后,你可以以 KB, MB, GB, TB 为单位来设置默认容量。比如
21
default = 1GB
22
[history]
23
#默认文件历史
24
keep_days = 0
25
#您可以从seahub下载文件夹作为zip存档
26
#但Windows上的一些zip软件不支持UTF-8
27
#您可以使用“windows_encoding”设置来解决此问题。
28
[zip]
29
# The file name encoding of the downloaded zip file.
30
windows_encoding = iso-8859-1

修改 SeaFile 配置文件

修改安装目录 conf 文件夹下的 seahub_settings.py 文件添加两行代码

1
SERVICE_URL = 'https://www.demo.com:8443'
2
FILE_SERVER_ROOT = 'https://www.demo.com:8443/seafhttp'

配置 Nginx

我这里使用非网页常规端口8443,我的树莓派是在没用公网的环境需要经过服务器穿透而我的服务器已经有一个Nginx需要占用80,443端口所谓一山不能容二虎。这个根据自身情况而定
几个关键配置:
修改网页服务后端的 proxy_pass http://127.0.0.1:8801; 端口为启动文件处设置的端口地址
修改文件服务后端的 proxy_pass http://127.0.0.1:8802; 端口为seafile配置文件处设置的端口地址
修改静态文件后端的 root /data/ser/sof/seafile/seafile-server-latest/seahub; 为你的实际seafile安装地址的 seahub 文件夹(写错会导致页面样式文件和JS文件报404错误)
还有HTTPS证书文件和目录地址跟域名,自行修改为实际地址和域名。
因为我的网盘不对公所以我禁掉了搜索引擎的访问,有需要的请删除“## 禁止网络爬虫”处的配置,以免搜索引擎收录不了。

1
server {
2
	## 基础配置
3
	listen 8443 ssl http2;
4
	access_log off;
5
	error_log  /dev/null;
6
	server_name www.demo.com;
7
	root /data/web/dat/nginx/web/www.demo.com;
8
	index index.html;
9
10
	# certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
11
	ssl_certificate /etc/letsencrypt/live/www.demo.com/fullchain.pem;
12
	ssl_certificate_key /etc/letsencrypt/live/www.demo.com/privkey.pem;
13
	ssl_session_timeout 1d;
14
	ssl_session_cache shared:SSL:50m;
15
	ssl_session_tickets off;
16
17
	# Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
18
	ssl_dhparam /etc/letsencrypt/live/dhparam.pem;
19
20
	# intermediate configuration. tweak to your needs.
21
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
22
	ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
23
	ssl_prefer_server_ciphers on;
24
25
	# HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
26
	# add_header Strict-Transport-Security max-age=15768000;
27
28
	# OCSP Stapling ---
29
	# fetch OCSP records from URL in ssl_certificate and cache them
30
	ssl_stapling on;
31
	ssl_stapling_verify on;
32
33
	## verify chain of trust of OCSP response using Root CA and Intermediate certs
34
	#ssl_trusted_certificate /etc/letsencrypt/live/www.demo.com/root_ca_cert_plus_intermediates;
35
	
36
	## 解析 OCSP 域名的服務器
37
	resolver 8.8.8.8 8.8.4.4;
38
39
	## 禁止网络爬虫
40
	if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") 
41
	{ return 403; }
42
43
	## 禁止非法访问
44
	location /.well-known {}
45
	#location / {return 500;}
46
47
	## 网页服务后端
48
	proxy_set_header X-Forwarded-For $remote_addr;
49
	location / {
50
		fastcgi_pass    127.0.0.1:8801;
51
		fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
52
		fastcgi_param   PATH_INFO           $fastcgi_script_name;
53
		fastcgi_param   SERVER_PROTOCOL     $server_protocol;
54
		fastcgi_param   QUERY_STRING        $query_string;
55
		fastcgi_param   REQUEST_METHOD      $request_method;
56
		fastcgi_param   CONTENT_TYPE        $content_type;
57
		fastcgi_param   CONTENT_LENGTH      $content_length;
58
		fastcgi_param   SERVER_ADDR         $server_addr;
59
		fastcgi_param   SERVER_PORT         $server_port;
60
		fastcgi_param   SERVER_NAME         $server_name;
61
		fastcgi_param   REMOTE_ADDR         $remote_addr;
62
		fastcgi_param   HTTPS               on;
63
		fastcgi_param   HTTP_SCHEME         https;
64
	}
65
66
	# 文件服务后端
67
	location /seafhttp {
68
		rewrite ^/seafhttp(.*)$ $1 break;
69
		proxy_pass http://127.0.0.1:8802;
70
		proxy_connect_timeout  36000s;
71
		proxy_read_timeout  36000s;
72
		proxy_send_timeout  36000s;
73
		send_timeout  36000s;
74
	}
75
76
	## 静态文件后端
77
	location /static {
78
		rewrite ^/static(.*)$ /media$1 break;
79
		root /data/ser/sof/seafile/seafile-server-latest/seahub;
80
	}
81
}

启动 Seafile

启动 Seafile sudo /etc/init.d/seafile start
启动 Nginx sudo /etc/init.d/nginx start
设置Seafile开机启动 sudo update-rc.d seafile defaults
设置Seafile开机启动 sudo update-rc.d nginx defaults

END

静态文件后端的 seahub 目录下的文件可以整个CP到又拍云或者七牛云后由Nginx重定向即可实现半CDN加速。