阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Squid代理http和https方式上网的操作记录

418次阅读
没有评论

共计 6663 个字符,预计需要花费 17 分钟才能阅读完成。

背景:
公司 IDC 机房有一台服务器 A,只有内网环境:192.168.1.150
现在需要让这台服务器能对外访问,能正常访问 http 和 https 请求(即 80 端口和 443 端口)

思路:
在 IDC 机房里另找其他两台有公网环境的服务器 B(58.68.250.8/192.168.1.8)和服务器 C(58.68.250.5/192.168.1.5),且这两台服务器和内网环境的服务器 A 能相互 ping 通。
其中:
在服务器 B 上部署 squid 的 http 代理,让服务器 C 通过它的 squid 代理上网,能成功访问 http
在服务器 C 上部署 squid 的 https 代理,让服务器 C 通过它的 squid 代理上网,能成功访问 https   [需要在客户端安装 stunnel]

 

下面开始记录这一需求的操作记录:
—————————————————————————————————————————
一、服务器 B 上的操作记录(http 代理)

1)安装 squid
yum 命令直接在线安装 squid
[root@openstack ~]# yum install -y gcc openssl openssl-devel #依赖软件要先提前安装
[root@openstack ~]# yum install squid

安装完成后,修改 squid.conf 文件中的内容,修改之前可以先备份该文件
[root@openstack ~]# cd /etc/squid/
[root@openstack squid]# cp squid.conf squid.conf_bak
[root@openstack squid]# vim squid.conf
http_access allow all                                                   #修改 deny 为 allow
http_port 192.168.1.8:3128
cache_dir ufs /var/spool/squid 100 16 256                    #打开这个注释,保证 /var/spool/squid 这个缓存目录存在

2)启动 squid,启动前进行测试和初始化
[root@openstack squid]# squid -k parse                    #测试
2016/08/09 13:35:04| Processing Configuration File: /etc/squid/squid.conf (depth 0)
2016/08/09 13:35:04| Processing: acl manager proto cache_object
…………..
…………..
2016/08/09 13:35:04| Processing: refresh_pattern . 0 20% 4320
2016/08/09 13:35:04| Initializing https proxy context

[root@openstack squid]# squid -z                            #初始化
2016/08/09 13:35:12| Creating Swap Directories

[root@openstack squid]# /etc/init.d/squid start
Starting squid: . [OK]

 

如果开启了防火墙 iptables 规则,则还需要在 /etc/sysconfig/iptables 里添加下面一行,即允许 3128 端口访问:
-A INPUT -s 192.168.1.0/24 -p tcp -m state –state NEW -m tcp –dport 3128 -j ACCEPT

然后重启 iptables 服务
[root@openstack squid]# /etc/init.d/iptables restart

—————————————————————————————————————————
二、服务器 C 上的的操作记录(https 代理)

1)安装 squid
yum 命令直接在线安装 squid
[root@openstack ~]# yum install -y gcc openssl openssl-devel #依赖软件要先提前安装
[root@openstack ~]# yum install squid
[root@openstack ~]# cd /etc/squid/
[root@openstack squid]# cp squid.conf squid.conf_bak

2)现在开始生成加密代理证书:
[root@bastion-IDC squid]# pwd
/etc/squid
[root@bastion-IDC squid]# openssl req -new > lidongbest5.csr
Generating a 2048 bit RSA private key
………………………………………………………………..+++
……………………………………………………………………………………………+++
writing new private key to ‘privkey.pem’
Enter PEM pass phrase:                                                                   #输入密码,后面会用到,比如这里输入 123456
Verifying – Enter PEM pass phrase:
—–
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [XX]:cn                                                  #国家
State or Province Name (full name) []:beijing                                       #省份
Locality Name (eg, city) [Default City]:beijing                                      #地区名字
Organization Name (eg, company) [Default Company Ltd]:huanqiu        #公司名
Organizational Unit Name (eg, section) []:Technology                            #部门
Common Name (eg, your name or your server’s hostname) []:huanqiu    #CA 主机名
Email Address []:wangshibo@xqshijie.cn                                              #邮箱

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:123456                                                         #证书请求密钥,CA 读取证书的时候需要输入密码
An optional company name []:huanqiu                                                #- 公司名称,CA 读取证书的时候需要输入名称

[root@bastion-IDC squid]# openssl rsa -in privkey.pem -out lidongbest5.key
Enter pass phrase for privkey.pem:                                                     #输入上面设置的密码 123456
writing RSA key

[root@bastion-IDC squid]# openssl x509 -in lidongbest5.csr -out lidongbest5.crt -req -signkey lidongbest5.key -days 3650
Signature ok
subject=/C=cn/ST=beijing/L=beijing/O=huanqiu/OU=Technology/CN=huanqiu/emailAddress=wangshibo@xqshijie.cn
Getting Private key

 

修改 squid.conf 配置文件
[root@bastion-IDC squid]# vim squid.conf
http_access allow all #deny 修改为 allow
#http_port 3128                                                                    #注释掉
https_port 192.168.1.5:443 cert=/etc/squid/lidongbest5.crt key=/etc/squid/lidongbest5.key            #添加这一行
cache_dir ufs /var/spool/squid 100 16 256                             #打开这个注释,保证 /var/spool/squid 这个缓存目录存在

3)重启 squid 服务
[root@bastion-IDC squid]# squid -k parse
[root@bastion-IDC squid]# squid -z
[root@bastion-IDC squid]# squid reload
[root@bastion-IDC squid]# /etc/init.d/squid restart

 

