Squid Proxy

摘要:
useradd-s/sbin/nologinerbu&&echo123456|passwd--stdinerbuyum-yinstallsquidvim/etc/squid/squid.confacllanhome1src192.168.0.2/32http_accessallowlanhmoe1systemctlrestartsquid&&ss-tnlp|grep3128&&systemctlenablesquidsystemctlstopfirewalld&&systemctldisablefirewalld建议我们在搭建客户端的时候最好使用自己擅长的操作系统,我比较擅长centos,所以就假设我们的服务端是centos,因为ubuntu不太方便,比如ubuntu默认不允许root直接登录,普通用户默认拥有sudo到root的权限,squid默认的配置文件废话太多,尽管上述几个缺点可以通过修改配置文件改变,但我就是不愿意费这个事,就直接使用centos做为服务端。
目录

前言

关于squid的这种应用详细原理这里不做赘述,推荐大家看了一下马哥教育对squid服务的详解,我们这里更多的关注的squid的使用

我们公司内部使用squid做代理服务,在某天突然坏了,我搞了五天,还没有修复,终于在第五天的凌晨才修复,通过这次故障处理,我也成长了很多;从头到尾把这次过程梳理一下。其实这个问题可以更快的解决,我虽然大概知道了问题所在,但是却犹犹豫豫,没有真正仔细的面对自己的判断,没有仔细看日志,白白浪费了好多天的时间。

拓扑描述:

服务端两个网卡,可以上网的网卡IP为192.168.0.100,连接内网的网卡的IP是192.168.10.10

客户端的IP都位于192.168.10.x网段,上网需要通过设置代理的服务端的IP即:192.168.10.10

服务端

服务的搭建相当简单,就简单的几条命令就搞定了。

## Centos7
# 创建一个不能登录的用户erbu,密码设置为123456,让客户端使用
# 有的的时候客户端在使用代理的时候必须指定代理的用户名和密码,但我们又不能给客户端root密码,让用一个普通用户即可。

useradd -s /sbin/nologin erbu && echo 123456 | passwd --stdin erbu

yum -y install squid
vim /etc/squid/squid.conf
	acl lanhome1 src 192.168.0.2/32
	http_access allow lanhmoe1
systemctl restart squid && ss -tnlp | grep 3128 && systemctl enable squid
systemctl stop firewalld && systemctl disable firewalld

建议我们在搭建客户端的时候最好使用自己擅长的操作系统,我比较擅长centos,所以就假设我们的服务端是centos,因为ubuntu不太方便,比如ubuntu默认不允许root直接登录,普通用户默认拥有sudo到root的权限,squid默认的配置文件 废话太多,尽管上述几个缺点可以通过修改配置文件改变,但我就是不愿意费这个事,就直接使用centos做为服务端。

客户端

服务端的搭建比较简单,因为方法比较固定,但是客户端使用方法有很多。

windows

widows只要在设置当中开启代理 ,指定服务端的IP+端口即可,早年间我在某个单位出差的时候发现,他们的很多电脑没有设置网关,却能正常的打开百度搜索,我非常惊讶,后来发现原是设置了代理 。

centos7

在centos7当中,我们用代理往往是为了安装软件,比如yum、pip

-------------------------------------------------------------------------------------
# wget
## 第一种方法,在命令行当中直接指代理的IP和端口,如下所示,经测试成功,值得一提的是我的代理服务端应用是squid,套接字是10.100.0.9+3128,客户端直接指定IP+端口就能使用wget,根本不需要指定用户名和密码,但这种方法只能下载http协议的东西,对https的东西是无法下载的;
wget http://www.baidu.com -e use_proxy=yes -e http_proxy=192.168.10.10:3128

## 想要下载https的东西,得这样,如下所示,将http改成https;
wget --no-check-certificate https://mirrors.aliyun.com/repo/Centos-7.repo -e use_proxy=yes -e https_proxy=192.168.10.10:3128

## 第二种方法,在wget的配置文件里面写,~/.wgetrc,新测有效;
http_proxy = http://192.168.10.10:3128
https_proxy = https://192.168.10.10:3128
ftp_proxy = http://192.168.10.10:3128
use_proxy = on
wait = 15
-------------------------------------------------------------------------------------
# yum
vim /etc/yum.conf
proxy=http://192.168.10.10:3128
proxy_username=erbu
proxy_password=123456
-------------------------------------------------------------------------------------
# pip
cd 
mkdir .pip
vim .pip/pip.conf
	[global]
	index-url=http://mirrors.aliyun.com/pypi/simple/
	[install]
	trusted-host=mirrors.aliyun.com

# 安装测试
pip install t5 --proxy="http://192.168.10.10:3128"
------------------------------------------------------------------------------------

windows可以这样,如下所示:
Squid Proxy第1张

ubuntu18+

----------------------------------------------------------------------------------------------
# 加环境变量,放到.bashrc和/etc/profile是一样的效果
root@client:~# cat .bashrc | tail -4
export http_proxy='http://192.168.10.10:3128'
export https_proxy='http://192.168.10.10:3128'
export ftp_proxy='http://192.168.10.10:3128'
export no_proxy='localhost,127.0.0.1'

# 重读
root@client:~# source .bashrc

## 测试
apt update && apt install apache2 -y
----------------------------------------------------------------------------------------------
# pip 源设置
pip config list
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config list

或者

cd 
mkdir .pip
vim .pip/pip.conf
	[global]
	index-url=http://mirrors.aliyun.com/pypi/simple/
	[install]
	trusted-host=mirrors.aliyun.com

# 安装测试
pip install t5 --proxy="http://192.168.10.10:3128"
--------------------------------------------------------------------------------------------

