说明
这段时间感觉在美国的博客站访问很慢,于是想通过一台闲置的阿里云服务器来实现CDN加速。
安装NGINX
首先需要安装nginx,这里使用了一台阿里云作为测试
sudo apt-get install nginx
CDN配置
首先需要添加hosts指向
vim /etc/hosts
加入新的指向(需要加速哪些域名就需要加入多少条指向)
xxx.xxx.xxx.xxx www.rayfalling.com xxx.xxx.xxx.xxx rayfalling.com
然后创建对应的缓存目录
mkdir -p /data/wwwroot/caches/rayfalling.com
授予nginx读写权限
chown -R www-data:www-data /data/wwwroot/caches/rayfalling.com/
Nginx配置
首先先复制一份原始的配置文件(http配置)
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/rayfalling.com.conf
然后添加以下内容 vim /etc/nginx/sites-available/rayfalling.com.conf
proxy_cache_path /data/wwwroot/caches/rayfalling.com levels=1:2 keys_zone=Rayfalling:100m inactive=120m max_size=200m; server { listen 80; server_name rayfalling.com; location / { proxy_set_header Accept-Encoding ""; proxy_pass https://rayfalling.com; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache Rayfalling; proxy_cache_valid 200 304 30m; proxy_cache_valid 301 24h; proxy_cache_valid 500 502 503 504 0s; proxy_cache_valid any 1s; proxy_cache_min_uses 1; expires 12h; } }
参数说明
/data/wwwroot/caches/rayfalling.com:为缓存目录 levels:指定该缓存空间有两层hash目录,第一层目录为1个字母,第二层为2个字母。 keys_zone=Rayfalling:100m:为缓存空间起个名字,这里取名为“Rayfalling”,后面的100m指内存缓存空间 inactive=120m:如果120分钟内该资源没有被访问则删除 max_size=200m:指硬盘缓存大小为200MB proxy_cache_valid:指定状态码缓存时间,前面写状态码,后面写缓存时间。
同步SSL证书
因为源站启用了HTTP2并且通过Let’s encrypt申请了泛域名证书,因此CDN也需要启用SSL证书,但是Let’s encrypt只能在一台服务器上申请,所以CDN节点需要定时同步源站上的SSL证书。这里我选择通过Rsync来同步证书。当然也可以选择通过git的方式来拉取。
首先先在源服务器和CDN节点上安装Rsync,Ubuntu默认自带Rsync
apt-get install rsync
创建配置文件
mkdir -p /etc/rsync.d touch /etc/rsync.d/rsyncd.conf chmod 600 /etc/rsync.d/rsyncd.conf
注意:Ubuntu下rsync在/etc/systemd/system/multi-user.target.wants/rsync.service
启动的配置文件路径为/etc/rsyncd.conf
touch /etc/rsyncd.conf chmod 600 /etc/rsyncd.conf
编辑/etc/rsyncd.conf使rsync以守护模式运行
vim /etc/rsyncd.conf
gid = root uid = root max connections = 5 log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock [ssl] path = /etc/letsencrypt ignore = errors read only = yes write only = no list = true auth users = rayfalling secrets file = /etc/rsync.d/server.pass
因为Let’s encrypt申请的证书目录在开启了autorenew的情况下,指向的是符号链接,所以我们需要同步整个目录
添加rsync同步所需的用户名和密码
echo 'your_username:your_password' > /etc/rsync.d/server.pass chmod 600 /etc/rsync.d/server.pass
启动守护进程
systemctl start rsync
查看启动状态
systemctl status rsync ● rsync.service - fast remote file copy program daemon Loaded: loaded (/lib/systemd/system/rsync.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-12-23 17:31:53 CST; 5s ago Main PID: 712 (rsync) Tasks: 1 (limit: 2317) CGroup: /system.slice/rsync.service └─712 /usr/bin/rsync --daemon --no-detach Dec 23 17:31:53 wanghaiwei systemd[1]: Started fast remote file copy program daemon.
可以看到守护进程已经启动成功了,接下来配置客户端的定时同步
先创建Rsync同步所需的密码文件
mkdir -p /etc/rsync.d/ cd /etc/rsync.d/ echo 'your_password' > your_username.pass chmod 600 /etc/rsync.d/your_username.pass
执行命令
rsync -arvz --password-file=/etc/rsync.d/your_username.pass [email protected]::ssl /data/ssl
执行成功,证书同步完毕,接下来设置下crontab定时任务
将上面执行的代码输入到一个sh脚本文件中
echo 'rsync -arvz --password-file=/etc/rsync.d/your_username.pass [email protected]::ssl /data/ssl' > sync_ssl.sh chmod +x sync_ssl.sh 添加定时任务 crontab -e 00 00 * * * /path/to/sync_ssl.sh
修改Nginx配置
SSL证书同步完毕后我们就能开启http/2和ssl访问了
proxy_cache_path /data/wwwroot/caches/rayfalling.com levels=1:2 keys_zone=Rayfalling:100m inactive=120m max_size=200m; server { ssl_certificate /data/ssl/live/rayfalling.com/fullchain.pem; ssl_certificate_key /data/ssl/live/rayfalling.com/privkey.pem; listen 443 ssl http2 default_server; server_name rayfalling.com; location / { proxy_set_header Accept-Encoding ""; proxy_pass https://rayfalling.com; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache Rayfalling; proxy_cache_valid 200 304 30m; proxy_cache_valid 301 24h; proxy_cache_valid 500 502 503 504 0s; proxy_cache_valid any 1s; proxy_cache_min_uses 1; expires 12h; } } server { listen 80 default_server; server_name rayfalling.com; return 301 https://$host$request_uri; }
启用nginx配置文件
cd /etc/nginx/sites-enabled/ ln -s ../sites-available/rayfalling.com.conf systemctl reload nginx.service
后记
由于现在阿里云对于HTTPS的封锁也很厉害(国内备案太麻烦了),具体情况参见这里,因此我决定放弃使用阿里云(大陆)服务器,以后可能会来考虑香港服务器或者就使用CloudFlare了吧。
2020.3.5更新
现在发现Rfchost的三网优化线路是真的厉害
参考链接
https://www.moerats.com/archives/575/
https://linux.cn/article-5945-1.html
本文地址: 使用Nginx反向代理实现CDN