Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2245832
  • 博文数量: 454
  • 博客积分: 4860
  • 博客等级: 上校
  • 技术积分: 6374
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 10:08
个人简介

10年工作经验,专研网站运维。

文章分类

全部博文(454)

文章存档

2017年(11)

2016年(13)

2015年(47)

2014年(36)

2013年(147)

2012年(64)

2011年(136)

分类: LINUX

2012-02-08 15:15:19

思路:
squid是帮助你访问目标服务器,不是转换你的访问地址比如:html.cxkj.com为192.168.1.10。而是接收到的你的访问申请比如html.cxkj.com,直接用你的这个访问地址html.cxkj.com,直接问配置文件里指定的服务器,比如:192.168.1.10m。以上说明就是下面的实例的说明:
cache_peer              192.168.1.10   parent 80 0 no-query round-robin originserver
cache_peer_domain       192.168.1.10    html.cxkj.com test.cxkj.com
第一行是大体指定转发给谁,那台机器,第二行是指定,什么请求发给谁,请求html.cxkj.com转发给192.168.1.10.
重点:
我开始配置错了,发现虚拟主机怎么都访问不了,其实是我对转发设置的部分理解错了。我开始是这样设置的:
cache_peer              html.cxkj.com   parent 80 0 no-query round-robin originserver
cache_peer              test.cxkj.com   parent 80 0 no-query round-robin originserver
cache_peer_domain       html.cxkj.com   192.168.1.202
cache_peer_domain       test.cxkj.com   192.168.1.202
cache_peer_access       html.cxkj.com   allow   all
cache_peer_access       test.cxkj.com   allow   all
开始理解成:访问202的主机都解析到html.cxkj.com活test.cxkj.com.其实不是这样的,就应该:
用正常的域名访问,解析到squid的外网接口,然后应该利用ip地址解析到相应的地址,解析的时候带着http头信息。squid自己就访问对应的apache了。
应该这样设置:
cache_peer              192.168.1.10   parent 80 0 no-query round-robin originserver

cache_peer_domain       192.168.1.10    html.cxkj.com test.cxkj.com

cache_peer_access       192.168.1.10    allow   all

解释一下:
把所有访问解析到192.168.1.10和192.168.1.241,然后具体的对照cache_peer_domain对应表,对应那个域名走哪个ip地址的服务器,最后cache_peer_access字段允许即可。

状况描述:
两台服务器,一台squid:192.168.1.202域名:sendmail.cxkj.com端口号:80,一台apache:192.168.1.10域名:html.cxkj.com.端口号:80(一下所有配置都是对squid的服务器的配置,没有涉及到apache的服务器。)

1、tar jxvf squid-3.1.15.tar.bz2
2、安装
./configure --prefix=/usr/local/squid
make
make install
3、建立squid用户
useradd squid
4、建立相关文件和文件夹及赋予权限
touch      /usr/local/squid/var/logs/cache.log
touch   /usr/local/squid/var/logs/access.log    squid
touch  /usr/local/squid/var/logs/store.log
touch  /usr/local/squid/var/logs/squid.pid
chown -R squid.squid    /usr/local/squid/var/logs/cache.log
chown -R squid.squid   /usr/local/squid/var/logs/access.log squid
chown -R squid.squid  /usr/local/squid/var/logs/store.log
chown -R squid.squid  /usr/local/squid/var/logs/squid.pid
chmod -R 777 /usr/local/squid/var/logs/cache.log
5、编辑基本网络
vim /etc/hosts    添加:
192.168.1.202   sendmail.cxkj.com
127.0.0.1       sendmail.cxkj.com
vim /etc/resolv.conf
nameserver 192.168.1.10   指定DNS
hostname  sendmail.cxkj.com
/etc/init.d/iptables stop
setenforce 0
把和squid冲突的端口都关闭。
6、编辑配置文件
vim /usr/local/squid/etc/squid.conf
#
visible_hostname sendmail.cxkj.com
dns_nameservers 192.168.1.10
cache_mgr       root@sendmail.cxkj.com
cache_effective_user    squid
cache_effective_group   squid
# Recommended minimum configuration:
#
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.1.0/24 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access allow manager

