CentOS6掛載阿里雲OSS及Nginx反向代理配置詳解

系統環境

  • CentOS 6.10
  • Nginx 1.10.2
  • 帝國CMS 7.5

1. 下載安裝包

wget http://gosspublic.alicdn.com/ossfs/ossfs_1.80.6_centos6.5_x86_64.rpm

2. 安裝ossfs

ossfs需要依賴fuse 2.8.4以上版本

查看當前系統的fuse版本

yum list fuse*
rpm -qa fuse*

低於要求版本需要先卸載

yum remove fuse fuse-devel
rpm -e fuse-XXX

重新安裝fuse

各個版本安裝包下載地址:https://github.com/libfuse/libfuse/releases
以fuse-2.9.9爲例,下載地址:https://github.com/libfuse/libfuse/releases/download/fuse-2.9.9/fuse-2.9.9.tar.gz

# 安裝依賴
yum install automake  gcc-c++ git libcurl-devel libxml2-devel make openssl-devel

# 安裝 fuse
wget https://github.com/libfuse/libfuse/releases/download/fuse-2.9.9/fuse-2.9.9.tar.gz
tar -zxvf fuse-2.9.9.tar.gz
cd fuse-2.9.9
./configure
make
make install

# 添加環境變量
vi /etc/profile
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

source /etc/profile

# 查看版本
pkg-config --modversion fuse

安裝ossfs

yum localinstall ossfs_1.80.6_centos6.5_x86_64.rpm

3. 配置賬號訪問信息

echo my-bucket-name:my-access-key-id:my-access-key-secret > /etc/passwd-ossfs
chmod 640 /etc/passwd-ossfs;

4. 掛載Bucket

# -o nonempty     表示ECS本地目錄可以爲非空
# -o allow_other    表示允許普通用戶訪問,不加圖片無法正常上傳
ossfs img-sitename /www/www.domain.net/d/file -ourl=http://oss-cn-qingdao-internal.aliyuncs.com -o nonempty -o allow_other 
ossfs sitename-skin /www/www.domain.net/skin -ourl=http://oss-cn-qingdao-internal.aliyuncs.com -o nonempty -o allow_other 

5. 卸載Bucket

fusermount -u /www/www.domain.net/d/file        # 正常卸載
fusermount -zu /www/www.domain.net/skin     # 強制卸載(解決正常卸載報 device busy 錯誤)

詳細參考:阿里雲官方教程:ossfs安裝與配置

6. Nginx重定向

location ^~ /d/file {
   proxy_pass https://sitename.oss-cn-qingdao.aliyuncs.com/;
   add_header Access-Control-Allow-Origin *;
}

location ^~ /skin {
   proxy_pass https://sitename-skin.oss-cn-qingdao.aliyuncs.com/;
   add_header Access-Control-Allow-Origin *;
}

proxy_pass 用法

第一種:

location ^~ /proxy/ {
	proxy_pass http://www.domain.com/;
}

代理到URL:http://www.domain.com/filename.html

第二種(相對於第一種,最後少一個 / )

location ^~ /proxy/ {
	proxy_pass http://www.domain.com;
}

代理到URL:http://www.domain.com/proxy/filename.html

第三種:

location ^~ /proxy/ {
	proxy_pass http://www.domain.com/path/;
}

代理到URL:http://www.domain.com/path/filename.html

第四種(相對於第三種,最後少一個 / )

location ^~ /proxy/ {
	proxy_pass http://www.domain.com/path;
}

代理到URL:http://www.domain.com/pathfilename.html

詳細參考:nginx 之 proxy_pass詳解

eg: 網站代理到OSS對象存儲

location ^~ /d/file/p {
   proxy_pass https://sitename.oss-cn-qingdao.aliyuncs.com/p;
   add_header Access-Control-Allow-Origin *;
}

訪問:http://www.domain.net/d/file/p/2020-03-10/3c750e47d237e069aaa10cdc9ebdba0c.jpg
轉到:https://img-sitename.oss-cn-qingdao.aliyuncs.com/p/2020-03-10/3c750e47d237e069aaa10cdc9ebdba0c.jpg



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章