CentOS 6.9 --Squid代理服务器

   2023-02-09 学习力0
核心提示:  主机名IP地址 网关  DNS  服务类型 Mastereth0:192.168.17.130(VMnet4)eth1:192.168.30.130(NAT)192.168.30.2119.29.29.29 Squidclienteth0:192.168.17.131(VMnet4)192.168.17.131 无       服务端安装Squid服务[root@Master ~]# yum inst

 

 

主机名 IP地址  网关   DNS   服务类型 
Master

eth0:192.168.17.130(VMnet4)

eth1:192.168.30.130(NAT)

192.168.30.2 119.29.29.29  Squid
client eth0:192.168.17.131(VMnet4) 192.168.17.131  无  

 

 

 

 

 

 

服务端安装Squid服务

[root@Master ~]# yum install -y squid

配置正向代理,修改配置文件

[root@Master ~]# vim /etc/squid/squid.conf   //添加
acl localdomain src 192.168.30.0/24
http_access allow localdomain
这里修改配置文件的第二种方式是
搜索http_access deny all将deny修改为allow,同时注释掉上面添加的两行内容

启动squid服务测试

在客户端测试

CentOS 6.9 --Squid代理服务器

CentOS 6.9 --Squid代理服务器

CentOS 6.9 --Squid代理服务器

CentOS 6.9 --Squid代理服务器

CentOS 6.9 --Squid代理服务器

 

只是有点慢,还有不知道为什么打不开百度。

配置透明代理,在正向代理的基础上。

[root@Master ~]# vim /etc/squid/squid.conf
http_port 3128 transparent   #直接搜索http_port 3128然后在行尾加上一个单词即可

开启IPv4地址转发

[root@Master ~]# vim /etc/sysctl.conf 

net.ipv4.ip_forward = 1

使之生效,并配置iptables:

