当前位置: 首页 > news >正文

【Centos】在CentOS上搭建Squid代理服务器详解

在CentOS上搭建Squid代理服务器详解

一、安装

首先,通过yum包管理工具安装Squid代理服务器和httpd-tools(用于生成密码文件):

yum install squid -y
yum install httpd-tools -y

二、生成密码文件

创建用于存储用户认证信息的目录:

mkdir /etc/squid_user/

使用htpasswd命令生成密码文件,例如,为用户名linjiusheng设置密码:

htpasswd -cd /etc/squid_user/passwords linjiusheng

输入密码时,注意密码不要超过8位。

三、测试密码文件

使用basic_ncsa_auth工具测试密码文件,输入用户名和密码进行验证:

/usr/lib64/squid/basic_ncsa_auth /etc/squid_user/passwords

输入用户名和密码(例如linjiusheng szpython),如果提示OK,说明验证成功。按ctrl + c打断测试。

四、配置Squid

编辑Squid的配置文件:

vim /etc/squid/squid.conf

在文件末尾添加以下内容:

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid_user/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated

五、启动服务

启动Squid服务:

systemctl start squid.service

停止服务:

systemctl stop squid.service

重启服务:

systemctl restart squid.service

六、删除用户

如果需要删除用户,可以编辑密码文件:

vim /etc/squid_user/passwords

七、日志

Squid的日志默认位于/var/log/squid/目录下。

八、测试代码

使用以下Python代码测试配置的Squid代理服务器:

import requests

# 代理服务器的地址和端口
proxy_url = "http://your_proxy_server_ip:3128"

# 代理服务器的用户名和密码
proxy_username = "your_username"
proxy_password = "your_password"

# 目标网站的URL
target_url = "http://www.example.com"

# 构建代理配置
proxy_config = {
    "http": proxy_url,
    "https": proxy_url,
}

# 构建包含身份验证信息的HTTP代理头
proxy_auth = requests.auth.HTTPProxyAuth(proxy_username, proxy_password)

# 发送带有代理配置和身份验证的请求
try:
    response = requests.get(target_url, proxies=proxy_config, auth=proxy_auth)
    if response.status_code == 200:
        print("请求成功!")
        print(response.text)
    else:
        print(f"请求失败,状态码:{response.status_code}")
except requests.exceptions.RequestException as e:
    print(f"发生异常:{e}")

替换your_proxy_server_ipyour_usernameyour_password为你的代理服务器IP、用户名和密码。这个代码段演示了如何使用Python的requests库发送通过Squid代理的HTTP请求。

九、如果通过Squid代理访问提示403 Forbidden

vim /etc/squid/squid.conf
http_access deny all 修改为 http_access allow all

然后重新启动squid


文章转载自:
http://bathymeter.aaladrg.cn
http://advection.aaladrg.cn
http://chivalry.aaladrg.cn
http://autarkist.aaladrg.cn
http://chromophobe.aaladrg.cn
http://bicone.aaladrg.cn
http://backfence.aaladrg.cn
http://algometry.aaladrg.cn
http://anoesis.aaladrg.cn
http://carnauba.aaladrg.cn
http://bronzer.aaladrg.cn
http://bug.aaladrg.cn
http://bodgie.aaladrg.cn
http://blunder.aaladrg.cn
http://amice.aaladrg.cn
http://bear.aaladrg.cn
http://blae.aaladrg.cn
http://biogeocoenosis.aaladrg.cn
http://bingle.aaladrg.cn
http://adenoid.aaladrg.cn
http://ascensive.aaladrg.cn
http://chemotherapy.aaladrg.cn
http://berkeleyism.aaladrg.cn
http://calumniate.aaladrg.cn
http://anesthetize.aaladrg.cn
http://chemosorb.aaladrg.cn
http://carrot.aaladrg.cn
http://biospeleology.aaladrg.cn
http://burrhead.aaladrg.cn
http://boilover.aaladrg.cn
http://www.dtcms.com/a/5174.html

相关文章:

  • 熬夜会秃头——Beta冲刺总结随笔
  • 基于jsp+servlet的图书管理系统
  • mybatis中<association> 和 <collection>
  • 12月7日作业
  • 使用K-means把人群分类
  • 无公网IP环境如何SSH远程连接Deepin操作系统
  • SSL证书代理
  • 一对一聊天
  • 优化记录 -- 记一次搜索引擎(SOLR)优化
  • Redis默认序列化方式乱码原因及解决办法
  • 【C/PTA —— 15.结构体2(课外实践)】
  • 基于spring boot电子商务系统
  • 维基百科文章爬虫和聚类:高级聚类和可视化
  • Query、BasicDBObject、BasicDBList查询mongodb不同使用方式
  • Linux 权限
  • 异想天开 | 如何实现PXE可视化?批量主机PXE如何监控状态?
  • HTML中使用JavaScript实现一个简单的鼠标悬停特效。
  • 【概率统计】如何理解概率密度函数及核密度估计
  • 企业微信HOOK开发接口调用,发送语音消息
  • 《系统架构设计师教程(第2版)》第2章-计算机系统基础知识-04-计算机网络
  • 免费百度SEO优化工具,百度SEO优化排名工具
  • Linux篇:进程间通信
  • 数据结构与程序的关系
  • 1、命名空间、C++的复合类型、缺省参数
  • 第四章 SpringCloud Alibaba 实现Ribbon负载均衡
  • C#基础与进阶扩展合集-进阶篇(持续更新)
  • docker搭建nginx实现负载均衡
  • 学生成绩管理系统(Java)
  • [node] Node.js的Web 模块
  • 浅话人工智能和深度学习