ubuntu搭建squid代理服务
一、安装依赖
apt install openssh-server
apt install apache2-utils
apt install squid
二、修改配置
安全起见,设置账号密码
htpasswd -c /etc/squid/passwd 用户名
# 输入两次明文密码,会自动生成passwd文件,共代理服务鉴权验证使用
修改配置文件
cp squid.conf squid.conf.bak
vim /etc/squid/squid.conf
为了方便使用,copy下面squid.conf
配置直接使用,也可根据配置修改原配置文件
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
# 注意此处路径具体为/usr/lib64还是/usr/lib视具体情况而定
# 查找指令为 sudo find / -name basic_ncsa_auth
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
acl auth_user proxy_auth REQUIRED
http_access allow auth_user
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny all
http_port 23333
coredump_dir /var/spool/squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880
refresh_pattern . 0 20% 4320
request_header_access X-Forwarded-For deny all
request_header_access From deny all
request_header_access Via deny all
三、启动代理服务
sudo squid -z # 初始化缓存文件,选用项,有时出现问题很玄学
service squid start
service squid status 查看启动是否成功
四、代理使用
import requests
url = 'https://myip.ipip.net'
resp = requests.get(url=url, proxies={
'http': 'http://账号:密码@代理IP地址:端口',
'https': 'https://账号:密码@代理IP地址:端口'})
resp.encoding = 'utf-8'
print(resp.text)
还没有评论,来说两句吧...