使用Thumbor建立CDN圖檔處理伺服器
- 使用Thumbor建立CDN圖檔處理伺服器
- 寫在前面
- 安裝
- linux安裝
- 啟動
- 配置nginx
- 使用
- 進行圖檔裁剪
- 圖檔反轉
- 圖檔壓縮
使用Thumbor建立CDN圖檔處理伺服器
寫在前面
因為雲伺服器的glibc版本普遍較低,最新版的Thumbor安裝後需要更新glibc,而更新glibc時間久,危險性大,可能導緻伺服器直接無法登入,是以參用低版本的Thumbor,目前選用的版本為6.6
安裝
linux安裝
- 先确認python版本,確定python版本為2.7
python --version
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5SN5cDO0MzN2gDZiVWMjVDOyYzX5UDNwITM5EzLcVDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
再確定pip版本
pip --version
如果 pip版本不對或未安裝,那麼使用下述方法進行安裝
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py
正式安裝thumbor
pip install thumbor==6.7.0
啟動
簡單啟動
thumbor
即可啟動,預設端口8888,預設配置在python庫中
正式啟動
生成配置檔案
thumbor-config -> /etc/thumbor.conf
修改配置檔案,避免其他人使用
-
限制來源,隻能過濾指定位址的圖檔
這樣便隻有百度和192.168.0.1位址下的圖檔才能使用ALLOWED_SOURCES = ['baidu.com','192.168.0.1']
-
加密
修改以下配置,即可進行加密
SECURITY_KEY = "123456"
ALLOW_UNSAFE_URL = False
使用時需要使用
thumbor -k 123456 https://baidu.com/test.img
計算圖檔位址
推薦使用方法1,既友善又確定圖檔來源
啟動
thumbor -p 8888 -c '/etc/thumbor.conf'
配置nginx
一般雲伺服器不會大開端口的,而且直接使用端口不安全、不美觀,是以使用nginx進行反向代理,并配置為https
server {
listen 443 ssl;
server_name your server;
charset utf-8; client_max_body_size 1024M;
ssl_certificate /yourpath/fullchain.cer;
ssl_certificate_key /yourpath/domain.key;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
send_timeout 60s;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_redirect off;
set $fixed_destination $http_destination;
if ( $http_destination ~* ^https(.*)$ ){
set $fixed_destination http$1;
}
### Set headers ####
proxy_set_header Host $http_host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Destination $fixed_destination
}
}
使用
進行圖檔裁剪
- 首先需要對圖檔位址進行處理,因為thumbor需要完全的url
window.encodeURIComponent(
"https://github.com/thumbor/thumbor/raw/master/example.jpg"
)
- 在浏覽器控制台輸出上述指令,就能得到編譯後的url
使用Thumbor建立CDN圖檔處理伺服器
https%3A%2F%2Fgithub.com%2Fthumbor%2Fthumbor%2Fraw%2Fmaster%2Fexample.jpg
- 将得到的url與下面的位址進行拼接,即可得到裁剪後的圖檔,裁剪的大小由
w(寬)xh(高)
控制
完整位址:http://localhost:8888/unsafe/wxh/
http://localhost:8888/unsafe/300x200/https%3A%2F%2Fgithub.com%2Fthumbor%2Fthumbor%2Fraw%2Fmaster%2Fexample.jpg
圖檔反轉
将上面的像素大小改為負數,即可進行反轉操作,w為負是左右反轉,h為負是上下反轉,都為負則是上下左右都反轉
http://localhost:8888/unsafe/-300x-200/https%3A%2F%2Fgithub.com%2Fthumbor%2Fthumbor%2Fraw%2Fmaster%2Fexample.jpg
圖檔壓縮
使用filters過濾器進行圖檔壓縮
max_bytes(number-of-bytes)
number-of-bytes
機關是位元組
http://localhost:8888/unsafe/-300x-200/filters:max_bytes(7500)/https%3A%2F%2Fgithub.com%2Fthumbor%2Fthumbor%2Fraw%2Fmaster%2Fexample.jpg