[root@Master ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
[root@Master ~]# service iptables start 
iptables: Applying firewall rules:                         [  OK  ]
[root@Master ~]# iptables -t nat -A POSTROUTING -s 192.168.17.0/24 -j SNAT --to 1922.168.30.130  //SNAT代理内部上网,将内部的地址全部转化为可以上网的地址 192.168.1.63
[root@Master ~]# iptables -t nat -A PREROUTING -s 192.168.17.0/24 -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128 //端口转化,来自192.168.2.0,从eth1且端口为80的数据进行数据重定向到3128,代理服务器为你工作
[root@Master ~]# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
REDIRECT   tcp  --  192.168.17.0/24      anywhere            tcp dpt:http redir ports 3128 

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  192.168.17.0/24      anywhere            to:192.168.30.130 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

重启squid去客户端测试

CentOS 6.9 --Squid代理服务器

客户端确认DNS和网关都可以ping通

CentOS 6.9 --Squid代理服务器

然后测试

使用curl -I www.sina.com或者time curl www.sina.com

CentOS 6.9 --Squid代理服务器

CentOS 6.9 --Squid代理服务器

配置反向代理加速

[root@master ~]# vim /etc/squid/squid.conf

http_access  allow all

http_port 3128 vhost #启用虚拟主机
cache_peer 192.168.17.131 parent 80 0 no-query originserver weight=1 name=a 
cache_peer 192.168.17.131 parent 81 0 no-query originserver weight=1 name=b
#no-query和originserver指明了服务器,不查询直接到源服务器
#name对前面的定义做了一个别名
cache_peer_domain a www.servera.com #访问www.servera.com将直接访问到上面定义的17.131的80端口
cache_peer_domain b www.serverb.com
cache_peer_access a allow all
cache_peer_access b allow all

重启squid

[root@master ~]# service squid restart 
Stopping squid: ................                           [  OK  ]
Starting squid: .                                          [  OK  ]

配置虚拟主机,slave作为httpd的服务端

[root@slave ~]# mkdir /var/www/html/sishen
[root@slave ~]# echo "<h1>This is serverb</h1>" > /var/www/html/index.html
[root@slave ~]# echo "<h1>This is servera</h1>" > /var/www/html/sishen/index.html
[root@slave ~]# vim /etc/httpd/conf/httpd.conf 
.......
Listen 80
Listen 81
............
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.servera.com
    DocumentRoot /var/www/html/sishen
    ServerName www.servera.com
    ErrorLog logs/dummy-host.servera.com-error_log
    CustomLog logs/dummy-host.servera.com-access_log common
</VirtualHost>
<VirtualHost *:81>
    ServerAdmin webmaster@dummy-host.serverb.com
    DocumentRoot /var/www/html
    ServerName www.serverb.com
    ErrorLog logs/dummy-host.serverb.com-error_log
    CustomLog logs/dummy-host.serverb.com-access_log common
</VirtualHost>
修改完成后,重启httpd服务

[root@slave ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]

修改C:\Windows\System32\drivers\etc\hosts文件

末尾添加如下内容:
192.168
.30.130 www.servera.com 192.168.30.130 www.serverb.com

使用物理机的IE浏览器访问测试

CentOS 6.9 --Squid代理服务器

 

CentOS 6.9 --Squid代理服务器

这里我用的Firefox查看的缓存信息,IE出了点问题,通过刷新当前页面可以查看命中缓存信息。

CentOS 6.9 --Squid代理服务器

CentOS 6.9 --Squid代理服务器

疑问:服务端配置两张网卡,第一次 我的eth0为VMnet4,eth1为NAT;这种情况不能连外网,不知道为什么。第二种情况是将两个顺序颠倒下就可以了。(网关写错了)

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 用Xshell设置CentOS密钥登录
    用Xshell设置CentOS密钥登录
    今天带来设置X shell 密钥登录  因为用的中文x shell 所以按照中文设置很简单1.点击工具(Tools)------新建用户密钥生成向导(New User key Wizard)(附图)2.然后出现下图时,记得密钥类型 选择RSA.其实选择DSA ,也能实现,二者其实是不同的算法。(我用的R
    03-08
  • centOS下安装Adobe Flash Player
    centOS下安装Adobe Flash Player
    centOS下安装Adobe Flash Player  2009-10-09 11:29:33|  分类:linux|  标签: |订阅 写出来就这么简单几步,当初刚接触Linux的时候搜索了很长时间,找了很多资料,总是出现这样那样的问题,最后终于摸索出最简单最有效的安装的方法。 随便打开一个带
    03-08
  • 部署Node.js项目(CentOS) node项目搭建
    部署Node.js项目(CentOS) node项目搭建
    操作步骤步骤 1:创建ECS实例选择操作系统为公共镜像CentOS7.2。使用root用户登录Linux实例。步骤2:部署Node.js环境—二进制文件安装该部署过程使用的安装包是已编译好的二进制文件,解压之后,在bin文件夹中就已存在node和npm,无需手工编译。安装步骤:1、
    02-10
  • centos安装python与jdk centos安装python3.7
    centos安装python与jdk centos安装python3.7
    安装python#压缩包安装[root@china ~]# yum -y install zlib*Loaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun
    02-10
  • centos6下同时安装python2和python3
    #build-essential compile packagesyum groupinstall "Development Tools"yum install openssl-develyum install zlib-develyum install make gcc gcc-c++ kernel-develhttp://unix.stackexchange.com/questions/291737/zipimport-zipimporterror-cant-decomp
    02-10
  • CentOS下查看文件和文件夹大小 linux查看文件夹
    当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择。  df可以查看一级文件夹大小、使用比例、档案系统及其挂入点,但对文件却无能为力。当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择。  df可以查
    02-10
  • centos7 离线升级/在线升级操作系统内核
    centos7 离线升级/在线升级操作系统内核
    centos7 离线升级/在线升级操作系统内核目录一.前言二.系统环境三.系统内核下载网址四.centos7离线升级系统内核1.先查看系统环境2.离线升级系统内核五.在线升级系统内核一.前言CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于
    02-10
  • centos7 rc.local脚本执行不成功
    腾讯云 centos7   配置文件/etc/rc.local的内容如下:#!/bin/sh#secu_agent init monitor, install at Thu Aug 3 11:19:41 CST 2017/usr/local/sa/agent/init_check.sh/dev/null 21/usr/sbin/ntpdate ntpupdate.tencentyun.com /dev/null 21 /usr/local/qclo
    02-10
  • 如何在centos7启动时自动挂载硬盘
    在/etc/rc.local中加入如下的语句,这样就不用每次重启后手动挂载了(后面挂载的目录根据自己的需求而定):mount  /dev/sdb1 /usr/sharedfiles/sdbmount  /dev/sdc1 /usr/sharedfiles/sdcmount  /dev/sdb1 /root/sdbmount  /dev/sdc1 /root/sdc注意:可以
    02-10
  • centos7.2 开启防火墙
    开启防火墙当我们修改了某些配置之后(尤其是配置文件的修改),firewall并不会立即生效。可以通过两种方式来激活最新配置 systemctl restart firewalld 和 firewall-cmd --reload 两种方式,前一种是重启firewalld服务,建议使用后一种“重载配置文件”
    02-10
点击排行