关于内网使用Nginx正向代理发送极光推送

一 业务场景

微信支付返回交易信息到内网的系统,信息由内网系统处理后访问极光提供的API https://api.jpush.cn/v3/push 由此来完成消息的推送。内网系统不能直接访问外网,准备使用nginx做正向代理访问极光的API。

二 使用极光前的准备工作

1 下载极光的jar包添加到项目中
2 服务器安装nginx
由于管网的文档很详细,以上两步自行解决。

三 配置nginx.conf

1 服务器IP
	假设nginx IP为 33.33.33.33
2 极光IP
	极光域名 api.jpush.cn 的IP是动态变化的,官方建议是开放域名访问,最要不要在hosts里绑定IP。
	官方有几个固定IP 
	113.31.138.48
	113.31.138.47
	183.232.57.12
	但是不保证固定不变,并且如果有变动也只会主动通知极光的VIP。
	所以你可以用固定的IP,也可以ping一下  api.jpush.cn 得到IP。
	我就使用的 113.31.138.48 
	PS:使用极光V3接口(只支持https请求)要开放443端口(),使用V2接口要开放80端口,但是后者已经不再维护了,所以不建议使用。
3 配置hosts
	view /etc/hosts 在hosts文件解析域名 
	113.31.138.48  api.jpush.cn
	保存
4 配置nginx.conf
	nginx.conf一版在默认目录  /etc/niginx/  下
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
   
    server {
        listen       443;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        
        location /v3 {
    	proxy_pass https://api.jpush.cn;
        }
    }
}

其他的没有需求的话就不用管,最基础的在server 里面监听 443端口,
然后配置一个location /v3 来匹配请求
然后将 https://api.jpush.cn 赋给 proxy_pass
重启nginx nginx -s reload
你能ping通极光,基本用这个配置就可以了。

四 极光推送部分代码

1 使用HttpProxy来代理
2 使用构造器 public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf)来实例化client
3 实例化 ClientConfig,来配置要访问的IP

HttpProxy httpProxy = new HttpProxy("33.33.33.33", 443)
ClientConfig clientConfig = ClientConfig.getInstance();
 clientConfig.setPushHostName("http://33.33.33.33");//这个必须设置,如果不设置会出现 400 bad request
JPushClient jPushClient = new JPushClient(masterSecret, appKey, httpProxy, clientConfig);
PushPayload pushPayload = buildPushObject_all_alias_alert(alias, total);
...
配置要推送的消息,看官方文档...
...

结束语

基本上极光推送的坑就在这个设置hostname上了,clientConfig.setPushHostName("http://33.33.33.33")
在网上查了许久,论坛也看了,并没有给出有效的办法,极光官方也只是强调域名的问题,nginx配置没有问题,就是代理不出去,报错400 bad request。 最后试着配置一下config结果就访问成功了,所以使用nginx代理访问一定要配置clientConfig.setPushHostName("服务器IP")! 重要的事情说三遍!!!
关于极光推送,有不对的地方、有什么好的建议或者想讨论的朋友欢迎留言~~~~
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值