使用Node.js和Express生成缩略图:
const express = require('express');
const sharp = require('sharp');
const app = express();
const port = 3000;
app.get('/thumbnail/:filename', async (req, res) => {
const filename = req.params.filename;
const imagePath = `/path/to/images/${filename}`;
sharp(imagePath)
.resize(100, 100) // 设置缩略图尺寸
.toBuffer()
.then(data => {
res.type('jpeg'); // 根据需要设置正确的MIME类型
res.send(data);
})
.catch(err => {
console.error(err);
res.status(500).send('Error generating thumbnail');
});
});
app.listen(port, () => {
console.log(`Thumbnail server running at http://localhost:${port}`);
});
配置nginx反向代理:
location /thumbnail/ {
proxy_pass http://localhost:3000/; # Node.js应用地址
proxy_set_header Host $host; # 传递主机头信息到后端服务器
proxy_set_header X-Real-IP $remote_addr; # 传递客户端IP地址到后端服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 传递X
}