ubuntu搭建squid代理服务

素颜马尾好姑娘i 2024-04-17 11:26 113阅读 0赞

一、安装依赖

  1. apt install openssh-server
  2. apt install apache2-utils
  3. apt install squid

二、修改配置

安全起见,设置账号密码

  1. htpasswd -c /etc/squid/passwd 用户名
  2. # 输入两次明文密码,会自动生成passwd文件,共代理服务鉴权验证使用

修改配置文件

  1. cp squid.conf squid.conf.bak
  2. vim /etc/squid/squid.conf

为了方便使用,copy下面squid.conf配置直接使用,也可根据配置修改原配置文件

  1. acl SSL_ports port 443
  2. acl Safe_ports port 80 # http
  3. acl Safe_ports port 21 # ftp
  4. acl Safe_ports port 443 # https
  5. acl Safe_ports port 70 # gopher
  6. acl Safe_ports port 210 # wais
  7. acl Safe_ports port 1025-65535 # unregistered ports
  8. acl Safe_ports port 280 # http-mgmt
  9. acl Safe_ports port 488 # gss-http
  10. acl Safe_ports port 591 # filemaker
  11. acl Safe_ports port 777 # multiling http
  12. acl CONNECT method CONNECT
  13. # 注意此处路径具体为/usr/lib64还是/usr/lib视具体情况而定
  14. # 查找指令为 sudo find / -name basic_ncsa_auth
  15. auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
  16. acl auth_user proxy_auth REQUIRED
  17. http_access allow auth_user
  18. http_access deny !Safe_ports
  19. http_access deny CONNECT !SSL_ports
  20. http_access deny all
  21. http_port 23333
  22. coredump_dir /var/spool/squid
  23. refresh_pattern ^ftp: 1440 20% 10080
  24. refresh_pattern ^gopher: 1440 0% 1440
  25. refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
  26. refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880
  27. refresh_pattern . 0 20% 4320
  28. request_header_access X-Forwarded-For deny all
  29. request_header_access From deny all
  30. request_header_access Via deny all

三、启动代理服务

  1. sudo squid -z # 初始化缓存文件,选用项,有时出现问题很玄学
  2. service squid start
  3. service squid status 查看启动是否成功

四、代理使用

  1. import requests
  2. url = 'https://myip.ipip.net'
  3. resp = requests.get(url=url, proxies={
  4. 'http': 'http://账号:密码@代理IP地址:端口',
  5. 'https': 'https://账号:密码@代理IP地址:端口'})
  6. resp.encoding = 'utf-8'
  7. print(resp.text)

发表评论

表情:
评论列表 (有 0 条评论,113人围观)

还没有评论,来说两句吧...

相关阅读

    相关 squid服务之配置透明代理

    透明代理提供的服务功能与传统代理时一致的,但是其“透明”的实现依赖于默认路由和防火墙的重定向策略,因此更适用于局域网主机服务,而不适合Internet中。 网络环境如下: