nginx正向代理的配置和使用
此处假设你的服务器上已经安装了
nginx
nginx
本身是不支持https
协议请求转发,为了让nginx
能达到这一效果需要借助第三方模块ngx_http_proxy_connect_module
- 下载新模块
GitHub上下载ngx_http_proxy_connect_module的zip压缩包源码
- 解压新模块源码
将新模块ngx_http_proxy_connect_module
源码压缩包上传到服务器/usr/nginx
目录,并解压并重命名
$ mkdir -p /usr/nginx #创建文件夹
$ cd /usr/nginx #进入
$ rz # 接着执行上传操作 可以使用 'rz' 命令
$ unzip ngx_http_proxy_connect_module-master.zip #执行解压操作
$ mv ngx_http_proxy_connect_module-master ngx_http_proxy_connect_module #重命名
-
由于已经安装了
nginx
,但可能未保留nginx
的源代码,我们可能需要重新下载与已安装nginx
版本相匹配的源代码。确保下载的nginx
版本与ngx_http_proxy_connect_module
模块兼容。
–说明:
这里nginx-1.17.7
版本对应proxy_connect_rewrite_1018.patch
补丁,其他版本相关补丁支持版本,详情见GitHub~ https://github.com/chobits/ngx_http_proxy_connect_module
-
比如我是用的是
nginx-1.17.7
,首先将nginx-1.17.7.tar.gz
上传到/usr/nginx
,接着执行解压操作
$ tar -xzvf nginx-1.17.7.tar.gz
- 添加新模块到nginx
使用root
用户进入nginx
的资源目录/usr/nginx/nginx-1.17.7
,给nginx
添加新模块ngx_http_proxy_connect_module
和并重新编译nginx
$ cd /usr/nginx/nginx-1.17.7
$ patch -p1 < /usr/nginx/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
$ ./configure --add-module=/usr/nginx/ngx_http_proxy_connect_module
$ make && make install
4.修改nginx的配置
修改nginx
的配置添加http
和https
的server,其他配置保持不变~
vi /usr/local/nginx/conf/nginx.conf
这个server主要配置是对DNS
解析和proxy_pass
代理进行:
#正向代理转发http或者https请求
server {
#指定DNS服务器IP地址
resolver 114.114.114.114;
#监听443端口,https默认端口443,我这里使用的http
listen 8002;
#正向代理转发http和https请求
proxy_connect;
proxy_connect_allow 80 443 563;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
location / {
proxy_pass $scheme://$host$request_uri;
# 发送到被代理网站的请求需要添加host头
proxy_set_header Host $http_host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0;
proxy_connect_timeout 30;
}
}
– DNS说明:
(国内外)目前比较主流的DNS:
(国外)谷歌:8.8.8.8
developers.google.com
(国外)OpenDNS:208.67.222.222
signup.opendns.com
(国内)114:114.114.114.114
www.114dns.com
(国内)腾讯:119.29.29.29
www.dnspod.cn
(国内)阿里:223.5.5.5
alidns.com
(国内)百度:180.76.76.76
dudns.baidu.com
- 检查和刷新nginx配置
$ /usr/local/nginx/sbin/nginx -t
$ /usr/local/nginx/sbin/nginx -s reload
- java项目后台调用
-说明:我这里项目服务是运行在内网服务器上,需要访问微信API
接口获取token
,所有咱们就可以在代理服务器的nginx
上配置正向代理(确保代理服务器可以访问外网),然后项目里使用正向代理,就能实现想要的效果
public Map<String,Object> getAccessTokenAndOpenId(String code) throws Exception {
//通过配置文件读取微信API的url和Appid和secret
String url = weChatApiProperties.getUrl().get("access_token") + "?appid="+wxMaProperties.getAppid()+"&secret="+wxMaProperties.getSecret()+"&code="+code+"&grant_type=authorization_code";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
//是否启用正向代理
if (Boolean.parseBoolean(weChatApiProperties.getProxy().get("status"))) {
//设置代理地址和端口,我这里就是:代理服务器地址+上面设置的端口8002
HttpHost proxy = new HttpHost(weChatApiProperties.getProxy().get("url"), Integer.parseInt(weChatApiProperties.getProxy().get("port")));
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
httpGet.setConfig(config);
}
//发起请求
CloseableHttpResponse response = httpClient.execute(httpGet);
//获取响应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 处理响应体数据
String returnJson = EntityUtils.toString(entity);
// 响应返回Map
Map<String, Object> rs = JSON.parseObject(returnJson, Map.class);
if(rs.get("access_token")==null || rs.get("openid")==null){
throw new CustomException("获取AccessToken和OpenId失败");
}
return rs;
}else{
throw new CustomException("获取AccessToken和OpenId失败");
}
}