-

贤钟的博客

一个人的奋斗
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

网站访问太慢,准备用用Squid实现反向代理进行加速

Posted on 2009-11-16 00:24  贤钟  阅读(3616)  评论(2编辑  收藏  举报

 

网站访问太慢,准备用用Squid实现反向代理进行加速,看中Squid主要是因为Squid主要的功能是存储。

服务器上分配了4G的空间用作缓存数据,其实分配1G就够用,我看了一下网站主目录一共才600M文件, 其中300M的html文件,就是全部缓存html 才占用 400M左右。

Squid的配置文件挺复杂的,不过填写几个常用的选项就可以了。

 

 我是通过 yum  安装的Squid,今天查了点资料发现配置文件好像不一样, 那么怎么查看安装的Squid版本呢 ?

1、squid -v

得到结果 Squid Cache: Version 2.6.STABLE21

2、# yum install squid
addons                                                   |  951 B     00:00    
base                                                     | 2.1 kB     00:00    
extras                                                   | 1.1 kB     00:00    
lxlabslxupdate                                           |  951 B     00:00    
lxlabsupdate                                             |  951 B     00:00    
updates                                                  | 1.9 kB     00:00    
Setting up Install Process
Package 7:squid-2.6.STABLE21-3.el5.i386 already installed and latest version
Nothing to do

OK 查到版本了! 

 

 

编辑好配置文件后执行验证

#squid -k parse

没有反应? 

哈哈 有反应说明配置的有问题。

如果出现这样的错误

  出现ERROR:
  WARNING: Cannot write log file: /var/log/squid/cache.log
  /var/log/squid/cache.log: Permission denied
  messages will be sent to 'stderr'.
  2009/11/15 17:49:09| Creating Swap Directories
  FATAL: Failed to make swap directory /var/log/squid/cache: (13) Permission denied

  给目录相应的权限
  chmod 777 -R /var/log/squid
  #chown squid:squid -R /var/log/squid   

这里我们没有建立squid。

 

在这里我所以的日志都记录在/var/log/squid里面。

输入

#squid -z  建立很多缓存目录。

Create swap directories 是创建SWAP目录的! 也就是cache_dir 中的目录

 

cache_dir ufs /var/spool/squid 4096 216 256   这里我们修改成4G。

这里 1级目录是 216 个, 二级目录是 256 个, 一共产生了  216 * 256 =55296 个目录。 好像目录值设置的有点大?

 

squid -Nd1 命令在windows版本下没用作用,
执行以后显示:

2009/11/15 01:18:24| Squid is already running!  Process ID 1813

能够看到Squid 已经运行,Process ID 1813。 时间好像不对,美国那边是凌晨1点啊。 我感觉应该是16号的1点才对,

去网上查查时区:

国家 城市    与北京时差
美国 旧金山 -16
美国 纽约    -13

果然如此,美国哟比我们晚十几个小时。

 

通过:ps aux |grep squid  查看是否启动

# ps aux |grep squid
root      1811  0.0  0.9   7828  1256 ?        Ss   Nov14   0:00 squid -D
squid     1813  0.0  4.6  10056  6100 ?        S    Nov14   0:03 (squid) -D
squid     1815  0.0  0.2   1512   288 ?        Ss   Nov14   0:00 (unlinkd)
root      5086  0.0  0.3   1832   492 pts/1    R+   02:01   0:00 grep squid
#

 

OK 现在没有架设DNS,就修改hosts文件。

打开C:\WINDOWS\system32\drivers\etc 目录 用记事本打开hosts文件

这里我们添加一条记录

127.0.0.1       localhost
204.12.243.243 http://www.pipc.net/

 然后ping 一下 没有修改之前的IP是 123.15.61.77  修改之后变成 204.12.243.243说明修改成功。

配置文件里设置成  http_port 82 所以连接82端口访问看看。

然后访问 http://www.pipc.net:82/

出现了错误:

---------------------------------------------------------

 

ERROR

The requested URL could not be retrieved


 

While trying to process the request:

GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Accept-Language: zh-cn
If-Modified-Since: Tue, 10 Nov 2009 15:16:08 GMT
If-None-Match: "199670046"
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Host: www.pipc.net
Connection: Keep-Alive
Cookie: VisitNum=28; rtime=3; ltime=1258270237890; cnzz_eid=96733582-1257782007-; cnzz_a1312416=27; vw1312416=%3A16051036%3A58505722%3A89713009%3A74252916%3A85181899%3A76984275%3A88219231%3A74289695%3A260336122%3A86739399%3A86734164%3A38405681%3A86734178%3A53975987%3A168281111%3A53975986%3A90648389%3A90648962%3A; sin1312416=none


The following error was encountered:

  • Invalid Request

Some aspect of the HTTP Request is invalid. Possible problems:

  • Missing or unknown request method
  • Missing URL
  • Missing HTTP Identifier (HTTP/1.0)
  • Request is too large
  • Content-Length missing for POST or PUT requests
  • Illegal character in hostname; underscores are not allowed

Your cache administrator is admin@pipc.net.


 

Generated Sun, 15 Nov 2009 14:00:56 GMT by my.pipc.net (squid/2.6.STABLE21)

 

---------------------------------------------------------

 

难道是82端口不行, 没有关系我们修改成80 看看。

再次访问没有报错,显示的结果不对,显示的是我以前上传的测试页面,不是 squid程序。

好像80端口有程序运行。 没有关系运行pstree能够看到系统有哪些进程:你可以用  pstree  -a 看到进程路径

# pstree
init-+-authdaemond---authdaemond
     |-crond
     |-klogd
     |-kloxo.httpd
     |-lighttpd
     |-mysqld_safe---mysqld---{mysqld}
     |-qmail-send-+-qmail-clean
     |            |-qmail-lspawn
     |            |-qmail-rspawn
     |            `-splogger
     |-sh---kloxo.exe---kloxo.exe
     |-snmpd---{snmpd}
     |-squid---squid---unlinkd
     |-sshd-+-3*[sshd---bash]
     |      |-sshd---bash---pstree
     |      `-sshd-+-bash
     |             `-sftp-server
     |-syslogd
     |-5*[tcpserver]
     |-tinydns
     `-xinetd

就是lighttpd这个web服务器啦 。

输入# lighttpd
2009-11-15 04:08:53: (server.c.548) No configuration available. Try using -f option. 

奇怪怎么会提示这个错误 ? 

输入#  lighttpd -h
lighttpd-1.4.20 (ssl) (Oct 30 2008 03:46:10) - a light and fast webserver
usage:
 -f <name>  filename of the config-file
 -m <name>  module directory (default: /usr/lib/lighttpd)
 -p         print the parsed config-file in internal form, and exit
 -t         test the config-file, and exit
 -D         don't go to background (default: go to background)
 -v         show version
 -V         show compile-time features
 -h         show this help

不管了杀掉算了,

 

 

ps aux | grep lighttpd
apache    4146  0.0  1.4   6348  1952 ?        S    03:45   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
lxlabs    4177  0.0  0.6   5212   848 ?        S    03:45   0:00 /usr/local/lxlabs/ext/lxlighttpd/sbin/kloxo.httpd -f /usr/local/lxlabs/kloxo/file/lighttpd.conf
root      4295  0.0  0.3   1832   496 pts/4    S+   04:13   0:00 grep lighttpd

输入 :# netstat -anp


tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4146/lighttpd      
tcp        0      0 0.0.0.0:82                  0.0.0.0:*                   LISTEN      3981/(squid)       
tcp        0      0 0.0.0.0:53                  0.0.0.0:*                   LISTEN      391/tcpserver      
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      379/xinetd         
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN      379/xinetd         
tcp        0      0 204.12.243.243:443          0.0.0.0:*                   LISTEN      4146/lighttpd     

 

可以看到 占用80端口的的确是lighttpd ,我们的squid占用的是82端口。

停止lighttpd

# lighttpd stop
2009-11-15 04:41:50: (server.c.548) No configuration available. Try using -f option.
还是不行,报错。

# /etc/init.d/lighttpd stop
Stopping lighttpd:                                         [  OK  ]

必须通过init.d 停止和启动。

测试一下OK:

# /etc/init.d/lighttpd start
Starting lighttpd:                                         [  OK  ]
# /etc/init.d/lighttpd stop
Stopping lighttpd:                                         [  OK  ]

 

如果安装lighttpd时是编译安装源码而没有 init script的,或是 init script不起作用时,我们可以使用下面的命令

# killall lighttpd


 

停止squid

# squid -k shutdown

修改配置文件端口设置为80

启动

#squid restart

 ps aux |grep squid
root      4500  0.0  0.3   1832   492 pts/4    S+   04:55   0:00 grep squid

再来一次:

# /etc/init.d/squid restart
Stopping squid: .                                          [  OK  ]
Starting squid: .                                          [  OK  ]

启动成功

 

再次访问 http://www.pipc.net/  还是一样的错误。 郁闷啊!

 

为什么还不行啊,再改改配置吧。

 

 

这次提示

-------------------------------------------------------------------------------------

ERROR

The requested URL could not be retrieved


 

While trying to retrieve the URL: http://www.pipc.net/

The following error was encountered:

  • Unable to forward this request at this time.

This request could not be forwarded to the origin server or to any parent caches. The most likely cause for this error is that:

  • The cache administrator does not allow this cache to make direct connections to origin servers, and
  • All configured parent caches are currently unreachable.

Your cache administrator is admin@pipc.net.


 

Generated Sun, 15 Nov 2009 15:36:21 GMT by www.pipc.net (squid/2.6.STABLE21)

-------------------------------------------------------------------------------------

这次错误页面有变化了, 关键是 “Unable to forward this request at this time. ” 没有关系 google一下。

网上说这个错误和 cache_peer_domain 配置项有关系。

 

1、 never_direct allow all选择打开

2、 端口后加上 vhost vhost

3、 添加了 cache_peer_domain www-iis http://www.pipc.net/ 

4、 添加    cache_peer 123.15.61.77 parent 80 0 no-query originserver name=www-iis

 

# /etc/init.d/squid restart 一下

 

^_^ 终于可以了,终于看到主页啦。 配置成功。  、

随便点点,看看有没有问题! 

 

哦,天哪! 出现了“找不到服务器或 DNS 错误”
是html页面,难道说有些页面第一次抓取会失败, 重新刷新一下就好了。

 

squid.conf 配置分享一下。

 

 https://files.cnblogs.com/pipc/squid.rar


--
---