Web Server — Nginx সম্পূর্ণ গাইড

লেসন ১৭ / ৩৮

⚡ Nginx কেন?

  • Event-driven, async — Apache prefork-এর চেয়ে বহুগুণ fast
  • Static file serving champion
  • Reverse proxy + load balancer built-in
  • Low memory footprint
  • Internet 33%+ Nginx

📦 Install

sudo apt install nginx -y
sudo systemctl enable --now nginx

# Test
curl http://localhost
# → "Welcome to nginx!"

# Files
/etc/nginx/                 # config root
/etc/nginx/sites-available/ # configs available
/etc/nginx/sites-enabled/   # active (symlink)
/var/www/html/              # default web root
/var/log/nginx/             # logs

🌐 Server Block (Virtual Host)

# /etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }

    location ~ /\.(?!well-known) {
        deny all;
    }

    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;
}

# Enable
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t          # test config!
sudo systemctl reload nginx

🔄 Reverse Proxy (Node/Python)

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
    }
}

🗜️ Gzip Compression

# /etc/nginx/nginx.conf (http block)
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

⚡ Rate Limiting

# http block
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;

# location
location /api/ {
    limit_req zone=api burst=20 nodelay;
    proxy_pass http://localhost:3000;
}

# Login protection
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
location /login {
    limit_req zone=login burst=5 nodelay;
}

📜 Cache Static Assets

location ~* \.(jpg|jpeg|png|gif|css|js|woff2)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
    access_log off;
}

📊 Log Analysis

# Top IPs
awk "{print $1}" /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10

# Top URLs
awk "{print $7}" /var/log/nginx/access.log | sort | uniq -c | sort -rn | head

# 5xx errors
grep " 5[0-9][0-9] " /var/log/nginx/access.log | tail -20

# Live error
sudo tail -f /var/log/nginx/error.log

⚙️ Performance Tune

# /etc/nginx/nginx.conf
worker_processes auto;
worker_connections 4096;
keepalive_timeout 65;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_max_body_size 50M;

🎓 মূল কথা ⭐

Nginx = production web server standard। sudo nginx -t always before reload। Server block per site। Reverse proxy দিয়ে Node/Python deploy easy।