Nginx කියන්නේ මොකක්ද?
Nginx කියන්නේ මොකක්ද, ඒක වැඩ කරන්නේ කොහොමද?
ඔයා Web Development හෝ Backend Development කරලා තියෙනවා නම් Nginx කියන නම අහලා ඇති.
Nginx (උච්චාරණය කරන්නේ “Engine-X”) කියන්නේ high-performance web server එකක්. ඒ වගේම එය reverse proxy, load balancer, සහ HTTP cache එකක් විදිහටත් භාවිතා කරන්න පුළුවන්.
සරලව කිව්වොත්,
Nginx කියන්නේ Website එකකට හෝ Application එකකට එන Web Traffic එක manage කරලා, ඒ requests නිවැරදි තැනට යවන software එකක්.
උදාහරණයක් විදිහට ඔයා:
https://example.comවෙත යන විට, ඔයාගේ request එක නිවැරදි Website එක හෝ Application එක වෙත යන්න ඕන.
මේ අතරමැදි traffic එක handle කරන්න Nginx භාවිතා කරන්න පුළුවන්.
Nginx එක Receptionist කෙනෙක් වගේ
ලොකු Office Building එකක් ගැන හිතන්න.
Visitor කෙනෙක් ආවම එයා කෙලින්ම ඕනෑම office එකකට යන්නේ නැහැ. මුලින්ම receptionist ළඟට යනවා.
Receptionist බලනවා:
ඔයා කවුද?
ඔයාට යන්න ඕන කොහෙද?
ඔයාගේ request එක handle කරන්න ඕන department එක මොකක්ද?
ඊට පස්සේ ඔයාව නිවැරදි තැනට යවනවා.
Nginx එකත් මේ වගේ.
User
↓
Nginx
↓
Application / Website / ServerUser ගෙන් එන request එක මුලින්ම Nginx වෙත එනවා. ඊට පස්සේ Nginx ඒ request එක handle කරන ආකාරය තීරණය කරනවා.
Nginx එකෙන් කරන්න පුළුවන් දේවල්
Nginx එකේ ප්රධාන භාවිතයන් කිහිපයක් තියෙනවා.
1. Web Server එකක් ලෙස
Nginx එකට Website එකක තියෙන files කෙලින්ම serve කරන්න පුළුවන්.
උදාහරණ:
HTML
CSS
JavaScript
Images
Videos
Fonts
උදාහරණයක්:
User
↓
Nginx
↓
index.htmlUser කෙනෙක් Website එකකට request කළාම, Nginx එකට අවශ්ය files කෙලින්ම ලබා දෙන්න පුළුවන්.
ඒ නිසා Nginx static websites host කිරීම සඳහාත් බහුලව භාවිතා වෙනවා.
2. Reverse Proxy එකක් ලෙස
Nginx භාවිතා කරන ඉතාමත් common ක්රමයක් තමයි Reverse Proxy එකක් ලෙස භාවිතා කිරීම.
උදාහරණයක් විදිහට ඔයාගේ Node.js application එක:
localhost:3000වල run වෙනවා කියලා හිතමු.
User කෙනෙක්ට:
http://example.com:3000වගේ URL එකක් භාවිතා කරන්න අවශ්ය නැහැ.
ඒ වෙනුවට Nginx එක Application එක ඉදිරියෙන් තබන්න පුළුවන්.
User
↓
example.com
↓
Nginx
↓
Node.js Application
localhost:3000User දකින්නේ:
https://example.comවිතරයි.
Nginx request එක ලබාගෙන ඒක Node.js application එකට forward කරනවා.
මේ ක්රියාවලියට කියන්නේ Reverse Proxying කියලා.
3. Load Balancing
ඔයාගේ Website එකට users ලක්ෂ ගණනක් එන්න පටන් ගත්තොත් එක Server එකකට ඒ සියලු requests handle කරන්න අමාරු වෙන්න පුළුවන්.
එතකොට Application එක Servers කිහිපයක run කරන්න පුළුවන්.
┌── Server 1
│
User → Nginx ├── Server 2
│
└── Server 3Nginx එකට incoming requests මේ Servers අතර distribute කරන්න පුළුවන්.
උදාහරණයක්:
Request 1 → Server 1
Request 2 → Server 2
Request 3 → Server 3
Request 4 → Server 1මේකට කියන්නේ Load Balancing කියලා.
එමඟින් එක Server එකකට අධික traffic එකක් පැමිණීම අඩු කරගන්න පුළුවන්.
4. HTTPS සහ SSL/TLS
Nginx එකට HTTPS connections handle කරන්නත් පුළුවන්.
උදාහරණයක්:
https://example.comNginx එකට encrypted HTTPS request එක receive කරලා, backend application එක සමඟ communicate කරන්න පුළුවන්.
මේ ක්රියාවලිය බොහෝවිට SSL/TLS Termination කියලා හඳුන්වනවා.
ඒ නිසා හැම Application Server එකකම HTTPS configuration එක වෙන වෙනම කරනවා වෙනුවට, Nginx එක ඉදිරියෙන් තියලා HTTPS handling එක එතනින් manage කරන්න පුළුවන්.
5. Caching
Nginx එකට frequently requested content cache කරන්නත් පුළුවන්.
හිතන්න එකම image එක 10,000 users ලා request කරනවා කියලා.
Caching නැත්නම්:
Users → Application → Database/StorageApplication එකට එකම content එක නැවත නැවත process කරන්න සිදුවෙන්න පුළුවන්.
Caching භාවිතා කළොත්:
Users → Nginx → Cached ContentNginx එකට cache කරලා තියෙන content එක කෙලින්ම return කරන්න පුළුවන්.
මේකෙන්:
Website performance වැඩි කරගන්න පුළුවන්.
Backend server එකේ workload එක අඩු වෙනවා.
Response time එක අඩු කරගන්නත් පුළුවන්.
Nginx සමඟ Node.js
Nginx සහ Node.js එකට භාවිතා කිරීම ගෙඩක් common.
Node.js application එක:
localhost:3000වල internally run වෙනවා කියලා හිතමු.
Nginx එක public-facing server එක ලෙස වැඩ කරන්න පුළුවන්.
Internet
↓
Nginx :80/:443
↓
Node.js :3000
↓
Databaseමෙතනදී,
Nginx handle කරන්නේ:
Incoming requests
HTTPS
Static files
Reverse proxying
Load balancing
Caching
Node.js handle කරන්නේ:
Application logic
API requests
Authentication
Business logic
Database operations
ඒ නිසා Nginx කියන්නේ Node.js වෙනුවට භාවිතා කරන backend එකක් නෙමෙයි.
ඒක බොහෝවිට Node.js application එකට ඉදිරියෙන් වැඩ කරන layer එකක්.
Node.js විතරක් භාවිතා නොකරන්නේ ඇයි?
මෙතනදී ඔයාට ප්රශ්නයක් එන්න පුළුවන්:
Node.js එකටත් HTTP requests receive කරන්න පුළුවන්. එහෙනම් Nginx ඕන ඇයි?
Development කරන වෙලාවේ Application එක direct run කිරීම සාමාන්ය දෙයක්.
Browser → Node.jsනමුත් production environment එකකදී Nginx එකෙන් තවත් වැදගත් features ගොඩක් ලබාගන්න පුළුවන්.
උදාහරණ:
Reverse proxy
Efficient static file serving
HTTPS handling
Load balancing
Caching
Request management
ඒ නිසා production architecture එකක් මේ වගේ වෙන්න පුළුවන්:
User
↓
Nginx
↓
Backend Application
↓
DatabaseNginx vs Apache
Nginx සහ Apache HTTP Server කියන්නේ දෙකම ජනප්රිය Web Servers.
Feature | Nginx | Apache |
|---|---|---|
Web Server | ✅ | ✅ |
Reverse Proxy | ✅ | ✅ |
Load Balancing | ✅ | ✅ |
HTTPS | ✅ | ✅ |
Static Files | Excellent | Excellent |
Configuration Style | Central configuration | Flexible configuration |
Common Use | Reverse proxy & modern deployments | Traditional web hosting & flexible setups |
දෙකම powerful Web Servers.
ඒ නිසා “Nginx, Apache වලට වඩා හොඳයි” කියලා කියන්න බැහැ. තෝරාගැනීම Application එකේ requirements සහ architecture එක මත depend වෙනවා.
Nginx භාවිතා කරන්නේ කොහෙද?
Nginx විවිධ ආකාරයේ systems වල භාවිතා වෙනවා.
🌐 Website Hosting
Websites සහ static files serve කිරීමට.
🔌 API Servers
API requests backend applications වෙත forward කිරීමට.
☁️ Cloud Deployments
Cloud server එකක run වෙන Application එකකට entry point එකක් ලෙස.
⚙️ Microservices
Requests විවිධ services වෙත route කිරීමට.
උදාහරණයක්:
/api/users → User Service
/api/orders → Order Service
/api/products → Product Service📈 High-Traffic Applications
Traffic එක Servers කිහිපයක් අතර distribute කිරීමට.
Real-World Example එකක්
ඔයා Online Shopping Website එකක් හදනවා කියලා හිතමු.
Architecture එක මේ වගේ වෙන්න පුළුවන්:
Users
↓
Nginx
↙ ↘
Frontend Backend
↓
DatabaseUser කෙනෙක්:
example.com/productsrequest කරනවා.
Nginx එක request එක receive කරනවා.
ඊට පස්සේ ඒක:
/products → Backend APIවෙත යවනවා.
Backend එක Database එකෙන් products ලබාගෙන response එක Nginx හරහා user වෙත යවනවා.
ඒ නිසා Nginx එක Web Application එකේ ඉදිරිපස තිබෙන traffic manager කෙනෙක් වගේ වැඩ කරනවා.
Developers ලාට Nginx අවශ්යද?
හැම Developer කෙනෙක්ටම Nginx භාවිතා කරන්න අවශ්ය නැහැ.
Local development කරන වෙලාවේ Application එක direct run කරන්න පුළුවන්.
Browser → Node.jsනමුත් Application එක production environment එකකට deploy කරනකොට Nginx ඉතා ප්රයෝජනවත් වෙනවා.
විශේෂයෙන්:
Linux Servers
Node.js
PHP
Laravel
React/Next.js deployments
APIs
Docker
Cloud Servers
Microservices
DevOps
වැනි technologies සහ environments සමඟ Nginx බහුලව භාවිතා වෙනවා.
Nginx ගැන මතක තියාගන්න ලේසිම ක්රමය තමයි මේ diagram එක:
INTERNET
↓
NGINX
↙ ↓ ↘
Website API Server 2
↓
DatabaseNginx කියන්නේ User සහ Application එක අතර සිටින traffic manager කෙනෙක් වගේ.
User ගෙන් request එක ලබාගෙන, ඒක serve කරන්න හෝ නිවැරදි backend service එක වෙත යවන්න Nginx එකට පුළුවන්.
Did you find this article helpful?
Related Articles
Azure වලින් මොනවද කරන්න පුළුවන්?
Microsoft Azure කියන්නේ විවිධ විදිහෙ දේවල් ගොඩක් කරන්න පුලුවන් cloud platform එකක්.
මොනවද මේ Linux Shell වර්ග?
Linux වල තියෙන Bash, Zsh, Fish, Dash වගේ Shells අතර වෙනස්කම්
Flutter සහ React Native
Mobile App Development එකට හොද මොකක්ද?
