热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

Nginx代理转发阿里云OSS上传的实现代码

这篇文章主要介绍了Nginx代理转发阿里云OSS上传的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

因为小程序上传需要https,服务器https用的是letsencrypt生成的证书,但是阿里云oss没有做https(懒得上传证书),就想着用Nginx代理转发上传请求。

Nginx配置

# HTTPS server
#
 server {
  listen  443 ssl;
  server_name your.domain.name;

  ...

  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $Host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For 
  }

  location /oss {
    proxy_set_header Host drift-book-dev.oss-cn-shenzhen.aliyuncs.com;
    proxy_set_header Connection keep-alive;
    proxy_pass http://***.oss-cn-***.aliyuncs.com/;
    #proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
 }

这里使用子path"/oss"做转发路径。

proxy_pass 指定 你的阿里云域名,记得后面一定要带斜杠"/",不然转发会失败;

nginx配置proxy_pass代理转发

假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。

第一种:

location /proxy/ { 
  proxy_pass http://127.0.0.1/; 
} 

代理到URL:http://127.0.0.1/test.html

第二种(相对于第一种,最后少一个 / )

location /proxy/ { 
  proxy_pass http://127.0.0.1; 
} 

代理到URL:http://127.0.0.1/proxy/test.html

第三种:

location /proxy/ { 
  proxy_pass http://127.0.0.1/aaa/; 
} 

代理到URL:http://127.0.0.1/aaa/test.html

第四种(相对于第三种,最后少一个 / )

location /proxy/ { 
  proxy_pass http://127.0.0.1/aaa; 
} 

代理到URL:http://127.0.0.1/aaatest.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
shurui26jx_882
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有