故障处理

  • 可以通过看查看3128端口的连接判断客户端是否已经连接到服务端
# 查看当前有哪些IP正在连接代理
netstat -n | grep 3128 | awk '{print $5}' | awk -F':' '{print $1}' | sort | uniq
  • 当然细致的错误还得是看日志
## 排错相关,排错主要看这两个日志
ls /var/log/squid/
	access.log  cache.log
  • MISS/503
cat /var/log/squid/access.log
1467339283.619  60229 183.12.65.8 TCP_MISS/503 0 CONNECT [www.google.com.hk:443](http://www.google.com.hk:443/) k19421 DIRECT/2607:f8b0:4007:80b::2003 -
1467339292.627  61011 183.12.65.8 TCP_MISS/503 0 CONNECT [www.google.com.hk:443](http://www.google.com.hk:443/) k19421 DIRECT/2607:f8b0:4007:80b::2003 -
1467339292.627  61014 183.12.65.8 TCP_MISS/503 0 CONNECT [www.google.com.hk:443](http://www.google.com.hk:443/) k19421 DIRECT/2607:f8b0:4007:80b::2003 -

当时出现这个问题,我解决了一个星期,通过对比正常squid服务器的日志发现,日志当中只有IPV6的地址,而正常的服务器解析出来的是IPV4的地址,后来查找了一段时间发现可以通过在配置文件当中的添加:

dns_v4_first on

然后重启squid服务之后,恢复正常。

常用源

ubuntu

Ubuntu 的软件源配置文件是 /etc/apt/sources.list

##################16.04
deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main

deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main

deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe

deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe

##################18.04
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

#################20.04
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#### 16.04
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse

########18.4
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

#####20.04
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

######21.04
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
root@client:~# cat /proc/version
Linux version 5.13.0-19-generic (buildd@lgw01-amd64-013) (gcc (Ubuntu 11.2.0-7ubuntu2) 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.37) #19-Ubuntu SMP Thu Oct 7 21:58:00 UTC 2021
root@client:~# uname -a
Linux client 5.13.0-19-generic #19-Ubuntu SMP Thu Oct 7 21:58:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
root@client:~# cat /etc/apt/sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu impish main restricted
# deb-src http://archive.ubuntu.com/ubuntu impish main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu impish-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu impish-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu impish universe
# deb-src http://archive.ubuntu.com/ubuntu impish universe
deb http://archive.ubuntu.com/ubuntu impish-updates universe
# deb-src http://archive.ubuntu.com/ubuntu impish-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu impish multiverse
# deb-src http://archive.ubuntu.com/ubuntu impish multiverse
deb http://archive.ubuntu.com/ubuntu impish-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu impish-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu impish-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu impish-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu impish partner
# deb-src http://archive.canonical.com/ubuntu impish partner

deb http://archive.ubuntu.com/ubuntu impish-security main restricted
# deb-src http://archive.ubuntu.com/ubuntu impish-security main restricted
deb http://archive.ubuntu.com/ubuntu impish-security universe
# deb-src http://archive.ubuntu.com/ubuntu impish-security universe
deb http://archive.ubuntu.com/ubuntu impish-security multiverse
# deb-src http://archive.ubuntu.com/ubuntu impish-security multiverse

centos

centos官方镜像:https://www.centos.org/centos-linux/ 里面涵盖cnetos7和centos8,注意下载的时候别下载错了,我们通常要下载x86架构的,而不是arm架构的。
阿里云镜像:https://developer.aliyun.com/mirror/
清华源:https://mirrors.tuna.tsinghua.edu.cn
# centos7
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
或
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# epel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
--------------------------------------------------------------------------------
# centos8
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
或
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

免责声明:文章转载自《Squid Proxy》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇模板字符串jprofiler主要功能简介及内存泄漏分析示例下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

RHEL6下squid代理之正向代理 枯木

        Squid cache(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器。Squid有广泛的用途,从作为网页服务器的前置cache服务器缓存相关请求来提高Web服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid主要...

编译安装squid3.1亲测

编译安装Squid2.6 1,设置“文件描述符”,并设置用户同时打开文件数量 # vi /usr/include/bits/typesizes.h # vi /usr/include/linux/posix_types.h 把里边的 #define __FD_SETSIZE 1024 改成 65536 2,设置当前环境 # ulimit -Hs 65536...

使用加密的squid配合stunnel实现HTTP代理

现在大部分人都是用ssh tunnel来搭建socks5代理,其实这种方式效率并不高,ssh tunnel并不是为了做代理而存在的。一个比较好的方法是加密squid配合stunnel实现http代理。下面介绍在Archlinux下配置https squid和windows下配置stunnel的方法。 1.首先是Archlinux下安装squid。注意现在...

Squid 安装

Squid简介   Squid是比较知名的代理软件,它不仅可以跑在linux上还可以跑在windows以及Unix上,它的技术已经非常成熟。目前使用Squid的用户也是十分广泛的。Squid与Linux下其它的代理软件如Apache、Socks、TIS FWTK和delegate相比,下载安装简单,配置简单灵活,支持缓存和多种协议。   Squid之所以用...

WINDOWS下的squid

  今天写这篇教程目的在于分享自己在WINDOWS主机下配置squid的方法。哪些地方写的不完善或是不完整或是需要修改的地方,大家可以提出。我会第一时间纠正。下面看正文部分。先提条件,您预安装配置squid的这台计算机必须是联入网络的,系统版本是windows 2000/xp/2003/server 2003。1)先下载Squid for Windows版...

squid故障汇总

1、COSS will not function without large file support (off_t is 4 bytes long. Please reconsider recompiling squid with --with-large-files Bungled squid_webcache.conf 。。。。。。。 检查是否在编译...