0基础自学linux运维-2.16-使用squid代理上网

需求

为了安全起见,公司内部有些服务器是不能上网的,只能是通过内部访问以增加安全性。
当需要上网的时候再使用代理上网,上网完之后需要关闭代理以保证安全性。
通过上面描述知道使用的是正向代理,我使用的是squid
一、环境说明 实验环境,服务器:centos6.x
外网ip地址为192.168.0.75,内网地址为192.168.3.75
客户端:centOS6.x
IP地址和服务器端IP地址同一网段,但是没外网IP上不了网
二、squid服务器安装 修改/etc/selinux/config文件中设置SELINUX=disabled
2.1 安装
yum install squid -y
cd /etc/squid/
cp squid.conf squid.conf.orig
2.2 修改配置 vi squid.conf
#设置为内网IP地址
acl localnet src 192.168.3.0/24
visible_hostname 192.168.3.75
#http_access deny all改为允许所有用户使用代理,或者注解掉
http_access allow all
#dns_nameservers也可以不要
dns_nameservers 114.114.114.114
2.3 启动squid #检查配置参数是否有错
squid -k parse
#初始化缓存目录,squid新版本3.1可以省略
#squid -z
/etc/init.d/squid start
netstat -alntp |grep squid
三、客户端设置 CentOS使用命令设置代理:http://blog.csdn.net/fwj380891124/article/details/42168683
里面的星号换成对应的 主机(也就是能上网的那台 VPS)的IP
vim /etc/profile
#添加
http_proxy=http://192.168.3.75:3128
export http_proxy
#使文件生效
source /etc/profile
#或者
【0基础自学linux运维-2.16-使用squid代理上网】vi /etc/profile.d/DaiLi.sh
export PATH=$PATH
http_proxy=http://192.168.3.75:3128
export http_proxy
source /etc/profile.d/DaiLi.sh
四、测试 [root@VM3 ~]# curl -I www.baidu.com
HTTP/1.0 200 OK
Date: Sun, 28 Feb 2016 17:06:03 GMT
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Set-Cookie: BAIDUID=337319A3542BDFFDA212770EAD081395:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=337319A3542BDFFDA212770EAD081395; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1456679163; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
....
有信息表示成功,也可以用yum install tree安装一下软件
配置解释:
acl Safe_ports port 80 8080 # http的端口
acl Safe_ports port 21# ftp的端口
acl Safe_ports port 443# https的端口
cache_dir aufs /data/cache 1024 16 256#缓存空间1024M大小 16个一级目录,256个子目录
cache_mem 128 MB#缓存可以使用的内存大小;放在内存中访问数据速度快;
mkdir/data/cache#创建缓存目录
chown -R squid:squid /data/cache#更改缓存目录权限
squid -z#初始化缓存目录,squid新版本3.1可以省略
/etc/init.d/squid start#启动squid服务
squid-k check#可以检测配置文件是否有错;可以简写为-kche
squid -k rec#可以重新加载配置,reconfig的简写;
service squid restart#重启squid服务;重启经常性的很慢,可以先killall squid,在启动服务;

    推荐阅读