Nginx+squid实现微型CDN搭建

微型CDN搭建

1.目的:

实现对nginx和squid的结合使用,同时加深对两者的理解。深度剖析代理服务器的功能特点。

2.要求:

1) 选取1台虚拟机作为源站(web服务器),在源站用nginx部署web服务器,并放上.txt、.jpg文件
2) 选取1台虚拟机1台作为Nginx反向服务器
3) 选取1台作为Squid服务器,并配置txt缓存1个月,jpg文件不缓存

3.服务器:

源站服务器: 192.168.163.138(内) 192.168.163.139(内) CentOS 7 x64

Squid服务器: 192.168.8.2(外) 192.168.163.137(内) www.squid.com CentOS 7 x64

Nginx代理服务器: 192.168.8.245(外) www.hubiao-homework.com CentOS 7 x64

4.软件:

nginx:

作用:用于作为源站服务代理和CDN代理(必须,源站可用其他web服务代替)

安装命令:

yum -y install nginx

使用命令:

#方式一:
nginx #启动
nginx -s stop 			#停止
nginx -s reload 		#重启
#方式二:
service nginx start 	#启动
service nginx stop  	#停止
service nginx restart 	#重启

注意:用哪一种方式启动,就应以哪一种方式停止,否则会操作失败

squid:

作用:用于作为源站缓存服务(必须

安装命令:

yum -y install squid

使用命令:

service squid start		#启动
service squid stop		#停止
service squid restart	#重启

lrzsz:

作用:用于在xshell上传或者下载文件(非必要)

安装命令:

yum -y install lrzsz

使用命令:

#上传文件
rz 			#回车之后会弹出文件选择框,选择你想要上传的文件即可   不可上传文件夹 如有需要,可先压缩再上传
#下载文件
sz fileName.fileType	#回车,选择本地保存的目录,确认即可

意外情况:

nginx 安装不成功,可更新yum源。

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum -y install nginx

5.环境配置:

防火墙配置:

打开22、80、3128端口

firewall-cmd --permanent --zone=public --add-port=22/tcp 
firewall-cmd --permanent --zone=public --add-port=80/tcp 
firewall-cmd --permanent --zone=public --add-port=3128/tcp 

重启防火墙

firewall-cmd --reload

查看端口开启情况

firewall-cmd --list-port

Nginx主配置文件:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

源站配置:

server {
        listen 80;
        server_name 192.168.163.139;
        root /etc/nginx/conf.d;

        location / {
                index index.html;
        }
}

server {
        listen 80;
        server_name www.hubiao.com;
        root /etc/nginx/conf.d;

        location / {
                index  index.html;
        }
}

Squid配置:

http_access     allow all
http_port       3128 transparent
http_port       80 accel vhost

cache_peer        192.168.163.138 parent 80 0 originserver round-robin weight=1
cache_peer_access 192.168.163.138 allow all

cache_peer        192.168.163.139 parent 80 0 originserver round-robin weight=1
cache_peer_access 192.168.163.139 allow all

acl allow_txt url_regex -i \.(txt)
cache allow allow_txt
refresh_pattern -i \.(txt)$ 43200 100% 43200

acl acl_nocache url_regex -i .*
cache deny acl_nocache

coredump_dir /var/spool/squid

cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 2000 16 256
access_log /var/log/squid/access.log
visible_hostname squid-server
cache_mgr biao.hu@baishan.com

Nginx代理配置:

upstream myServer {
        server 192.168.8.2:80;
}


server {
        listen 80;
        server_name www.hubiao-homework.com;

        location / {
                proxy_pass http://myServer;
        }
}

本地host配置:

6.访问Nginx代理服务器

www.hubiao-homework.com

www.hubiao-homework.com/test.txt

www.hubiao-homework.com/test.jpg

7.关闭源站

8.curl 测试

curl 192.168.8.2 -I

curl 192.168.8.2/index.html -I

结论:html文件未被缓存

curl 192.168.8.2/test.jpg -I

结论:jpg文件未被缓存

curl 192.168.8.2/test.txt -I

结论:txt文件被缓存

9.总结

本次实验是通过nginx+squid实现微型CDN服务,在这个过程中,源站采用nginx代理两个源站;squid用来作为源站缓存服务;最后用另一台服务器开启nginx服务来代理squid服务器,实现微型CDN服务的搭建。

实验步骤一般可以先实现源站搭建---->squid服务搭建----->squid服务代理源站----->nginx代理服务搭建----->nginx代理squid服务器;最后通过访问nginx代理服务器,实现对源站的访问,如果关闭源站,可以对squid服务器缓存的资源进行访问,如果没有,则访问失败。

### 配置与集成 NginxSquid 当考虑将 NginxSquid 结合用于 Web 缓存和代理时,可以利用两者的优点来构建高效的网络架构。Nginx 是一款高性能 HTTP 和反向代理服务器,而 Squid 则是一个功能强大的缓存和代理软件。 #### 使用 Nginx 作为前端负载均衡器和静态资源处理 为了提高网站响应速度并减轻源站压力,在部署方案中可让 Nginx 处理静态文件请求以及分发流量到多个后端服务实例上[^1]: ```nginx http { upstream backend_servers { server 192.168.0.1; server 192.168.0.2; } server { listen 80; server_name example.com; location /static/ { alias /var/www/html/static/; } location / { proxy_pass http://backend_servers; } } } ``` #### 设置 Squid 进行透明缓存加速 为了让 Squid 成为有效的缓存层,需对其进行适当配置实现对特定 URL 或者整个站点的内容进行缓存操作。下面展示了一个简单的 `squid.conf` 文件片段,其中定义了一些基本参数以便启动 Squid 并使其能够正常工作: ```bash # Define cache directory cache_dir ufs /var/spool/squid 1000 16 256 # Allow local network access acl our_networks src 192.168.0.0/16 http_access allow our_networks # Set maximum object size that can be cached (in KB) maximum_object_size 1 MB # Cache refresh patterns for different types of content refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern . 0 20% 4320 ``` #### 将两者结合起来形成高效的服务链路 最后一步就是把上述两个组件串联起来共同发挥作用。可以通过修改 Nginx配置使得它将某些类型的请求转发给位于其后的 Squid 实例来进行进一步处理。这不仅有助于减少重复的数据传输量,还可以显著提升用户体验质量: ```nginx location ~* \.(gif|jpg|png|css|js)$ { resolver 8.8.8.8; # Use Google DNS or any other reliable one set $upstream "http://localhost:3128"; # Assuming Squid runs on port 3128 locally proxy_set_header Host $host; proxy_pass $upstream; } ``` 通过这种方式,Nginx 负责接收客户端连接并将图片、样式表等容易变化较少的资源交给 Squid 来做本地化存储;而对于动态页面或其他不适合长期保存的信息,则继续由原来的业务逻辑负责生成返回给用户浏览器显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值