nginx加nodejs搭建圖片伺服器
May 17, 2023 am 10:22 AM隨著網路的發展,圖片和影片等媒體資源的使用越來越廣泛。身為網站經營者,如何快速、穩定地提供大量的圖片資源成為了一個必須考慮的問題。在此,我們介紹一種使用 nginx 與 Node.js 建立圖片伺服器的方案,來提供高效率、快速、可靠的圖片服務。
一、方案概述
此方案的主要組成部分如下:
- 使用nginx 來提供靜態檔案服務,如圖片、影片等;
- 使用Node.js 來做圖片的處理和快取;
- 利用Redis 記憶體資料庫來快取圖片。
在此方案中,nginx 來提供靜態檔案服務,而 Node.js 作為處理中心,負責處理圖片的縮放、裁剪、浮水印等操作。同時,利用 Redis 的快取機制,減少 Node.js 頻繁讀取圖片的次數,提高圖片處理速度與回應時間。
二、方案實作
- 安裝nginx
#透過apt-get 安裝nginx:
sudo apt-get update sudo apt-get install nginx
登入後複製
- 安裝Node. js 和npm
透過nvm 安裝Node.js 和npm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash source ~/.bashrc nvm install <node-version>
登入後複製
- 安裝Redis
sudo apt-get update sudo apt-get install redis-server
登入後複製
- 建立Node.js 專案
{ "name": "image-server", "version": "1.0.0", "description": "An image server based on Node.js", "main": "app.js", "dependencies": { "express": "^4.17.1", "sharp": "^0.28.3", "redis": "^3.0.2" } }
登入後複製
- 編寫Node.js 程式碼
const express = require('express'); const sharp = require('sharp'); const redis = require('redis'); const app = express(); const port = process.env.PORT || 3000; // Connect to Redis const redisClient = redis.createClient(); // Handle image requests app.get('/:path', async (req, res) => { const { path } = req.params; const { w, h, q } = req.query; // Check if the image exists in Redis cache redisClient.get(path, async (err, cachedImage) => { if (cachedImage) { // Serve the cached image res.header('Content-Type', 'image/jpeg'); res.send(cachedImage); } else { // Read the original image const image = sharp(`images/${path}`); // Apply image transforms if (w || h) image.resize(Number(w), Number(h)); if (q) image.jpeg({ quality: Number(q) }); // Convert the image to Buffer const buffer = await image.toBuffer(); // Cache the image in Redis redisClient.set(path, buffer); // Serve the transformed image res.header('Content-Type', 'image/jpeg'); res.send(buffer); } }); }); // Start the server app.listen(port, () => { console.log(`Server is listening on port ${port}`); });
登入後複製
- 設定nginx
http { ... # Set proxy cache path proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; proxy_cache_key "$scheme$request_method$host$request_uri"; ... server { listen 80; server_name example.com; location /images/ { # Enable proxy cache proxy_cache my_cache; proxy_cache_valid 60m; proxy_cache_lock on; # Proxy requests to Node.js app 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 $proxy_add_x_forwarded_for; # Enable caching of proxied responses proxy_cache_revalidate on; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } }
登入後複製
- 減少了圖片伺服器的負載,提高了網站的穩定性和可靠性。
- 使用 nginx 代理快取和 Redis 快取技術,減少了圖片處理和傳輸時間,提高了圖片服務的回應速度。
- 使用 Node.js 應用程式作為圖片服務的處理中心,方便進行圖片的處理與管理。
- 透過配置 nginx 反向代理和 Redis 緩存,避免了快取雪崩、快取穿透等問題,保證了圖片服務的品質和可用性。
以上是nginx加nodejs搭建圖片伺服器的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript中的高階功能是什麼?如何使用它們來編寫更簡潔和可重複使用的代碼?
