nginx代理实现https访问阿里云oss资源

2022年4月29日

起因:买了阿里云的oss, 5年50块钱40G, 也够我自己用了,但是我自己的网站是https的,要想加载阿里云的oss图片资源也得是https的才行

方案一:阿里提供的https服务, 缺点太贵,

方案二:上传自己的证书, 缺点:太麻烦,我用的是免费的certbot, 每三个月会重新更新一次证书,也就需要重新上传到阿里云

方案三:用自己服务器上的nginx转发阿里云的资源 (推荐)

nginx server 配置:

 1 server_name  static.bbb.ccc;
 2 
 3 set $cors_origin 'https://aaa.bbb.ccc'; #跨域设置 默认只有 aaa.bbb.ccc的域名才可以访问
 4 if ($http_origin ~* "^(.*?).bbb.ccc$") {
 5     set $cors_origin $http_origin;
 6 }
 7 
 8 location /ali/ {
 9  add_header Access-Control-Allow-Origin $cors_origin; #跨域设置 只有 *.bbb.ccc的域名才可以访问
10  add_header Access-Control-Request-Method: 'GET';
11 
12  proxy_pass http://xxxxx.oss-cn-hangzhou-internal.aliyuncs.com/;
13  proxy_set_header Host "xxxxx.oss-cn-hangzhou-internal.aliyuncs.com";
14  proxy_set_header X-real-ip $remote_addr;
15  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
16  proxy_hide_header Content-Disposition; #注意,这一句可以防止浏览器下载文件
17  expires 30d;
18 }

 

posted @ 2022-04-29 17:52  myD  阅读(1752)  评论(0编辑  收藏  举报