我们公司采用的web架构师nginx+squid+apache,其实大家很费解为什么要这样的组合,我的理解是:
1:nginx实现了代理的作用,区分动态请求和静态请求.
2:nginx在处理静态请求的时候,能够hash请求地址,最终请求到特定的squid服务器上,实现了一个squid集群
3:网站请求的时候包含很多内容,一些不变的页面或者文件放在nginx本地.有效减少对squid和apache的请求.
根据该结构,我配置了一下(和我们公司的配置有很大不同)
nginx配置:
server {
listen 8082 ;
server_name www.nginx3.org;
error_log logs/www.nginx3.org_error.log ;
access_log logs/www.nginx3.org_access.log ;
location / {
proxy_pass http://squid;
}
}
upstream squid{
server 127.0.0.1:3128 ;
}
8082端口接收web请求,然后将其转发到squid 3128端口
squid配置:
http_port 3128
cache_log /data1/squid/var/logs/cache.log
cache_access_log /data1/squid/var/logs/access.log
cache_store_log /data1/squid/var/logs/store.log
cache_effective_user www
cache_dir ufs /data1/squid/var/cache 100 16 256
cache_swap_low 90
cache_swap_high 95
acl all src 0.0.0.0/0.0.0.0
http_access allow all
#启用反向代理
httpd_accel_host 127.0.0.1
httpd_accel_port 80
httpd_accel_single_host on
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
squid将其转发到apache的80端口进行处理
apache配置:
<VirtualHost *:80>
DocumentRoot "/data1/apache/htdocs"
DirectoryIndex index.php index.html
ServerName apacheup1.sina.com.cn
</VirtualHost>
以上配置非常简单,继续优化
//----------------------------------------- 2008-12-01优化
1:从上面的配置看,权限控制做的非常差,从新配置了一下:
acl all src 0.0.0.0/0.0.0.0
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl allow_purge src 10.71.1.0/24
acl allowed_doms dstdomain www.nginx3.org
acl allowed_methods method GET
acl allowed_methods method POST
acl allowed_methods method HEAD
acl PURGE method PURGE
acl allow_port port 80 #对原始服务器端口的访问
acl allow_port port 8080
acl localservers dstdomain .nginx.com
http_access allow PURGE localhost
http_access deny PURGE
http_access deny !allowed_methods
#http_access deny !allowed_doms
http_access deny !allow_port
http_access deny to_localhost
no_cache deny localservers
http_access allow localhost
http_access allow all
acl类型
1:ip的地址:squid能够以子网,地址范围,域名等形式编写地址.
src 表示来源地址
dst 表示原始服务器(目标)地址,假如值为域名,则在squid启动的时候会将主机转换为ip地址.
2:域名匹配,srcdomain会进行dns反向解析,影响squid解析效率
3:正则表达式
4:端口
5:http请求方法
6:协议
squid如何匹配访问控制元素:
squid在检查acl元素值时使用or逻辑,当squid找到第一个值匹配时,它停止搜索,所以将最可能匹配的值放在列表开头处,能减少延时.
访问控制规则:
对http_access和其他规则设置,squid使用and逻辑.
2:反向代理的设置
在这个例子中nginx/squid/apache单独运行在一台机器中(通过ip区分).
httpd_accel_host表示squid后台服务器的ip地址或者主机名
httpd_accel_uses_host_header:大部分squid请求会将host头部插入url里,仅对那些缺少host头部的请求,squid才使用httpd_accel_host
httpd_accel_single_host:httpd_accel_uses_host_header:决定指令squid插入url里的主机名,该指令决定squid转发它的请求到哪里.url里面的内容和host头部不会影响转发决定
3:squid access 日志
在nginx proxy代理squid的时候,使用的upstram为http://squid,所以在squid的请求日志中记录的日志也是http://squid,怎么才能改变这个值呢,一种比较笨的方法就是将upstram设置为对应的域名,比较靠谱的方法是设置proxy参数
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;