Skip to content


Squid带验证(NCSA)的透明代理

NCSA验证辅助器相对流行,这归咎于它的简单性和历史原因。它将用户名和密码存储在一个单独的文本文件里,类似于Unix的/etc/passwd文件。这个密码文件格式最初是作为NCSA HTTP服务器项目的一部分发展而来的。在squid.conf里,只须指定密码文件的路径作为程序的单一命令行参数。squid已经发布到squid 3.x,这里仍然使用相对稳定的squid-2.6.STABLE23

一.安装squid
wget http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE23.tar.gz
tar zxvf squid-2.6.STABLE23.tar.gz
cd squid-2.6.STABLE23

编译优化参考
http://en.gentoo-wiki.com/wiki/Safe_Cflags/Intel#Core_2_Duo.2FQuad.2C_Xeon_51xx.2F53xx.2F54xx.2F3360.2C_Pentium_Dual-Core_T23xx.2B.2FExxxx.2C_Celeron_Dual-Core

gcc –version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50)

CHOST=”x86_64-pc-linux-gnu”
CFLAGS=”-march=core2 -mtune=generic -O2 -pipe”
CXXFLAGS=”${CFLAGS}”

export CHOST=”x86_64-pc-linux-gnu”
export CFLAGS=”-march=core2 -mtune=generic -O2 -pipe”
export CXXFLAGS=”${CFLAGS}”


ulimit -HSn 65536


./configure –prefix=/opt/squid-2.6.STABLE23 –with-maxfd=65536 –with-pthreads –enable-dlmalloc –enable-poll –enable-useragent-log –enable-referer-log –with-large-files –enable-default-err-language=Simplify_Chinese –enable-err-languages=”Simplify_Chinese English” –disable-internal-dns –enable-auth=”basic” –enable-basic-auth-helpers=NCSA
make
make install

NCSA认证需加编译时加上–enable-auth=”basic” –enable-basic-auth-helpers=NCSA

二.配置squid.conf文件
配置软链接方便管理

cd /opt/
ln -s ./squid-2.6.STABLE23 ./squid

去除squid配置文件的注释

cat /opt/squid/etc/squid.conf
cat /opt/squid/etc/squid.conf.default |grep -v ^# |grep -v ^$ > /opt/squid/etc/squid.conf

或者编辑个新的,对外3128端口
vi /opt/squid/etc/squid.conf

#squid信息
visible_hostname proxy
#联系人
cache_mgr administrator
http_port 3128 transparent

# 单机
icp_port 0

# 分配给squid的内存
cache_mem 500 MB

cache_swap_low 90
cache_swap_high 95

# 超过此值不被缓存
maximum_object_size 500 KB

# 装入内存的文件大小上限
maximum_object_size_in_memory 200 KB

# 缓存类型,缓存目录,大小,一二级目录
cache_dir ufs /opt/squid/spool 4000 16 256

# 支持header的vary
cache_vary on

# 进程文件
pid_filename /var/log/squid/squid.pid

# 使用apache格式日志
emulate_httpd_log on

# 日志;设none可禁用
logformat combined %>a %ui %un [%tl] “%rm %ru HTTP/%rv” %Hs %h” “%{User-Agent}>h” %Ss:%Sh

cache_store_log none
cache_log /var/log/squid/cache.log
access_log /var/log/squid/access.log combined
#access_log none

auth_param basic program /opt/squid/libexec/ncsa_auth /opt/squid/etc/passwd
acl auth_user proxy_auth REQUIRED

acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8

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

hierarchy_stoplist cgi-bin ?
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY

#不要相信ETag 因为有gzip
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache

http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

#只充许认证用户
http_access allow auth_user

http_access deny all
#icp_access allow all

cachemgr_passwd disable config shutdown authenticator offline_toggle
#cachemgr_passwd none info stats/objects
#cachemgr_passwd secretPP all

# ip限制
#acl OverConnLimit maxconn 16
#http_access deny OverConnLimit

