码迷,mamicode.com
首页 > 其他好文 > 详细

squid代理(一)--传统代理

时间:2018-09-14 23:59:59      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:amp   重复   emc   访问日志   nsa   epoll   程序   lan   cal   

squid代理(一)--传统代理

Web代理的工作机制
缓存网页对象,减少重复请求

代理的基本类型
传统代理:适用于Internet,客户端上网需明确指定服务端IP以及端代理口
透明代理:适用于共享上网网关,客户端上网不需指定服务端IP以及端代理口

使用代理的好处
提高Web访问速度
隐藏客户机的真实IP地址

编译安装squid
centos7服务器一枚
准备好squid-3.5.23.tar.gz源码包
[root@localhost ~]# tar -zxf squid-3.5.23.tar.gz
[root@localhost ~]# cd squid-3.5.23/
[root@localhost squid-3.5.23]# ./configure --prefix=/usr/local/squid --sysconfdir=/etc --enable-linux-netfilter --enable-async-io=240 --enable-default-err-language=Simplify_Chinese --disable-poll --enable-epoll --enable-gnuregex
--prefix= ##安装位置
--sysconfdir= ##单独将配置文件修改到其他目录
--enable-linux-netfilter ##使用内核过滤
--enable-async-io=值 ##异步I/O,提升存储性能
--enable-default-err-language=Simplify_Chinese ##错误信息的显示语言
--disable-poll与--enable-epoll ##关闭默认使用poll模式,开启epoll模式提升性能
--enable-gnuregex ##使用GNU正则表达式

[root@localhost squid-3.5.23]# make && make install ##编译安装

安装完后创建连接文件,创建用户和组
[root@localhost squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ ##软连接ln -s [源文件或目录] [目标文件或目录]
[root@localhost squid-3.5.23]# useradd -M -s /sbin/nologin squid ##创建squid用户,并且不允许登陆系统
[root@localhost squid-3.5.23]# chown -R squid:squid /usr/local/squid/var/ ##指定文件的拥有者改为指定的用户或组

squid的配置文件
[root@localhost squid-3.5.23]# vi /etc/squid.conf ##在coredump_dir /usr/local/squid/var/cache/squid上边添加一下两条配置
http_port 3128 ##默认自带(用来指定代理的监听端口)
cache_effective_user squid ##指定squid程序用户,用来设置初始化,运行时缓存的账号,否则启动不成功
cache_effective_group squid
:wq
[root@localhost squid-3.5.23]# squid -k parse ##检查配置文件是否正确

启动、停止squid
[root@localhost ~]# squid -z ##-z选项用来初始化缓存目录
[root@localhost ~]# squid ##启动squid服务
[root@localhost ~]# netstat -anpt |grep squid ##监听3128端口是否开启
tcp6 0 0 :::3128 :::* LISTEN 82716/(squid-1)

编辑squid服务脚本
[root@localhost ~]# vi /etc/init.d/squid
#!/bin/bash
chkconfig: 2345 90 25
Squid Server Scrip

PID="/usr/local/squid/var/run/squid.pid "
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"

case "$1" in
start)
netstat -anpt | grep squid &>/dev/null
if [ $? -eq 0 ]
then
echo "Squid正在运行"
else
$CMD
fi
;;
stop)
kill -9 cat $PID &> /dev/null
rm -rf $PID &>/dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
echo "squid正在运行...."
else
echo "squid未启动"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
)
echo "请输入正确参数{start|stop|reload|restart|check|status}"
;;
esac
:wq
[root@localhost ~]# chmod +x /etc/init.d/squid ##添加执行权限
[root@localhost ~]# chkconfig --add squid ##添加为系统服务
[root@localhost ~]# systemctl restart squid ##重启测试脚本
[root@localhost ~]# netstat -anpt |grep squid
tcp6 0 0 :::3128 :::
LISTEN 82942/(squid-1)

传统代理
squid服务器的配置:添加如下配置
[root@localhost ~]# vim /etc/squid.conf
http_access allow all ##表示允许任意客户机使用代理,放在http_access deny all之前
reply_body_max_size 10 MB ##表示允许下载的最大文件大小(10MB)
:wq

重启squid服务
[root@localhost ~]# systemctl restart squid

[root@localhost ~]# squid -k reconfigure

验证代理
在Windows中设置代理
打开IE浏览器-->工具-->Intelnet选项-->连接-->局域网设置-->"勾上"代理服务器的勾-->地址中写代理服务器的IP,
端口中写"3128"-->确定,访问百度即可

在Linux中设置代理,需要通过环境变量来制定代理服务器的地址、端口
[root@localhost ~]# vi /etc/profile ##配置文件中添加一下内容
HTTP_PROXY=http://192.168.10.30:3128 ##http协议
HTTPS_PROXY=http://192.168.10.30:3128 ##https协议
FTP_PROXY=http://192.168.10.30:3128 ##ftp协议
NO_PROXY=192.168.1.,192.168.2. ##对这两个局域网网段不适用代理
export HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY

[root@localhost ~]# source /etc/profile ##刷新配置文件

代理服务器验证方法
1、chakansquid访问日志的新增记录
[root@localhost ~]# tail -f /usr/local/squid/var/logs/access.log
1536938536.649 3 192.168.10.100 TCP_MISS/404 525 GET http://192.168.10.40/noindex/css/fonts/L
ightItalic/OpenSans-LightItalic.eot? - HIER_DIRECT/192.168.10.40 text/html

2、查看被访问端的web访问日志的新增记录
[root@localhost ~]# tail -f /var/log/httpd/access_log
192.168.10.30 - - [14/Sep/2018:23:22:17 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.eot? HT
TP/1.1" 404 240 "http://192.168.10.40/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; T
rident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;
InfoPath.2)"

squid代理(一)--传统代理

标签:amp   重复   emc   访问日志   nsa   epoll   程序   lan   cal   

原文地址:http://blog.51cto.com/liuliheng/2175439

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!