NGINX : configuration
# Folder Structure :
Express সার্ভারের জন্য SSL সহ NGINX সেটআপের প্রাথমিক কোড তৈরি করে ফেললাম (server/app.js)। এখন আমরা পরবর্তী ধাপে যাবো, যেখানে NGINX কনফিগারেশন ফাইল লিখব এবং সেটিকে Express সার্ভারের উপর প্রক্সি হিসেবে ব্যবহার করব।
✅ Create a HTTPS server (SSL) :
// File: server/app.js
"use strict";
console.clear();
const express = require("express");
const fs = require("fs");
const https = require("https");
const path = require("path");
const app = express();
// Middleware
app.use(express.json());
// Basic route
app.get("/", (req, res) => {
res.status(200).send("<h1>Hello from Express + HTTPS + NGINX</h1>");
});
// Error handlers
app.use((req, res, next) => {
res.status(404).json({
status: 404,
message: "Page not found!",
});
});
app.use((err, req, res, next) => {
res.status(500).json({
status: 500,
message: "Internal server error",
error: err,
});
});
// SSL cert/key from local ssl/ directory
const sslOptions = {
key: fs.readFileSync(path.join(__dirname, "..", "ssl", "key.pem")),
cert: fs.readFileSync(path.join(__dirname, "..", "ssl", "cert.pem")),
};
const PORT = 3000;
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`Express server running on https://localhost:${PORT}`);
});
✅ NGINX setup :
🔧 ধাপ ১: Express অ্যাপ্লিকেশন চলছে এমন ধরো https://localhost:3000
তুমি ইতিমধ্যে SSL সহ Express সার্ভার তৈরি করেছো এবং সেটি 3000
পোর্টে চলছে।
🧾 ধাপ ২: NGINX ইনস্টল করো
Windows-এ NGINX ইনস্টল করতে:
-
এই লিংকে যাও:
👉 https://nginx.org/en/download.html -
"Mainline version" থেকে zip file ডাউনলোড করো (Windows version)।
-
ফাইলটি extract করে একটি ফোল্ডারে রাখো (যেমন:
C:\nginx
)
📝 ধাপ ৩: nginx.conf
ফাইল কনফিগার করো
-
তোমার NGINX ফোল্ডারের মধ্যে
conf/nginx.conf
ফাইলটি ওপেন করো। -
নিচের মতো
server
ব্লক যুক্ত করো বা সম্পাদনা করো:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_pass https://localhost:3000;
proxy_ssl_verify off;
proxy_set_header Host $host;
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 $scheme;
}
}
}
🔐
proxy_ssl_verify off;
সেট করা হয়েছে কারণ আমরা self-signed certificate ব্যবহার করছি।
🛠️ পরবর্তীতে real SSL certificate ব্যবহার করলে এটিon
করে দেবে।
▶️ ধাপ ৪: NGINX চালু করো
-
NGINX এর ফোল্ডারে গিয়ে
nginx.exe
রান করাও (double click বা PowerShell দিয়েstart nginx
চালাও)। -
তারপর ব্রাউজারে গিয়ে ওপেন করো:
👉 http://localhost
এখানে তোমার Express অ্যাপের রেসপন্স দেখতে পাবে।
🧪 যাচাই (Testing)
তুমি যদি NGINX ঠিকভাবে চালাও, তাহলে তুমি
http://localhost
দিয়ে request দিলে সেটি NGINX → Express (HTTPS) → response flow-এ যাবে।-
Express অ্যাপের response লগে আসবে।
✅ সংক্ষেপে
ধাপ | কাজ |
---|---|
১ | Express app (HTTPS সহ) রান কর |
২ | NGINX install করো |
৩ | nginx.conf ফাইলে proxy_pass সেট করো |
৪ | NGINX চালু করে http://localhost দিয়ে access করো |
✅ default.conf / nginx.cenf :
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_pass https://localhost:3000;
proxy_ssl_verify off;
proxy_set_header Host $host;
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 $scheme;
}
}
}
✅ how can i understand the nginx is working or not ?
NGINX ঠিকভাবে কাজ করছে কিনা সেটা বোঝার কয়েকটি সহজ উপায় আছে:
✅ 1. ব্রাউজারে গিয়ে চেক করা
👉 Express.js অ্যাপ চলছে https://localhost:3000
এ
👉 কিন্তু তুমি যদি http://localhost
খুলে Express এর পেজ দেখতে পাও, তাহলে বোঝা যাবে NGINX ঠিকভাবে রিকোয়েস্ট forward করছে।
✅ 2. NGINX শুরু হওয়ার সময় কোনো Error দেখায় কিনা
যদি তুমি
nginx.exe
ডাবল ক্লিক করো, সেটা দ্রুত বন্ধ না হয় — তাহলে ধরতে পারো যে সেটা কাজ করছে।-
অথবা Command Prompt থেকে এটা চালাও:
C:\Users\we\Downloads\nginx-1.27.5\nginx.exe
যদি কোনো error না আসে, তাহলে NGINX চলছে।
✅ 3. Task Manager এ দেখে নেওয়া
Ctrl + Shift + Esc চাপো → Task Manager খুলবে
-
“Processes” ট্যাবে গিয়ে খুঁজো
nginx.exe
-
যদি দুটো
nginx.exe
থাকে (একটা master, একটা worker), তাহলে এটা প্রমাণ NGINX চলছে।
✅ 4. nginx -t কমান্ড দিয়ে config চেক করা
Command Prompt খুলে চালাও:
cd C:\Users\we\Downloads\nginx-1.27.5
nginx -t
যদি মেসেজ আসে:
"nginx: configuration file ... is ok"
এবং
"nginx: configuration file ... test is successful"
তাহলে সব ঠিক আছে ✅
✅ 5. 404/500 Error না পাওয়া
তুমি যদি http://localhost
ওপেন করে Express এর expected HTML/Page দেখতে পাও, এবং কোনো 404/502/500 error না আসে, তাহলে ধরেই নিতে পারো NGINX success-fully reverse proxy হিসেবে কাজ করছে।
🧪 চাইলে তুমি Express.js এর রেসপন্সে কিছু লেখা দাও, যেমন "Hello from Express via NGINX!" — সেটা যদি ব্রাউজারে আসে, তাহলে নিশ্চিত ভাবে কাজ করছে।
✅ How many Task are running : (should have one) :
Command Prompt খুলে চালাও:
tasklist /FI "IMAGENAME eq nginx.exe"
✅ Turn of all the Task
Command Prompt খুলে চালাও:
taskkill /F /IM nginx.exe
♻️ ধাপ ২: শুধুমাত্র একটি instance চালু করুন
তারপর nginx.conf
ফাইল ঠিক করে শুধু একটি বার Nginx চালু করুন: