1. 安装nginx工具
sudo apt install nginx apache2-utils
2. 创建缓存目录
# 创建缓存目录(选择一个有足够空间的位置)
sudo mkdir -p /mnt/data/ccache-nginx
# 设置合适的权限(让NGINX可以读写)
sudo chown -R www-data:www-data /mnt/data/ccache-nginx # Ubuntu/Debian
# 确认权限
ls -la /mnt/data/ccache-nginx
3. 配置NGINX
sudo vim /etc/nginx/sites-available/ccache-server添加如下:
server {
listen 8080;
server_name localhost;
location /ccache/ {
alias /var/ccache/ccache-nginx/;
log_not_found off;
dav_methods PUT DELETE;
create_full_put_path on;
client_max_body_size 100M;
dav_access user:rw group:rw all:r;
limit_except GET HEAD {
auth_basic "CCache Remote Storage";
auth_basic_user_file /etc/nginx/ccache_htpasswd;
}
autoindex off;
}
location /health {
return 200 "OK\n";
add_header Content-Type text/plain;
}
location / {
return 403;
}
}
4. 设置身份验证(可选但推荐)
sudo htpasswd -c /etc/nginx/ccache_htpasswd ccache_user
# 会提示输入密码,如:my_secure_password_123
5. 启用配置
sudo ln -s /etc/nginx/sites-available/ccache-server /etc/nginx/sites-enabled/
sudo nginx -t # 测试语法是否ok
sudo systemctl reload nginx # 重启服务
6. 测试服务器
# 测试服务器是否响应
curl -X GET http://localhost:8080/health
# 应该返回:OK
# 测试PUT文件
curl -X PUT -d "test data" http://localhost:8080/ccache/test.txt
# 应该返回:201 Created(或204 No Content)
# 测试GET文件
curl http://localhost:8080/ccache/test.txt
# 应该返回:test data
# 测试DELETE文件
curl -X DELETE http://localhost:8080/ccache/test.txt
-------------------------------认证测试--------------------------------
# 带认证的PUT
curl -X PUT -u ccache_user:密码 -d "test" http://localhost:8080/ccache/auth_test.txt
7. 配置CCACHE客户端
# 无认证版本
export CCACHE_REMOTE_STORAGE="http://SERVER_IP:8080/ccache/"
# 或带认证版本
export CCACHE_REMOTE_STORAGE="http://ccache_user:密码@SERVER_IP:8080/ccache/"