您的位置:首页 > 运维架构 > 反向代理

如何在Linux上用Squid搭建代理服务器

2015-08-20 16:19 573 查看
转:http://www.linuxidc.com/Linux/2013-10/91346.htm

在Linux上用Squid搭建代理服务器,这个东东全世界都是,但是有一些细节问题在网上说的不明不白的,折腾了半天搞好了,如鲠在喉不吐不快。

一台电脑linux系统,上不了网,于是在另外一台可以上网的linux上面搭建一个代理服务器,步骤如下:

下载:squid-3.2.9.tar.bz2

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2013年资料/10月/13日/如何在Linux上用Squid搭建代理服务器

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

上传到服务器后解压:

解压: tar -vxjf squid-3.2.9.tar.bz2

解压后生成目录:squid-3.2.9

进入目录翻看文档INSTALL:

xxxx> more INSTALL

To build and install the Squid Cache, type:

% ./configure --prefix=/usr/local/squid

% make all

% make install

To run a Cache, you will need to:

1. customize the squid.conf configuration file:

% vi /usr/local/squid/etc/squid.conf

2. Initalise the cache:

% /usr/local/squid/sbin/squid -z

3. start the cache:

% /usr/local/squid/sbin/squid

If you want to use the WWW interface to the Cache Manager, copy

the cachemgr.cgi program into your httpd server's cgi-bin

directory.

安装步骤:

./configure --prefix=/usr/local/squid

make all

sudo make install(因为要拷贝到系统目录,需要root权限,所以sudo了,你也可以root登录执行,我是Ubuntu的系统,所以用sudo,有root权限就行)

检查配置文件:

sudo vi /usr/local/squid/etc/squid.conf

配置项1:

# Squid normally listens to port 3128

http_port 3128

配置项2:

acl localnet src 192.168.0.0/16

http_access allow localnet

配置项3:

# Uncomment and adjust the following to add a disk cache directory.

cache_dir ufs /usr/local/squid/var/cache/squid 100 16 128

cache_mem 32 MB (这一条必须配置)

否则你就会遭遇报错: 2013/10/12 16:16:55 kid1| WARNING cache_mem is larger than total disk cache space!

安装好了以后,系统中新建了一个用户squid,在组中一查,发现属于nobody组的:

cat /etc/passwd|grep squid

cat /etc/group|grep 65534

安装squid的所在目录是:/usr/local/squid

我闲得没事干,直接改了所属用户为squid:nobody

sudo chown -Rf squid:nobody /usr/local/squid

建立cache的时候,对下面目录需要nobody用户权限,这个是网上没有说的很清楚的地方,折腾了我半天:

sudo chown -Rf nobody /usr/local/squid/var/cache/

sudo chown -Rf nobody /usr/local/squid/var/logs/

否则你会遭遇:

WARNING: Cannot write log file: /usr/local/squid/var/logs/cache.log

FATAL: Failed to make swap directory /usr/local/squid/var/cache/squid/00: (13) Permission denied

初始化squid.conf里配置的cache目录,就是建立了一堆的目录:

sudo /usr/local/squid/sbin/squid -z

在前台启动squid,并输出启动过程

sudo /usr/local/squid/sbin/squid -N -d1

显示ready to server reques,则启动成功。可以键入ctrl+c,停止squid,并以后台运行的方式启动。

我没有在配置文件中配置DNS,而是在 /etc/resolv.conf 中配置:

domain site

nameserver x.x.x.x

所以打印出来的日志中就这样的:

2013/10/12 16:42:13| Adding nameserver x.x.x.x from /etc/resolv.conf

squid从这个配置文件中读取了dns配置来用。

启动squid后台运行

sudo /usr/local/squid/sbin/squid -s

检查一下进程是否存在:ps -ef|grep squid

通过squid客户端查看squid运行状态

/usr/local/squid/bin/squidclient -h 127.0.0.1 -p 3128 mgr:info

那台不能上网的机器配置如下:

export http_proxy=http://192.168.199.235:3128/

可以把这句写到你的启动文件中,比如什么.profile或者.bashrc,或者/etc/profile等等。

取消:unset http_proxy

测试一下能不能上网了:

wget www.linuxidc.com

能down下来文件就大功告成了!

推荐阅读:

配置Squid代理http和rsync http://www.linuxidc.com/Linux/2013-05/84642.htm

Squid:实现高速的Web访问 http://www.linuxidc.com/Linux/2013-04/83512.htm

CentOS 6.2 编译安装Squid 配置反向代理服务器 http://www.linuxidc.com/Linux/2012-11/74529.htm

Squid在企业网中的应用 http://www.linuxidc.com/Linux/2012-10/71818.htm

Squid 的详细介绍请点这里

Squid 的下载地址请点这里

在实际安装过程中,不用创建用户,也不用修改配置文件,默认的就可以,只需要将 chmod 777 var -R ,同时运行:/usr/local/squid/sbin/squid -N -d1就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: