一,在阿里云服务器上安装并配置Squid

安装

sudo apt-get install squid

配置

vi /etc/squid/squid.conf

将文中的http_port改为你需要的端口(默认是3128)

http_port 8899

http_access deny all 改为

http_access allow all

 或者将文中的http_access deny all注释掉

http_port 8899
http_access allow all

写在文末, 之所以要将http_access deny all注释掉是因为squid.conf从上往下读, 符合要求了之后就不再继续往下匹配了

如果需要匿名代理,还需要如下配置:

# 关闭via 
via off
# 设置不修改http_forwarded_for
forwarded_for transparent

创建squid交换目录

cd /usr/sbin/

./squid -

启动squid, 并查看端口状态

./squid

netstat -ntl

 若端口还是3128, 并没有配置成功, 那可以先关闭squid服务并且重新打开就可以了

squid -k shutdown

阿里云Ubuntu16.04通过Squid配置实现代理爬虫_python脚本

若开放的端口还是3128,并没有开放你需要的端口,则需要下方指令来关闭服务

./squid -k shutdown

 然后再次启动squid即可打开端口




二,配置云服务器开放端口 

打开阿里云控制台网络与安全下的安全组

阿里云Ubuntu16.04通过Squid配置实现代理爬虫_python脚本_02


我在squid.conf中配置的是8899端口,所以开放8899端口

                             阿里云Ubuntu16.04通过Squid配置实现代理爬虫_python脚本_03

配置完成之后到自己的机子上终端敲个命令测试一下

curl -x 阿里云公网ip:8899 http://httpbin.org/get

阿里云Ubuntu16.04通过Squid配置实现代理爬虫_云服务_04

或者写个python脚本用代理访问一下http://httpbin.org/get观察返回的外网ip有没有改为代理ip

import requests
import re
proxy = '服务器ip:开放端口'

proxies = {
'http':proxy,
'https':proxy
}

response = requests.get('http://httpbin.org/get',proxies=proxies)
print(response.text)
params = re.compile('"origin": "(.*?)"',re.S)
print(re.findall(params,response.text)[0])


阿里云Ubuntu16.04通过Squid配置实现代理爬虫_python脚本_05