CDN内容分发网络

本文详细介绍了如何在Linux环境中安装和配置Varnish作为CDN内容分发网络的一部分,包括Varnish的安装、配置文件修改、端口设置以及Apache默认发布目录的调整。此外,还展示了Nginx的配置方法,以实现与Varnish的集成,提升网站性能。
摘要由CSDN通过智能技术生成

一 cdn内容分发网络

  软件安装

Varnish安装:
官网:http://varnish-cache.org/
软件下载:https://developer.aliyun.com/mirror/
# yum install -y 
varnish-4.0.5-1.el7.x86_64.rpm
varnish-libs-4.0.5-1.el7.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm
jemalloc-devel-3.6.0-1.el7.x86_64.rpm

 

二  varnish

varnish工作原理

1 软件安装

varnish-4.0.5-1.el7.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm        varnish-libs-4.0.5-1.el7.x86_64.rpm
jemalloc-devel-3.6.0-1.el7.x86_64.rpm

yum install  -y *

 2 修改配置文件

cd /etc/varnish/

vim default.vcl 

###############

#

This is an example VCL file for Varnish.

#

It does not do anything by default, delegating control to the

# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#

See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/

and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

Marker to tell the VCL compiler that this VCL has been adapted to the

new 4.0 format.

vcl 4.0;

Default backend definition. Set this to point to your content server.

backend web1 {
    .host = "172.25.5.2";
    .port = "80";
}

backend web2 {
    .host = "172.25.5.3";
    .port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend_hint = web1;
                #return(pass);
        }
        elseif (req.http.host ~ "^bbs.westos.org") {
                set req.backend_hint = web2;
                #return(pass);
        }
        else {
                return(synth(404,"Not in cache"));
        }

    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {

Happens after we have read the response headers from the backend.

​    #

Here you clean the response headers, removing silly Set-Cookie headers

and other mistakes your backend does.

}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT from westos cache";
        }
        else {
        set resp.http.X-Cache = "MISS from westos cache";
        }
        return (deliver);
}

    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.

3 修改端口:

vim varnish.params

VARNISH_LISTEN_PORT=80

## server2 /server3

 

 4 修改apache默认发布目录

cd /var/www/html

cat index.html

server2:www.westos.org

server3: bbs.westos.org

## 物理机

修改本地解析文件

vim /etc/hosts

172.25.7.1  www.westos.org   bbs.westos.org   westos.org

测试:

curl  www.westos.org -I

 

## server4安装nginx

编辑文件vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
 	upstream  westos {
    	server 172.25.5.3:80;
	server 172.25.5.2:80;
	}
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    #access_log  logs/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #gzip  on;
    
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
server {
	listen 80;
	server_name www.westos.org;
	
	location / {
		proxy_pass http://westos;
	}
}

}

启动nginx

## server1

 

#

This is an example VCL file for Varnish.

#

It does not do anything by default, delegating control to the

builtin VCL. The builtin VCL is called when there is no explicit

return statement.

#

See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/

and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

Marker to tell the VCL compiler that this VCL has been adapted to the

new 4.0 format.

vcl 4.0;

Default backend definition. Set this to point to your content server.

#backend web1 {

.host = "172.25.5.2";

.port = "80";

#}

backend web2 {
    .host = "172.25.5.3";
    .port = "80";
}
backend web3 {
    .host = "172.25.5.4";
    .port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend_hint = web3;
                return(pass);
        }
        elseif (req.http.host ~ "^bbs.westos.org") {
                set req.backend_hint = web2;
                #return(pass);
        }
        else {
                return(synth(404,"Not in cache"));
        }

    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT from westos cache";
        }
        else {
        set resp.http.X-Cache = "MISS from westos cache";
        }
        return (deliver);
}

    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.

 

 

 

 

 

测试:

curl www.westos.org 

三 nginx+varnish

## server3:

安装编译nginx

systemctl stop httpd 关闭apache

tar zxf nginx-1.20.1.tar.gz 

cd nginx-1.20.1/

yum install pcre-devel gcc openssl-devel -y 安装依赖插件

./configure  --with-http_realip_module --with-http_ssl_module

make -j2

make install

cd /usr/local/nginx/

ln -s /usr/local/nginx/sbin/nginx  /usr/local/bin/

2 编辑nginx文件

cd conf

vim nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    #access_log  logs/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #gzip  on;
    
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root   html;
            index  index.html index.htm;
        real_ip_header    X-Forwarded-For;
            real_ip_recursive on;
            set_real_ip_from 172.25.5.0/24;




 	   }
 	
 	    #error_page  404              /404.html;
 	
 	    # redirect server error pages to the static page /50x.html
 	    #
 	    error_page   500 502 503 504  /50x.html;
 	    location = /50x.html {
 	        root   html;
 	    }
 	
 	    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 	    #
 	    #location ~ \.php$ {
 	    #    proxy_pass   http://127.0.0.1;
 	    #}
 	
 	    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 	    #
 	    #location ~ \.php$ {
 	    #    root           html;
 	    #    fastcgi_pass   127.0.0.1:9000;
 	    #    fastcgi_index  index.php;
 	    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 	    #    include        fastcgi_params;
 	    #}
 	
 	    # deny access to .htaccess files, if Apache's document root
 	    # concurs with nginx's one
 	    #
 	    #location ~ /\.ht {
 	    #    deny  all;
 	    #}
 	}


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3 启动nginx

nginx -t 语法检测

nginx 启动应用

4 测试:

物理机

curl www.westos.org

 

5查看日志

cd ..

cat /usr/local/nginx/log/access.log

172.25.5.250 - - [13/Jul/2021:14:19:34 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.61.1"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值