1. 南猫Home
  2. cj

使用Nginx反向代理,自建CDN加速节点

说明:博客刚刚搬到了美西,感觉速度不如香港,于是准备拿香港的服务器自建CDN加速了,对于自建CND,之前讲过Fikker,查看:CentOS安装Fikker自建CDN系统,支持Https/反向代理/防CC攻击,使用非常方便,功能也很强大,不过免费版不支持页面缓存、也不支持HTTP/2,所以放弃了。就使用Nginx反向代理实现自建CDN加速了,这里分享下方法。

安装

1、安装Nginx

#这里使用的是军哥的lnmp
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh nginx

2、CDN配置
假如我需要对www.southcat.net搭建CDN节点,数据放在192.168.1.100,需要先修改hosts指向,告知CDN节点去哪里去获取网站数据,也就是回源地址,做如下修改:

vi /etc/hosts
192.168.1.100    www.southcat.net

然后创建nginx配置文件southcat.net.conf

#创建缓存目录
mkdir -p /data/wwwroot/caches/www.southcat.net
#设置缓存目录权限
chown -R www:www /data/wwwroot/caches/www.southcat.net
#创建southcat.net.conf
vi /usr/local/nginx/conf/vhost/southcat.net.conf

southcat.net.conf中添加下面的内容,缓存目录/缓存时间请根据实际情况调整。

proxy_cache_path /data/wwwroot/caches/www.southcat.net levels=1:2 keys_zone=Rats:50m inactive=30m max_size=50m;
server {
    listen 80;
    server_name www.southcat.net;
    charset utf-8,gbk;
        location / {
        proxy_set_header Accept-Encoding "";
           proxy_pass https://www.southcat.net;
           proxy_redirect off;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_cache Rats;
           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/www.southcat.net:为缓存目录
levels:指定该缓存空间有两层hash目录,第一层目录为1个字母,第二层为2个字母。
keys_zone=Rats:50m:为缓存空间起个名字,这里取名为“Rats”,后面的50m指内存缓存空间
inactive=30m:如果30分钟内该资源没有被访问则删除
max_size=50m:指硬盘缓存大小为50MB
proxy_cache_valid:指定状态码缓存时间,前面写状态码,后面写缓存时间。

然后重启Nginx生效

lnmp nginx reload

3、Https配置
如果是https网站,配置文件参考:

proxy_cache_path /data/wwwroot/caches/www.southcat.net levels=1:2 keys_zone=Rats:50m inactive=30m max_size=50m;
server {
    listen 443 ssl http2;
    ssl_certificate    /home/southcat.net.crt;
    ssl_certificate_key    /home/southcat.net.key;
    ssl_session_timeout 1d;
    ssl_session_cache builtin:1000 shared:SSL:10m;
    ssl_dhparam /data/ssl/dhparam.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    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';
    ssl_prefer_server_ciphers on;
    ssl_stapling on;
    ssl_stapling_verify on;
    server_name www.southcat.net;
    access_log /data/wwwlogs/southcat.net_nginx.log combined;
   
    charset utf-8,gbk;
        location / {
        proxy_set_header Accept-Encoding "";
           proxy_pass https://www.southcat.net;
           proxy_redirect off;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_cache Rats;
           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;
    return 301 https://$host$request_uri;
}



原创文章,作者:南猫,如若转载,请注明出处:https://southcat.net/2041/


不妨点个广告再走嘛