【nginx正向代理的配置和java项目使用】

nginx正向代理的配置和使用

此处假设你的服务器上已经安装了nginx

nginx本身是不支持https协议请求转发,为了让nginx能达到这一效果需要借助第三方模块ngx_http_proxy_connect_module

  1. 下载新模块
    GitHub上下载ngx_http_proxy_connect_module的zip压缩包源码
    在这里插入图片描述
  2. 解压新模块源码
    将新模块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
  1. 添加新模块到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的配置添加httphttps的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

  1. 检查和刷新nginx配置
$ /usr/local/nginx/sbin/nginx -t
$ /usr/local/nginx/sbin/nginx -s reload
  1. 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失败");
        }

    }
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值