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)

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值