正向代理是一个位于客户端(client)和服务端(server)之间的服务器,客户端需要访问服务端的内容,先向代理发送一个请求并指定访问目标(服务端),然后代理端向服务端转交请求并将获得的内容返回给客户端。
使用nginx配置正向代理,需要用到nginx中的ngx_http_proxy_connect_module模块,该模块为第三方模块需要上传模块包,通过补丁的形式打入nginx安装包编译后,再离线安装nginx。
ngx_http_proxy_connect_module下载地址:https://github.com/chobits/ngx_http_proxy_connect_module
nginx下载地址:Index of /download/
实验环境与版本
二者的版本对应关系在下载链接中查看中可查看,本文此处安装nginx1.22.1与proxy_connect_rewrite_102101.patch。实验环境为1台银河麒麟(非桌面版kylin)操作系统用于搭建nginx正向代理;1台没有互联网环境的linux系统与1台没有互联网环境的windwos系统用于代理后的测试。
一、安装包的准备
1、将下载好的nginx-1.21.1.tar.gz和ngx_http_proxy_connect_module-master(先解压)上传到/root里。解压nginx的tar文件。
###因为我的包上传到/root下了,所以在/root路径下执行。
tar -xzvf nginx-1.21.1.tar.gz ###解压包
cd nginx-1.21.1 ###进入nginx路径
patch -p1 < /root/ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_102101.patch ###需要填写patch包所在的路径
二、编译与安装
1、在上一步的nginx-1.21.1路径下,使用configure进行编译
./configure --prefix=/etc/nginx --add-module=/root/ngx_http_proxy_connect_module-master ###prefix参数表示编译后安装包要安装的位置,可以./configure --help查看别的参数。--add-module是补丁要求的整个下载文件
2、通过编译生成makefile并安装nginx。
make && make install
三、配置安装好的nginx
在/etc/nginx/conf/nginx.conf中“http”中添加如下配置:
“$scheme”参数表示原请求是http就转发http,https就转发https。可根据实际应用选择。
“proxy_connect_allow”参数表示收到请求后,使用对应的端口进行代理访问。
此处的代理端口、转发端口等参数可以根据自身的应用特点进行灵活调整。
vim /etc/nginx/conf/nginx.conf
####http模块底部添加,以下server
server {
#指定DNS服务器IP地址
resolver 61.139.2.69;
#监听443端口,https默认端口443
listen 443;
#正向代理转发https请求
proxy_connect;
proxy_connect_allow 443 563;####代理访问端口
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
location / {
# proxy_pass http://$host;
proxy_pass $scheme://$http_host$request_uri;
# proxy_set_header Host $host;
}
}
四、启动nginx
/etc/nginx/sbin/nginx -t ##nginx配置测试
/etc/nginx/sbin/nginx ####启动;此处也可以将手动安装的nginx添加到系统服务使用systemctl启动。
五、添加systemctl系统启动(可跳过)
通过创建并配置系统服务文件nginx.service完成,如果已经启动了nginx,此处建议先stop一下,再添加系统服务。
touch /etc/systemd/system/nginx.service
vim /etc/systemd/system/nginx.service
###填写如下配置
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
ExecStartPre=/etc/nginx/sbin/nginx -t ###此处的路径为当前nginx安装的路径
ExecStart=/etc/nginx/sbin/nginx ###此处的路径为当前nginx安装的路径
ExecReload=/etc/nginx/sbin/nginx -s reload ###此处的路径为当前nginx安装的路径
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
六、测试与验证
本文使用三个测试环境,分别是:1、配置正向代理的银河麒麟(Kylin Linux)服务器2、没有互联网环境的linux系统3、没有互联网环境的windwos系统。
1、本机的测试,在本机linux系统上直接curl测试。
curl https://www.baidu.com/ -v -x 127.0.0.1:443 ###查看返回值
2、windows系统的测试,需要在系统中设置代理访问。
3、linux系统的测试,需要配置linux系统的全局代理设置。
vim /etc/profile
export http_proxy=http://192.168.1.7:443
export https_proxy=http://192.168.1.7:443
####退出保存后执行source
source /etc/profile
测试互联网访问。
七、关于NTP服务的代理测试
本文在此处思考了很久,ntp服务的代理可以换一个思路,在正向代理服务器上直接起chronyd服务,并将其设置为ntp服务器。不使用正向代理功能即可实现时钟同步。
1、代理服务器上配置chronyd时钟同步服务
###正向代理服务器上编辑chronyd服务配置文件
vim /etc/chrony.conf ####此处使用银河麒麟v10sp3系统测试,若使用别的系统需要找到系统中chronyd.conf的位置并编辑
server ntp.ntsc.ac.cn iburst ###根据自身所在环境配置ntp域名
server ntp1.aliyun.com iburst
####在第26行修改
# Allow NTP client access from local network.
allow 192.168.1.0/24 ###作用是允许ip地址范围的客户端与服务器进行时间同步
####在第29行修改
# Serve time even if not synchronized to a time source.
local stratum 10 ####用于指定本地时钟的分层(stratum)级别
2、重启chronyd服务,查看时间同步的upd服务
systemctl restart chronyd
ss -ulwn
#####
-u:表示仅显示UDP协议。ntp是基于UDP。
-l:表示列出监听状态的端口。
-w:表示显示端口的原数据信息。
-n:表示显示端口号,而无需将其转换为服务名称
3、使用lsof命令验证
该端口是否由chrony进行监听
lsof -i :123 ###查看123端口正在被chronyd进程打开
chronyc sources ###查看同步情况
4、linux系统测试ntp服务
在linux测实机上配置chronyd服务,在/etc/chrony.conf中配置ntp服务的地址池,将pool值修改为代理时间服务器的地址并重启服务验证。
#pool pool.ntp.org iburst
pool 192.168.1.7 iburst
###重启服务
systemctl restart chronyd
5、windows系统测试ntp服务
在windows系统“时间和日期”-“Internet时间”中设置ntp(代理)服务器的ip,并点击“立即更新”。若出现“时间同步出错”的告警,需要手动重启Windows Time服务,在运行界面搜索services.msc,“服务”窗口中找到Windos Time并手动重启。