# Deny requests to certain unsafe ports
#http_access allow      all

# Deny CONNECT to other than secure SSL ports
#http_access allow CONNECT !SSL_ports

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access allow all

# Squid normally listens to port 3128
#http_port 80   accel   vhost   vport
http_port 80 accel   vhost vport

# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /usr/local/squid/var/cache 10000 16 256

# Leave coredumps in the first cache dir
coredump_dir /usr/local/squid/var/cache

# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320

cache_log       /usr/local/squid/var/logs/cache.log
access_log      /usr/local/squid/var/logs/access.log    squid
cache_store_log /usr/local/squid/var/logs/store.log
pid_filename    /usr/local/squid/var/logs/squid.pid

cache_mem       1024    MB
cache_swap_low  90
cache_swap_high 95
maximum_object_size     512000  KB
minimum_object_size     4096    KB

#cache_peer     192.168.1.10    parent 80 0 no-query originserver round-robin
#cache_peer 192.168.1.243 parent 80 0 originserver
#cache_peer html.cxkj.com parent 80 0 originserver
#cache_peer              html.cxkj.com  parent 80 0 no-query round-robin originserver
#cache_peer_domain       html.cxkj.com   sendmail.cxkj.com
cache_peer              192.168.1.10   parent 80 0 no-query round-robin originserver
#cache_peer             192.168.1.10   parent 80 0 no-query round-robin originserver
cache_peer_domain       192.168.1.10    html.cxkj.com test.cxkj.com
#cache_peer_domain      192.168.1.10    test.cxkj.com
cache_peer_access       192.168.1.10    allow   all
#cache_peer_access      192.168.1.10    allow   all
   保存退出。
最后修改/etc/hosts文件。
把squid添加到系统启动项
 编辑 /etc/rc.d/rc.local
 添加如下行: /usr/local/squid/sbin/squid -s
 利用Runc脚本……..
ulimit -HSn 65535
ulimit -n
vim /etc/rc.d/rc.local
ulimit -HSn 65535
保存退出。
7、相关配置
初始化目录
#/usr/local/squid/sbin/squid -z
检查语法:
#/usr/local/squid/sbin/squid -k parse
开启服务:
#/usr/local/squid/sbin/squid –NCd1 (前台正式运行,用于调试非常好.)
 #/usr/local/squid/sbin/squid        (后台运行)
更新配置文件:
 # /usr/local/squid/sbin/squid -k reconfig (更新配置文件后更新)
关闭squid
/usr/local/squid/sbin/squid  -k interrupt
滚动日志文件
/usr/local/squid3/sbin/squid -k rotate

检测系统运行状态
/usr/local/squid/bin/squidclient -h 192.168.1.202 -p 80 mgr:info
清空缓存:
/usr/local/squid/bin/squidclient -h 192.168.1.202 -p 80 -m http://sendmail.cxkj.com/
8、配置完成:
最后检测:

用Linux自带的工具检测一下Http头看Cache生效没

 #curl -I http://192.168.1.30/

 如信息中出现有以下这一行就代表缓存生效了。

 X-Cache: HIT from squid

 如果是

 X-Cache: MISS from squid

 就代表缓存还没储存,或者是没生效.

3,查看你的日志文档。

 #more /usr/local/squid/var/logs/access.log | grep TCP_MEM_HIT

 该指令可以看到在squid运行过程中,有那些文件被squid缓存到内存中,并返回给访问用户。

 #more /usr/local/squid/var/logs/access.log | grep TCP_HIT

 该指令可以看到在squid运行过程中,有那些文件被squid缓存到cache目录中,并返回给访问用户。

 #more /usr/local/squid/var/logs/access.log | grep TCP_MISS

 该指令可以看到在squid运行过程中,有那些文件没有被squid缓存,而是现重原始服务器获取并返回给访问用户。

 关于 TCP_XXXX 等参数及代表的信息,请参看《squid中文权威指南》13.2.1 章节。

阅读(1809) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~