插件窝 干货文章 如何在Linux上设置代理服务器

如何在Linux上设置代理服务器

proxy 代理 HTTP bash 164    来源:    2025-04-16

在Linux上设置代理服务器的详细指南

一、代理服务器类型选择

在Linux上设置代理服务器前,首先需要确定您需要的代理类型:

  1. HTTP/HTTPS代理:用于网页浏览
  2. SOCKS代理:支持更多协议类型
  3. 透明代理:无需客户端配置
  4. 反向代理:用于服务器负载均衡

二、常用代理服务器软件

1. Squid (HTTP/HTTPS代理)

安装

# Debian/Ubuntu
sudo apt-get install squid

# RHEL/CentOS
sudo yum install squid

基本配置: 配置文件通常位于 /etc/squid/squid.conf

# 允许本地网络访问
acl localnet src 192.168.1.0/24
http_access allow localnet

# 设置监听端口
http_port 3128

# 禁止所有其他访问
http_access deny all

启动服务

sudo systemctl start squid
sudo systemctl enable squid

2. TinyProxy (轻量级HTTP代理)

安装

# Debian/Ubuntu
sudo apt-get install tinyproxy

# RHEL/CentOS
sudo yum install tinyproxy

配置: 编辑 /etc/tinyproxy/tinyproxy.conf

Port 8888
Allow 127.0.0.1
Allow 192.168.1.0/24

启动服务

sudo systemctl start tinyproxy
sudo systemctl enable tinyproxy

3. Dante (SOCKS代理)

安装

# Debian/Ubuntu
sudo apt-get install dante-server

# RHEL/CentOS
sudo yum install dante

配置: 编辑 /etc/danted.conf

logoutput: /var/log/danted.log
internal: eth0 port = 1080
external: eth0
method: username none
user.privileged: proxy
user.notprivileged: nobody
client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: connect disconnect
}
pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    command: bind connect udpassociate
    log: connect disconnect
}

启动服务

sudo systemctl start danted
sudo systemctl enable danted

三、客户端代理配置

全局环境变量设置

# 临时设置
export http_proxy="http://proxy-server:3128"
export https_proxy="http://proxy-server:3128"
export ftp_proxy="http://proxy-server:3128"
export no_proxy="localhost,127.0.0.1,192.168.1.0/24"

# 永久设置(添加到 ~/.bashrc 或 /etc/environment)
echo 'export http_proxy="http://proxy-server:3128"' >> ~/.bashrc
echo 'export https_proxy="http://proxy-server:3128"' >> ~/.bashrc
source ~/.bashrc

APT包管理器代理设置

# 创建或编辑配置文件
sudo nano /etc/apt/apt.conf.d/proxy.conf

# 添加以下内容
Acquire::http::Proxy "http://proxy-server:3128";
Acquire::https::Proxy "http://proxy-server:3128";

Wget代理设置

# 编辑 ~/.wgetrc
echo 'http_proxy = http://proxy-server:3128' >> ~/.wgetrc
echo 'https_proxy = http://proxy-server:3128' >> ~/.wgetrc

Git代理设置

git config --global http.proxy http://proxy-server:3128
git config --global https.proxy http://proxy-server:3128

四、高级配置

1. 认证配置(以Squid为例)

# 创建密码文件
sudo htpasswd -c /etc/squid/passwords proxyuser

# 修改squid配置
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated

2. 访问控制

# 限制访问时间
acl workhours time MTWHF 09:00-17:00
http_access allow workhours

# 限制目标网站
acl good_sites dstdomain .example.com .example.org
http_access allow good_sites

3. 缓存配置

# 设置缓存目录
cache_dir ufs /var/spool/squid 100 16 256

# 缓存大小限制
maximum_object_size 256 MB
minimum_object_size 0 KB

五、故障排除

  1. 检查服务状态

    sudo systemctl status squid
    sudo journalctl -u squid
    
  2. 检查日志

    tail -f /var/log/squid/access.log
    tail -f /var/log/squid/cache.log
    
  3. 测试连接

    curl -x http://proxy-server:3128 http://example.com
    
  4. 检查端口

    sudo netstat -tulnp | grep 3128
    

六、安全建议

  1. 始终使用防火墙限制访问
  2. 为代理服务器启用认证
  3. 定期更新代理服务器软件
  4. 监控代理服务器日志
  5. 限制可以访问代理的IP范围

通过以上步骤,您可以在Linux系统上成功设置并配置各种类型的代理服务器。根据您的具体需求选择适合的代理软件和配置方式。