返回

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

   
摘要GPT
摘要小助理暂时失联跑路啦……😜

安装

1、安装Nginx

这里使用的是军哥的LNMP 单独安装Nginx

wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz -O lnmp2.0.tar.gz && tar zxf lnmp2.0.tar.gz && cd lnmp2.0 && ./install.sh nginx

2、CDN配置

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

vi /etc/hosts
192.168.1.100    www.moerats.com

然后创建nginx配置文件moerats.com.conf

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

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

proxy_cache_path /usr/local/nginx/caches/www.moerats.com levels=1:2 keys_zone=xiaoz:50m inactive=30m max_size=50m;
server {
    listen 80;
    server_name www.moerats.com;
    charset utf-8,gbk;
        location / {
        proxy_set_header Accept-Encoding "";
           proxy_pass https://www.moerats.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 xiaoz;
           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;
    }
}

参数说明:

/usr/local/nginx/caches/www.moerats.com:为缓存目录

levels:指定该缓存空间有两层hash目录,第一层目录为1个字母,第二层为2个字母。

keys_zone=xiaoz:50m:为缓存空间起个名字,这里取名为“xiaoz”,后面的50m指内存缓存空间

inactive=30m:如果30分钟内该资源没有被访问则删除

max_size=50m:指硬盘缓存大小为50MB

proxy_cache_valid:指定状态码缓存时间,前面写状态码,后面写缓存时间。

3. 然后重启Nginx生效

/etc/init.d/nginx reload

4、Https配置

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

proxy_cache_path /data/wwwroot/caches/www.moerats.com levels=1:2 keys_zone=Rats:50m inactive=30m max_size=50m;
server {
    listen 443 ssl http2;
    ssl_certificate    /home/moerats.com.crt;
    ssl_certificate_key    /home/moerats.com.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.moerats.com;
    access_log /data/wwwlogs/moerats.com_nginx.log combined;
   
    charset utf-8,gbk;
        location / {
        proxy_set_header Accept-Encoding "";
           proxy_pass https://www.moerats.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 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;
}

你也可以使用多台VPS,利用CloudXNS智能解析,将不同地区的DNS解析到最近的VPS上,进行CND加速。

知识共享许可证 CC BY-NC-SA 4.0
最后更新于 2024-06-15 18:08
使用 Hugo 构建
主题 hugo-magic小洋葱 魔改 由 Jimmy 设计
Written by Human, Not by AI