当前位置: 代码迷 >> 综合 >> squid:http和https的代理服务器设置指南(MacOS版)
  详细解决方案

squid:http和https的代理服务器设置指南(MacOS版)

热度:14   发布时间:2024-02-19 12:54:31.0

这里写图片描述

之前曾经介绍过在Linux的CentOS 7上安装squid 3.5来实现http和https的步骤,这篇文章验证一下在MacOS上的使用过程。


目录

  • 机器配置
  • 安装squid
  • 确认版本
  • 确认服务状态
  • 确认设定文件目录
  • 修改设定文件
  • 启动squid
  • 确认端口号
  • 常见问题:无法进行域名解析
  • 结果验证
    • http方式
    • https方式
  • 总结


机器配置

liumiaocn:~ liumiao$ uname -a
Darwin liumiaocn 19.6.0 Darwin Kernel Version 19.6.0: Sun Jul  5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64 x86_64
liumiaocn:~ liumiao$

安装squid

安装命令: brew install squid

liumiaocn:~ liumiao$ brew install squid
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/cask and homebrew/services).
==> New Casks
...省略
==> Summary
?  /usr/local/Cellar/squid/4.13: 2,111 files, 10.0MB
liumiaocn:~ liumiao$ 

确认版本

版本确认: brew info squid

liumiaocn:~ liumiao$ brew info squid
squid: stable 4.13 (bottled), HEAD
Advanced proxy caching server for HTTP, HTTPS, FTP, and Gopher
http://www.squid-cache.org/
/usr/local/Cellar/squid/4.13 (2,111 files, 10.0MB) *Poured from bottle on 2020-09-16 at 20:32:20
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/squid.rb
License: GPL-2.0
==> Dependencies
Required: openssl@1.1 ?
==> Options
--HEADInstall HEAD version
==> Caveats
To have launchd start squid now and restart at login:brew services start squid
Or, if you don't want/need a background service you can just run:squid
==> Analytics
install: 786 (30 days), 1,874 (90 days), 7,825 (365 days)
install-on-request: 755 (30 days), 1,819 (90 days), 7,418 (365 days)
build-error: 0 (30 days)
liumiaocn:~ liumiao$ 

确认服务状态

使用如下命令确认服务状态:

执行命令:brew services list |grep squid

liumiaocn:~ liumiao$ brew services list |grep squid
squid stopped      
liumiaocn:~ liumiao$ 

确认设定文件目录

因为使用的是brew进行的安装,所以可以使用如下命令确认实际的安装目录:

执行命令:brew --prefix

liumiaocn:~ liumiao$ brew --prefix
/usr/local
liumiaocn:~ liumiao$ 

修改设定文件

本例中squid的设定文件如下所示:

liumiaocn:~ liumiao$ ls /usr/local/etc/squid*
/usr/local/etc/squid.conf		/usr/local/etc/squid.conf.default	/usr/local/etc/squid.conf.documented
liumiaocn:~ liumiao$ 

缺省安装之后squid.conf和squid.conf.default的内容是一致的

liumiaocn:~ liumiao$ diff $(brew --prefix)/etc/squid.conf.default $(brew --prefix)/etc/squid.conf
liumiaocn:~ liumiao$

粗暴地修改为allow all

liumiaocn:~ liumiao$ vi /usr/local/etc/squid.conf
liumiaocn:~ liumiao$ diff $(brew --prefix)/etc/squid.conf.default $(brew --prefix)/etc/squid.conf
59c59
< http_access deny all
---
> http_access allow all
liumiaocn:~ liumiao$ 

启动squid

执行命令:brew services start squid

liumiaocn:~ liumiao$ brew services start squid
==> Successfully started `squid` (label: homebrew.mxcl.squid)
liumiaocn:~ liumiao$ 

确认结果如下

liumiaocn:~ liumiao$ brew services list |grep squid
squid started liumiao /Users/liumiao/Library/LaunchAgents/homebrew.mxcl.squid.plist
liumiaocn:~ liumiao$ 

确认端口号

缺省方式下,squid使用3128端口,使用lsof确认如下所示:

liumiaocn:~ liumiao$ lsof -i tcp:3128
COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
squid   20960 liumiao   14u  IPv6 0x98c0cebd6a8dbff7      0t0  TCP *:ndl-aas (LISTEN)
liumiaocn:~ liumiao$ 

常见问题:无法进行域名解析

此时发现无法使用域名只能使用ip来访问

原因:dns未配置,在squid的配置文件中添加8.8.8.8和8.8.4.4

详细可参看:https://liumiaocn.blog.csdn.net/article/details/108630561


结果验证

squid的缺省port为3128,在这里没有修改,在客户端使用如下方式即可访问:

http方式

export http_proxy=http://192.168.31.242:3128
curl http://www.baidu.com

liumiaocn:~ liumiao$ export http_proxy=http://192.168.31.242:3128
liumiaocn:~ liumiao$ curl http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
liumiaocn:~ liumiao$ 

https方式

export https_proxy=http://192.168.31.242:3128
curl https://www.baidu.com

liumiaocn:~ liumiao$ export https_proxy=http://192.168.31.242:3128
liumiaocn:~ liumiao$ curl http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
liumiaocn:~ liumiao$

总结

这篇文章介绍了如何在MacOS上安装并设定squid和使用的方法。

  相关解决方案