服务器搭建CDN加速节点 使用Nginx反向代理

2018-04-0613:26:21 2

服务器搭建CDN加速节点 使用Nginx反向代理

1、安装Nginx

这里使用lmnp

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.cesafe.com搭建CDN节点,数据放在192.168.1.10,需要先修改hosts指向,告知CDN节点去哪里去获取网站数据,也就是回源地址,做如下修改:

vim /etc/hosts
192.168.1.10    www.cesafe.com

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

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

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

HTTP版:

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

 

HTTPS版:

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

参数说明:

/data/wwwroot/caches/www.cesafe.com:为缓存目录
levels:指定该缓存空间有两层hash目录,第一层目录为1个字母,第二层为2个字母。
keys_zone=Rats:50m:为缓存空间起个名字,这里取名为“Rats”,后面的50m指内存缓存空间
inactive=30m:如果30分钟内该资源没有被访问则删除
max_size=50m:指硬盘缓存大小为50MB
proxy_cache_valid:指定状态码缓存时间,前面写状态码,后面写缓存时间。

配置完成后重启nginx即生效

lnmp nginx reload

 

CE安全网
CE安全网广告位招租

发表评论

您必须登录才能发表评论!

目前评论:2   其中:访客  2   CE安全网官方  0

    • avatar CE安全网 4

      TEST 测试

      • avatar CE安全网 4

        测试