-
业务场景:多台机器之间互通但是机器分区分为互联网区和外网区(互联网区可以访问公网互联网,外网区与公网互联网隔离)此时想要在外网区对互联网进行访问。
-
由于Nginx默认不支持HTTPS代理,我们需要额外添加模块。使用的模块是ngx_http_proxy_connect_module。使用模块前需请确保模块和Nginx版本匹配。下载链接:github.com/chobits/ngx…
-
一般这时nginx已经安装好了我们只需要将之前的nginx备份下然后在源码包中重新编译并添加该模块即可(注意之后只能make不可install,install会将之前安装的覆盖掉);编译好后复制出来就好
-
详细步骤如下:
1、备份
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/bak/nginx
备份是个好习惯
2、安装模块
2.1、进入源码包
cd /usr/local/nginx-1.24.0/
2.2、导入补丁
patch -p1 </usr/local/nginx-module/ngx_http_proxy_connect_module-0.0.5/patch/proxy_connect_rewrite_102101.patch
/usr/local/nginx-module/ngx_http_proxy_connect_module-0.0.5/patch/proxy_connect_rewrite_102101.patch我的路径位置注意替换
注意:
① 此处的补丁版本与nginx版本有关,
② 这一步不能少,少了会在编译的时候报错
附官网版本选择图
2.3、添加模块
./configure --prefix=/usr/local/nginx --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --add-module=/usr/local/nginx-module/ngx_http_proxy_connect_module-0.0.5
/usr/local/nginx-module/ngx_http_proxy_connect_module-0.0.5/patch/proxy_connect_rewrite_102101.patch我的路径位置注意替换
2.4、编译
make
注意:之前已经安装过的这里不能install
2.5、复制出来
cp objs/nginx /usr/local/nginx/sbin/nginx
2.6、模块验证
/usr/local/nginx/sbin/nginx –V
会列出安装所添加的模块
3、修改配置文件
不放心的同样也可提前备份
server {
#正向代理端口
listen 5432;
server_name localhost;
#指定DNS服务器IP地址并关闭IPV6
resolver 114.114.114.114 valid=60s ipv6=off;
#解析超时时间
resolver_timeout 30s;
#正向代理转发请求
proxy_connect;
#允许的端口
proxy_connect_allow 443 80;
#超时时间
proxy_connect_connect_timeout 20s;
proxy_connect_read_timeout 20s;
proxy_connect_send_timeout 20s;
location / {
# 正向代理配置,根据请求地址自动解析出目标网站地址并进行代理
proxy_pass $scheme://$host$request_uri;
# 发送到被代理网站的请求需要添加Host请求头
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
4、重新加载配置文件
systemctl reload nginx
附脚本
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存退出后执行
加载命令
systemctl daemon-reload
5、验证
代理服务器端代理联网验证curl -I www.baidu.com/ -v -x 127.0.0.1:代理的port
被代理服务器端验证
① 端口是否通畅
telnet 代理机IP:代理的port
② 代理测试
curl -I blog.csdn.net/ -v -x 代理机IP:代理的port
这样比较麻烦每次都要指定IP和端口将其放入环境变量
cp /etc/profile /home/bak
vi /etc/profile
#编辑/etc/profile文件 在最后一行加入
export http_proxy=192.168.0.97:8080
export https_proxy=192.168.0.97:8080
#192.168.0.97:8080 为你的代理服务器ip和端口
#不需要代理的
export no_proxy=localhost,127.0.0.1
使其生效
source /etc/profile
再次验证
curl -I www.baidu.com/
到此就完成了通过Nginx完成了内网服务器通过代理实现了对外网的访问
但是这样就好了吗?
我们通过接口对外网进行调用
发现仍然不通,裂开然后继续查找资料需要在调用的时候指定代理,代码如下
@RequestMapping("02")
public String test2() {
HttpRequest httpRequest = HttpRequest.get("https://www.baidu.com");
httpRequest.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xx.xx.xx.xx", 8080)));
return httpRequest.execute().body();
}
调用验证成功返回
每个接口都要这样改吗?继续查阅资料,可以在引导类中指定代理这样就不需要每个都去指定了
System.setProperty("http.proxyHost", "xx.xx.xx.xx");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "xx.xx.xx.xx");
System.setProperty("https.proxyPort", "8080");
同样也是成功的,到此问题解决!