在Linux系统中设置代理服务器可以通过多种工具实现,以满足不同的需求。以下是针对不同场景和需求的详细配置步骤
使用 dnsmasq 作为简单的代理服务器
步骤:
-
安装 dnsmasq:
sudo apt update && sudo apt install dnsmasq
-
配置 dnsmasq 作为代理服务器:
- 打开配置文件:
sudo nano /etc/dnsmasq.conf
- 添加以下内容:
interface= port=123 listen=eth # 将代理设置为本地地址 address=127...1 # 代理目标地址和端口(想代理的服务器为 example.com,端口为 80) proxy=127...1:80
- 打开配置文件:
-
启动 dnsmasq 服务并设置开机启动:
sudo systemctl start dnsmasq sudo systemctl enable dnsmasq
-
测试 dnsmasq 代理:
curl 127...1
如果看到目标服务器返回内容,代理配置正确。
使用 Apache 作为反向代理
步骤:
-
安装 Apache:
sudo apt update && sudo apt install apache2
-
启用 Apache 的反向代理模块:
sudo a2enmod proxy sudo a2enmod proxy_http
-
配置 Apache 的反向代理:
- 打开配置文件:
sudo nano /etc/apache2/sites-available/default
- 添加以下内容:
Listen 127...1:80 <VirtualHost 127...1:80> ProxyOn ProxyReverse AllowReverseProxy ProxyPass /http://example.com/ ProxyPassReverse / <Directory /> Allow all granted by deny from all </Directory> </VirtualHost> - 保存并退出。
- 打开配置文件:
-
启动 Apache 服务:
sudo systemctl start apache2 sudo systemctl enable apache2
-
测试 Apache 代理:
curl 127...1
如果看到目标服务器返回内容,代理配置正确。
使用 Squid 作为高性能代理
步骤:
-
安装 Squid:
sudo apt update && sudo apt install squid
-
配置 Squid:
- 打开配置文件:
sudo nano /etc/squid/squid.conf
- 添加以下内容:
# 启用HTTP和HTTPS代理 http_proxy=127...1:80 https_proxy=127...1:443 # 代理目标地址和端口(想代理的服务器为 example.com,端口为 80) proxy=http://example.com:80 # 启用缓存 cache_dir /var/cache/squid
- 打开配置文件:
-
启动 Squid 服务并设置开机启动:
sudo systemctl start squid sudo systemctl enable squid
-
测试 Squid 代理:
curl 127...1
如果看到目标服务器返回内容,代理配置正确。
使用 Traefik 作为现代反向代理
步骤:
-
安装 Traefik:
sudo apt update && sudo apt install traefik
-
配置 Traefik:
- 创建配置文件:
sudo nano /etc/traefik/traefik.yml
- 添加以下内容(根据需求调整后端信息):
accessLog: { filename: /var/log/traefik/access.log } logLevel: error servers: - url: http://localhost:80 entryPoints: - name: web routes: - rule: host: example.com target: http://example.com:80 - url: http://localhost:443 entryPoints: - name: websecure routes: - rule: host: example.com target: http://example.com:443 - 保存并退出。
- 创建配置文件:
-
启动 Traefik 服务:
sudo systemctl start traefik sudo systemctl enable traefik
-
测试 Traefik 代理:
curl -H "Host: example.com" http://127...1
如果看到目标服务器返回内容,代理配置正确。
使用 Nginx 作为高性能反向代理
步骤:
-
安装 Nginx:
sudo apt update && sudo apt install nginx
-
配置 Nginx:
- 打开配置文件:
sudo nano /etc/nginx/sites-available/default
- 添加以下内容:
server { listen 127...1:80; server_name example.com; proxy_pass http://example.com:80; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; location / { proxy_pass http://example.com:80; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } - 保存并退出。
- 打开配置文件:
-
启动 Nginx 服务:
sudo systemctl start nginx sudo systemctl enable nginx
-
测试 Nginx 代理:
curl -H "Host: example.com" http://127...1
如果看到目标服务器返回内容,代理配置正确。
使用 Privoxy 为代理加密
步骤:
-
安装 Privoxy:
sudo apt update && sudo apt install privoxy
-
配置 Privoxy:
- 打开配置文件:
sudo nano /etc/privoxy/privoxy.conf
- 添加以下内容:
# 监听地址和端口 listen-address=127...1:108 # 代理目标地址和端口(想代理的服务器为 example.com,端口为 80) proxy .example.com 127...1:80 # 启用Tor网络代理(可选) use-tor
- 打开配置文件:
-
启动 Privoxy 服务:
sudo systemctl start privoxy sudo systemctl enable privoxy
-
测试 Privoxy 代理:
curl -H "Host: example.com" http://127...1:108
如果看到目标服务器返回内容,代理配置正确。
使用 Netfilter 和 iptables 手动设置
步骤:
-
安装 iptables:
sudo apt update && sudo apt install iptables
-
清除防火墙规则:
sudo iptables -F
-
设置 NAT 和 MASQUERADE:
sudo iptables -t nat -A POSTROUTING -o eth -j MASQUERADE sudo iptables -t nat -A PREROUTING -i eth -j ACCEPT sudo iptables -t nat -A POSTROUTING -j ACCEPT
-
启动 iptables 服务:
sudo systemctl start iptables sudo systemctl enable iptables
-
测试 iptables 代理:
curl 127...1
如果看到代理服务器返回内容,配置正确。
使用 FQDN代理 解决 DNS 问题
**步骤

如果没有特点说明,本站所有内容均由原子加速器官方网站|提供客户端版本、线路管理与节点选择功能,适配Windows、Android、iOS等设备,便于用户进行网络连接优化原创,转载请注明出处!