Nginx Reverse Proxy & Free SSL with Let's Encrypt
Nginx Reverse Proxy နှင့် Let's Encrypt အခမဲ့ SSL တပ်ဆင်ခြင်း
Configure Nginx as a high-performance reverse proxy for your backend applications, configure virtual hosts, and automate SSL with Certbot.
Port 80/443 သို့ လာသမျှ request များကို မိမိ၏ Node/Python app သို့ လွှဲပြောင်းပေးသည့် Nginx Reverse Proxy ရေးသားပုံနှင့် Certbot ဖြင့် HTTPS ပြုလုပ်နည်း။
1. What is a Reverse Proxy? (Reverse Proxy သဘောတရား)
Forward Proxy နှင့် Reverse Proxy ကွာခြားချက်
ဝဘ်ဆာဗာတွင် Client များသည် Port 3000 သို့မဟုတ် Port 8000 သို့ တိုက်ရိုက် ဝင်ရောက်ခြင်းထက် Standard Port ဖြစ်သော 80 (HTTP) နှင့် 443 (HTTPS) သို့သာ ဝင်ရောက်ကြပါသည်။
Nginx သည် ရှေ့တန်းတွင် အကာအကွယ်အဖြစ် ရပ်တည်ပြီး၊ လာသမျှ traffic များကို စစ်ဆေးကာ နောက်ကွယ်ရှိ Application (ဥပမာ- localhost:3000) သို့ ပြန်လည် လွှဲပြောင်းပေးပါသည်။ ဤစနစ်ကို Reverse Proxy ဟု ခေါ်ဆိုပါသည်။
2. Install & Configure Nginx (Nginx တပ်ဆင်ခြင်းနှင့် Config ရေးသားခြင်း)
Sites-Available နှင့် Sites-Enabled ချိတ်ဆက်ပုံ
Ubuntu တွင် Nginx ၏ virtual host ဖိုင်များကို `/etc/nginx/sites-available/` တွင် ရေးသားပြီး၊ အသက်သွင်းလိုသော ဖိုင်များကို `/etc/nginx/sites-enabled/` သို့ Symbolic Link (symlink) ပြုလုပ်ရပါသည်။
# Create config file for your domain
sudo nano /etc/nginx/sites-available/myapp.com
# Paste configuration:
server {
listen 80;
listen [::]:80;
server_name myapp.com www.myapp.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_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# Enable site and reload Nginx
sudo ln -s /etc/nginx/sites-available/myapp.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Command ရှင်းလင်းချက် (ဖတ်ရှုရန် နှိပ်ပါ)
Nginx သည် myapp.com သို့ လာသမျှ request များကို localhost:3000 ရှိ Node app သို့ WebSockets support ပါဝင်စွာ လွှဲပြောင်းပေးမည် ဖြစ်ပါသည်။
3. Enable Free SSL with Certbot (Let's Encrypt)
HTTPS သော့ခလောက်စိမ်း အခမဲ့ တပ်ဆင်ခြင်းနှင့် Auto-Renewal
Certbot သည် Electronic Frontier Foundation (EFF) မှ ဖန်တီးထားသော အခမဲ့ tool ဖြစ်ပြီး Nginx config ဖိုင်ကို အလိုအလျောက် ပြင်ဆင်ပေးကာ Let's Encrypt မှ SSL Certificate ကို ရယူတပ်ဆင်ပေးပါသည်။
# Install certbot python-nginx plugin
sudo apt install certbot python3-certbot-nginx -y
# Automatically obtain and install SSL certificate
sudo certbot --nginx -d myapp.com -d www.myapp.com
# Test auto-renewal dry run
sudo certbot renew --dry-run
Command ရှင်းလင်းချက် (ဖတ်ရှုရန် နှိပ်ပါ)
Certbot သည် SSL Certificate ရယူပြီး ၃ လတစ်ကြိမ် သက်တမ်းကုန်ဆုံးချိန်တွင် systemd timer ဖြင့် အလိုအလျောက် auto-renew ပြုလုပ်ပေးသွားမည် ဖြစ်ပါသည်။
What is the command to test Nginx configuration syntax before reloading the server?
Nginx server block ရေးပြီးပါက syntax အမှားအယွင်း ရှိမရှိ စစ်ဆေးရန် မည်သည့် command ကို အသုံးပြုရသလဲ?