Squid安装及配置

一、安装Squid

执行命令安装Squid

yum update  # centos
yum install squid # centos

apt-get update # ubuntu and debian 
apt-get install squid # ubuntu and debian

安装完成后编辑squid的配置文件

vim /etc/squid/squid.conf

修改配置文件及添加相关配置(添加的配置可以放到配置文件的最后面,相同的配置项后面会覆盖前面的配置)

修改前要注释掉http_access deny all

dns_nameservers 1.1.1.1
http_port 23333 #端口信息,这个最好修改,避免默认端口被扫
http_access allow all
cache_mem 60 MB

输入squid -h查看squid的帮助文档

./squid -k start 
Usage: squid [-cdhvzCFNRVYX] [-n name] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]
       -a port   Specify HTTP port number (default: 3128).
       -d level  Write debugging to stderr also.
       -f file   Use given config-file instead of
                 /etc/squid/squid.conf
       -h        Print help message.
       -k reconfigure|rotate|shutdown|restart|interrupt|kill|debug|check|parse
                 Parse configuration file, then send signal to 
                 running copy (except -k parse) and exit.
       -n name   Specify service name to use for service operations
                 default is: squid.
       -s | -l facility
                 Enable logging to syslog.
       -u port   Specify ICP port number (default: 3130), disable with 0.
       -v        Print version.
       -z        Create missing swap directories and then exit.
       -C        Do not catch fatal signals.
       -D        OBSOLETE. Scheduled for removal.
       -F        Don't serve any requests until store is rebuilt.
       -N        No daemon mode.
       -R        Do not set REUSEADDR on port.
       -S        Double-check swap during rebuild.
       -X        Force full debugging.
       -Y        Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.

生成Swap

cd /usr/sbin/
./squid -z  # Create missing swap directories and then exit.

重载配置,使配置生效。

# 1
/etc/init.d/squid reload #重载配置
/etc/init.d/squid start #启动
/etc/init.d/squid stop #停止

# 2 如果你找不到squid,那么可以使用
squid -f /etc/squid/squid.conf # 使用配置文件启动
squid -k reconfigure # 重新加载文件,启动的时候配置修改后
squid -k shutdown # 停止
squid -k restart # 重启 
squid -k parse # 解析,配置完成之后,可以通过这个命令查看已经已经配置完成的所有配置项

启动squid,其实代理已经可以用了。

二、测试Squid

Mac和Linux方式,设置临时代理.

export http_proxy=http://xxx.xxx.xxx.xxxx:23333;export https_proxy=http://xxx.xxx.xxx.xxxx:23333;

Windows方式,设置临时代理。

set http_proxy=http://xxx.xxx.xxx.xxxx:23333; set https_proxy=http://xxx.xxx.xxx.xxxx:23333

在服务端监控下日志

tail -f /var/log/squid/access.log

在终端里面执行下面命令,测试访问。

curl -l "https://www.trojansun.com"

可以看到,服务器端的日志已经有这个访问的信息了。

三、配置密码

这里使用最简单的密码验证ncsa_auth

安装生成密码工具

yum install httpd-tools #centos下执行
apt install apache2-utils #ubuntu debian下执行

使用命令生成用户名和密码

htpasswd -h # 查看帮助
htpasswd -c /etc/squid/passwd trojansun # 提示的方式创建
htpasswd -b -c /etc/squid/passwd trojansun trojansun.com # 免提示直接使用命令行中的密码创建
  • -c 是创建一个新的文件
  • /etc/squid/passwd 新文件的路径
  • trojansun 用户名

然后输入两次密码(第一次输入,第二次确认)

然后squid的配置文件(squid.conf)最下方添加如下内容

auth_param basic program /usr/lib64/squid/ncsa_auth /etc/squid/passwd
acl auth_user proxy_auth REQUIRED
http_access allow auth_user

/usr/lib64/squid/ncsa_auth、/etc/squid/passwd替换你本地实际的路径

这里需要注意,如果你想只允许`auth_user有效,那么就要把之前配置的http_access allow all注释掉,否则不使用用户名和密码也可以使用。

四、高匿代理配置

高匿代理的配置,只需要在配置文件的最下方添加如下内容即可

request_header_access Via deny all
request_header_access X-Forwarded-For deny all
request_header_access From deny all

更详细配置可参考:http://wiki.ubuntu.org.cn/Squid

TrojanSun

锦城虽云乐,不如早还家。

暂无评论

发表评论

您的电子邮件地址不会被公开,必填项已用*标注。

相关推荐

NextCloud 升级方法

PHP的环境使用的是oneinstack,当前的NextCloud的版本是19.0.5,按照后台的提示现在最新 ...

Oneinstack安装GMP

在使用NextCloud的时候会提示为了提升性能,需要安装GMP。 我这里的环境是运行在Ubuntu上的one ...

Linux 开启crontab日志

Linux系统下,默认Crontab是不输出日志文件的,虽然在晚上看到了很多查看日志文件的文章,但是我们的问题 ...