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

简介:

 

背景:
公司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.baidu.com                           #访问80端口ok
[root@dev-new-test1 stunnel]# curl https://www.xqshijie.com                      #访问443端口ok
[root@dev-new-test1 stunnel]# yum list                                                     #yum可以正常使用
[root@dev-new-test1 stunnel]# wget http://www.autohome.com.cn/3442      #wget正常下载

 

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************


本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/5853199.html,如需转载请自行联系原作者
目录
相关文章
|
26天前
|
缓存 安全 网络协议
一起深入了解http和https的区别
HTTP适合于非敏感信息的传输,而HTTPS则是在要求安全性、隐私保护及信任机制的现代互联网环境中不可或缺的标准配置。随着网络安全意识的提高和技术的发展,越来越多的网站和服务都转向使用HTTPS,力求在提供便捷服务的同时保障用户数据的安全。HTTPS将成为未来的基本选择。
35 0
一起深入了解http和https的区别
|
1月前
|
安全 Linux 网络安全
Linux使用HTTP隧道代理代码示例模版
Linux使用HTTP隧道代理代码示例模版
16 0
|
3天前
|
缓存 安全 网络协议
【面试必备】HTTP和HTTPS是什么?有什么差异?
HTTP(超文本传输协议)和HTTPS(超文本传输安全协议)是用于在互联网上传输数据的协议。它们都是应用层协议,建立在TCP/IP协议栈之上,用于客户端(如浏览器)和服务器之间的通信。
11 2
|
19天前
|
安全 数据安全/隐私保护
深入解析:HTTP和HTTPS的三次握手与四次挥手
在这些握手和挥手过程中,双方交换信息,协商参数,建立或关闭连接,以保证数据的可靠传输。HTTPS在此基础上加入了数字证书验证和加密通信,增加了安全性。这些步骤确保了HTTP和HTTPS协议的通信过程的稳定和安全。
110 0
|
26天前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
18 0
|
28天前
|
JSON JavaScript 网络安全
新款HTTP代理工具Proxyman(界面美观、功能强大)
新款HTTP代理工具Proxyman(界面美观、功能强大)
|
1月前
|
机器学习/深度学习 人工智能 监控
视觉智能平台常见问题之http转https便捷的转换如何解决
视觉智能平台是利用机器学习和图像处理技术,提供图像识别、视频分析等智能视觉服务的平台;本合集针对该平台在使用中遇到的常见问题进行了收集和解答,以帮助开发者和企业用户在整合和部署视觉智能解决方案时,能够更快地定位问题并找到有效的解决策略。
20 0
|
1月前
|
安全 算法 网络协议
一文搞懂HTTP与HTTPS
一文搞懂HTTP与HTTPS
|
1月前
|
存储 数据采集 负载均衡
建立HTTP代理IP池的技术和工具支持
建立HTTP代理IP池的技术和工具支持
42 0
|
1月前
|
存储 缓存 安全
https跳过SSL认证时是不是就是不加密的,相当于http?
https跳过SSL认证时是不是就是不加密的,相当于http?
117 0