使用nginx实现正向代理功能 正向代理是一个位于客户端client和服务端(server)之间的服务器客户端需要访问服务端的内容先向代理发送一个请求并指定访问目标(服务端)然后代理端向服务端转交请求并将获得的内容返回给客户端。使用nginx配置正向代理需要用到nginx中的ngx_http_proxy_connect_module模块该模块为第三方模块需要上传模块包通过补丁的形式打入nginx安装包编译后再离线安装nginx。ngx_http_proxy_connect_module下载地址https://github.com/chobits/ngx_http_proxy_connect_modulenginx下载地址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就转发httphttps就转发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] DescriptionThe NGINX HTTP and reverse proxy server Afternetwork.target [Service] Typeforking 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 PrivateTmptrue [Install] WantedBymulti-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_proxyhttp://192.168.1.7:443 export https_proxyhttp://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 chronyd5、windows系统测试ntp服务在windows系统“时间和日期”-“Internet时间”中设置ntp代理服务器的ip并点击“立即更新”。若出现“时间同步出错”的告警需要手动重启Windows Time服务在运行界面搜索services.msc“服务”窗口中找到Windos Time并手动重启。八、后续补充本文前期在测试正向代理时选择将linux系统的http、https均由代理节点的443端口进行转发后期在查询相关资料后发现可以选择将两类流量分开代理如代理节点80端口代理http流量、443端口代理https流量代理节点上nginx添加如下设置vim /etc/nginx/conf/nginx.conf ###编辑nginx配置文件 ####配置文件内设置关于80端口的代理 server { #指定DNS服务器IP地址 resolver 114.114.114.114; #监听80端口http默认端口80 listen 80; #服务器IP或域名 server_name localhost; #正向代理转发http请求 location / { proxy_pass http://$host$request_uri; proxy_set_header HOST $host; proxy_buffers 256 4k; proxy_max_temp_file_size 0k; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_next_upstream error timeout invalid_header http_502; } }测试节点的linux系统上对系统代理的设置修改为vim /etc/profile ###修改代理文件

本月热点