#squid使用的用户组和用户
cache_effective_user nobody
cache_effective_group nobody
tcp_recv_bufsize 65535 bytes

# 隐藏squid的版本信息
header_access Via deny all
#显示服务器的ip,隐藏真实客户ip
header_access X-Forwarded-For deny all

coredump_dir /opt/squid-2.6.STABLE23/var/cache

# 出错提示
error_directory /opt/squid/share/errors/Simplify_Chinese

三.相关目录

mkdir /opt/squid/spool
chown nobody:nobody /opt/squid/spool
mkdir /var/log/squid
chown nobody /var/log/squid

初始化
/opt/squid/sbin/squid -z

2011/04/13 17:29:54| Creating Swap Directories

启动
/opt/squid/sbin/squid -s

四,squid常用命令
1,初始化你在 squid.conf 里配置的 cache 目录
#squid/sbin/squid -z
如果有错误提示,请检查你的 cache目录的权限。
2,对你的squid.conf 排错,即验证 squid.conf 的 语法和配置。
#squid/sbin/squid -k parse
如果squid.conf 有语法或配置错误,这里会返回提示你,如果没有返回,恭喜,可以尝试启动squid。
3,在前台启动squid,并输出启动过程。
#squid/sbin/squid -N -d1
如果有到 ready to server reques,恭喜,启动成功。
然后 ctrl + c,停止squid,并以后台运行的方式启动它。
4,启动squid在后台运行。
#squid/sbin/squid -s
这时候可以 ps -A 来查看系统进程,可以看到俩个 squid 进程。
5,停止 squid
#squid/sbin/squid -k shutdown
这个不用解释吧。
6,重引导修改过的 squid.conf
#squid/sbin/squid -k reconfigure
这个估计用的时候比较多,当你发现你的配置有不尽你意的时候,可以随时修改squid.conf,然后别忘记对你的 squid.conf排错,然后再执行此指令,即可让squid重新按照你的 squid.conf 来运行。
7,把squid添加到系统启动项
编辑 /etc/rc.d/rc.local
添加如下行:
echo “65535″>/proc/sys/fs/file-max
ulimit -HSn 65535
/opt/squid/sbin/squid -s
当然,并不是每个人都喜欢这种启动方式,你可以用你最习惯的方式;或者把它安装为服务。

#查看运行信息
/opt/squid/bin/squidclient -h 127.0.0.1 -p 3128 mgr:info

#crontab分割日志
20 0 * * * /opt/squid/sbin/squid -k rotate

五.iptables
添加3128为进入充许,INPUT为链名
iptables -A INPUT -p tcp -m tcp –dport 3128 -j ACCEPT
保存iptables
/etc/init.d/iptables save

六.NCSA认证用户
生成用户及密码
htpasswd -c /opt/squid/etc/passwd user_c1g
添加用户
htpasswd /opt/squid/etc/passwd user_andy
修改用户密码可以vi编辑删除后再添加

七.htpasswd获取
htpasswd的获取有点麻烦,可以编译个apache来得到,在bin目录下有这个文件

wget http://apache.etoak.com/httpd/httpd-2.2.22.tar.gz
tar zxvf httpd-2.2.22.tar.gz
cd httpd-2.2.22
./configure –preifx=/opt/httpd
make
make install
#移动文件
cp /opt/httpd/bin/htpasswd /opt/squid/libexec/
#删除apache
rm -rf /opt/httpd

八.浏览器上配置代理
傲游可以写入用户和密码,支持黑名单
firefox加Foxyproxy标准版,支持黑白名单但不能写入密码
360,ie,chrome相对不够方便点

九.去除验证的透明代理
squid.conf中去掉NCSA认证就是个透明代理

acl auth_user proxy_auth REQUIRED
auth_param basic program /opt/squid/libexec/ncsa_auth /opt/squid/etc/passwd
http_access allow auth_user

#把deny 改成allow

http_access deny all

参考:squid权威指南

Posted in Squid/varnish.

Tagged with , .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.