(1). 场景:
①. 存储是在七牛云,动态存储域名与业务域名不一致
②. 本地的view.html包与OSS的域名不一致
(2). pdf.js默认不支持跨域,官方给出了两个方案:
①. 使用cors。
②. 将业务服务器作为代理
(3). 实现:
①. 最简单的方案后端拿到pdf后,存在本地服务器:
a. 增加后端压力
b. 占用业务服务器存储
②. OSS存储服务器增加跨域的头Access-Control-Allow-Origin,允许业务服务器直接读取,但在ie9上会出现安全提示
③. nginx反向代理:
a. 存储域名为动态的,定义好url:
"/Index/viewer?file=http://hd4.xwg.cc/2017-04-10_1491805971_FlNoJrXvyicG7SRDg4Y6E3tA8-7G.pdf?bucket=qxt-2017"
b. 对应的存储域名由bucket来决定为qxt-2017.cdn.xwg.cc
c. nginx配置
location ~ .*.pdf
{
resolver 100.100.2.138;
proxy_pass http://$arg_bucket.cdn.xwg.cc;
}
location ~ .*.pdf
{
resolver 8.8.8.8;
proxy_pass https://test-core.oss-cn-shanghai.aliyuncs.com;
}
④. 未加上resolver指令,nginx一直报错,而静态的存储域名没事:
Parameter value can contain variables. In this case, if an address is specified as a domain name, the name is searched among the described server groups, and, if not found, is determined using a resolver.