一.业务背景
内网服务器(B,C)通过外网服务器(A)上外网
因此在服务器A上搭建nginx正向代理使服务器B,C可通过服务器A上网
A服务器ip:172.20.210.120
B服务器ip:自写
C服务器ip:自写
二.实现nginx正向代理
1.安装
下载nginx正向代理所需模块
链接
有多种下载方式,这里选择git clone
[root@server01 ~]# yum -y install git
# 下载git
[root@server01 ~]# mkdir git-nginx
[root@server01 ~]# cd git-nginx
[root@server01 ~]# git init
# 创建git仓库
[root@server01 ~]# git clone https://github.com/chobits/ngx_http_proxy_connect_module
# 克隆到本地git仓库
下载nginx的rpm包
安装所需的包:
yum -y install lvm2 zlib-devel pcre-devel
版本为1.9.2
需更改路径为自身存放路径
2.配置
修改nginx配置文件
[root@server01 nginx-1.9.2]# cat /usr/local/nginx/conf/nginx.conf
server {
resolver 114.114.114.114; # DNS服务器ip地址
listen 8081; #监听8081端口
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_timeout 10s;
proxy_connect_read_timeout 10s; #设置从被代理服务器读取应答内容的超时时间为10s
proxy_connect_send_timeout 10s; #
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_set_header Host $http_host;
proxy_pass https://$host$request_uri; #设定代理服务器的协议和地址
proxy_buffers 256 4k; #设置从被代理服务器读取应答内容的缓存区的数目为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;
}
#保存退出
#检查文件是否有错误
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#确认正确,启动nginx服务
[root@localhost ~]# /usr/local/nginx/sbin/nginx
#检查服务是否启动成功
[root@localhost ~]# netstat -lntp
#出现8081端口即为启动成功
#防火墙放通8081端口(也可直接关闭防火墙,不推荐)
[root@localhost ~]# firewall-cmd --permanent --add-port=8081/tcp
success
#重启防火墙
[root@localhost ~]# firewall-cmd --reload
success
三.B,C通过nginx正向代理访问外网
服务器B,C需能跟服务器A通信
服务器B,C配置:
[root@localhost ~]# vim /etc/profile
#最后一行添加
export http_proxy=172.20.210.120:8081
export https_proxy=172.20.210.120:8081
#保存退出
#生效配置
[root@localhost ~]# source /etc/profile