如果开启了防火墙 iptables 规则,则还需要在 /etc/sysconfig/iptables 里添加下面一行,即允许 443 端口访问:
-A INPUT -s 192.168.1.0/24 -p tcp -m state –state NEW -m tcp –dport 443 -j ACCEPT

然后重启 iptables 服务
[root@bastion-IDC squid]# /etc/init.d/iptables restart

—————————————————————————————————————————
三、服务器 A(即客户端)上的操作记录

1)安装配置 stunnel

关闭客户端的 iptables 防火墙
[root@dev-new-test1 ~]# /etc/init.d/iptables stop

[root@dev-new-test1 ~]# cd /usr/local/src/
[root@dev-new-test1 src]# pwd
/usr/local/src

下载:http://pan.baidu.com/s/1boDDwrP(提取秘钥:pc7p)
[root@dev-new-test1 ~]#yum install -y openssl openssl-devel gcc

[root@dev-new-test1 src]# ls
stunnel-5.35.tar.gz
[root@dev-new-test1 src]# tar -zvxf stunnel-5.35.tar.gz
[root@dev-new-test1 src]# ls
stunnel-5.35 stunnel-5.35.tar.gz
[root@dev-new-test1 src]# cd stunnel-5.35
[root@dev-new-test1 stunnel-5.35]# ./configure
[root@dev-new-test1 stunnel-5.35]# make && make install

安装完成后,配置 stunnel.conf
[root@dev-new-test1 stunnel-5.35]# cd /usr/local/etc/stunnel/
[root@dev-new-test1 stunnel]# ls
stunnel.conf-sample
[root@dev-new-test1 stunnel]# cp stunnel.conf-sample stunnel.conf
[root@dev-new-test1 stunnel]# ls
stunnel.conf stunnel.conf-sample
[root@dev-new-test1 stunnel]# cat stunnel.conf              #把原来内容清空,写入:
client = yes
[https]
accept = 127.0.0.1:8088
connect = 192.168.1.5:443                               #运行本机 stunnel 端口 8088 连接 squid 服务端 192.168.1.5 的 443 端口,然后在 /etc/profile 里配置本机 8088 端口代理(如下)

2)启动 stunnel 服务
[root@dev-new-test1 stunnel]# /usr/local/bin/stunnel /usr/local/etc/stunnel/stunnel.conf
[root@dev-new-test1 stunnel]# ps -ef|grep stunnel
root 20281 1 0 02:23 ? 00:00:00 /usr/local/bin/stunnel /usr/local/etc/stunnel/stunnel.conf
root 20283 13002 0 02:23 pts/0 00:00:00 grep –color stunnel
[root@dev-new-test1 stunnel]# lsof -i:8088
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
stunnel 20281 root 7u IPv4 745475 0t0 TCP localhost:radan-http (LISTEN)

3)配置 /etc/profile 系统环境变量
底部添加下面两行
[root@dev-new-test1 stunnel]# vim /etc/profile
……………
export http_proxy=http://192.168.1.8:3128                          #这个是通过服务端 A 机器的 3128 端口的 squid 上网(http 代理)
export https_proxy=http://127.0.0.1:8088                            #这个是通过服务端 B 机器的 443 端口的 squid 上网(https 代理)

[root@dev-new-test1 stunnel]# source /etc/profile                   #配置生效

4)测试:
[root@dev-new-test1 stunnel]# curl http://www.linuxidc.com                           #访问 80 端口 ok
[root@dev-new-test1 stunnel]# curl https://www.c.com                      #访问 443 端口 ok
[root@dev-new-test1 stunnel]# yum list                                                     #yum 可以正常使用
[root@dev-new-test1 stunnel]# wget http://www.a.com.cn/3442      #wget 正常下载

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140398.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计6663字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7995438
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
星哥带你玩飞牛NAS-1:安装飞牛NAS

星哥带你玩飞牛NAS-1:安装飞牛NAS

星哥带你玩飞牛 NAS-1:安装飞牛 NAS 前言 在家庭和小型工作室场景中,NAS(Network Atta...
Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集 在云原生体系中,Prometheus 已成为最主流的监控与报警...
我用AI做了一个1978年至2019年中国大陆企业注册的查询网站

我用AI做了一个1978年至2019年中国大陆企业注册的查询网站

我用 AI 做了一个 1978 年至 2019 年中国大陆企业注册的查询网站 最近星哥在 GitHub 上偶然...
免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

  免费无广告!这款跨平台 AI RSS 阅读器,拯救你的信息焦虑 在算法推荐主导信息流的时代,我们...
开发者福利:免费 .frii.site 子域名,一分钟申请即用

开发者福利:免费 .frii.site 子域名,一分钟申请即用

  开发者福利:免费 .frii.site 子域名,一分钟申请即用 前言 在学习 Web 开发、部署...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换...
Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集 在云原生体系中,Prometheus 已成为最主流的监控与报警...
如何安装2026年最强个人助理ClawdBot、完整安装教程

如何安装2026年最强个人助理ClawdBot、完整安装教程

如何安装 2026 年最强个人助理 ClawdBot、完整安装教程 一、前言 学不完,根本学不完!近期,一款名...
星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛 NAS-11:咪咕视频订阅部署全攻略 前言 在家庭影音系统里,NAS 不仅是存储中心,更是内容...
你的云服务器到底有多强?宝塔跑分告诉你

你的云服务器到底有多强?宝塔跑分告诉你

你的云服务器到底有多强?宝塔跑分告诉你 为什么要用宝塔跑分? 宝塔跑分其实就是对 CPU、内存、磁盘、IO 做...