
Reverse Proxy คืออะไร?
Reverse Proxy คือ Server ที่รับ Request จาก Client แล้วส่งต่อไปยัง Backend Server ที่เหมาะสม โดย Client ไม่รู้ว่ากำลังคุยกับ Server ตัวไหน Nginx เป็น Reverse Proxy ที่ได้รับความนิยมมากที่สุดในโลกเพราะเร็ว เบา และตั้งค่าง่าย
ทำไมต้องใช้ Reverse Proxy?
# =============================================
# ประโยชน์ของ Reverse Proxy:
# =============================================
# 1. Load Balancing → กระจาย Traffic ไปหลาย Server
# 2. SSL Termination → จัดการ HTTPS ที่จุดเดียว
# 3. Caching → Cache Static Content เร็วขึ้น
# 4. Security → ซ่อน Backend Server จาก Internet
# 5. Compression → บีบอัด Response ลดขนาด
# 6. Virtual Hosts → หลาย Domain บน Server เดียว
# 7. Rate Limiting → จำกัด Request ป้องกัน DDoS
ติดตั้ง Nginx
# Ubuntu/Debian:
sudo apt update && sudo apt install nginx -y
# CentOS/RHEL:
sudo dnf install nginx -y
# เริ่มต้น:
sudo systemctl enable --now nginx
sudo systemctl status nginx
# ทดสอบ:
curl http://localhost
Basic Reverse Proxy Configuration
# /etc/nginx/sites-available/myapp.conf:
server {
listen 80;
server_name myapp.company.com;
location / {
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# Enable:
sudo ln -s /etc/nginx/sites-available/myapp.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
SSL/HTTPS with Let’s Encrypt
# ติดตั้ง Certbot:
sudo apt install certbot python3-certbot-nginx -y
# สร้าง Certificate:
sudo certbot --nginx -d myapp.company.com
# Auto-Renew:
sudo certbot renew --dry-run
# Nginx SSL Config (Auto-Generated):
server {
listen 443 ssl;
server_name myapp.company.com;
ssl_certificate /etc/letsencrypt/live/myapp.company.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp.company.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name myapp.company.com;
return 301 https://$host$request_uri;
}
Load Balancing
# upstream config:
upstream backend_servers {
least_conn;
server 192.168.1.10:3000 weight=3;
server 192.168.1.11:3000 weight=2;
server 192.168.1.12:3000 backup;
}
server {
listen 80;
server_name myapp.company.com;
location / {
proxy_pass http://backend_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
# Load Balancing Methods:
# round_robin (default) → สลับไปเรื่อยๆ
# least_conn → ส่งไปตัวที่ Connection น้อยสุด
# ip_hash → Client เดิมไป Server เดิมเสมอ
# hash $request_uri → URL เดิมไป Server เดิม
Caching
# เพิ่ม Cache:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://backend_servers;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
proxy_cache my_cache;
proxy_cache_valid 200 30d;
expires 30d;
proxy_pass http://backend_servers;
}
}
Security Headers
# เพิ่ม Security Headers:
server {
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
# Rate Limiting:
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://backend_servers;
}
}
Nginx Commands Cheatsheet
| คำสั่ง | หน้าที่ |
|---|---|
nginx -t |
Test Configuration |
systemctl reload nginx |
Reload Config (No Downtime) |
systemctl restart nginx |
Restart Nginx |
nginx -T |
แสดง Config ทั้งหมด |
tail -f /var/log/nginx/access.log |
ดู Access Log |
tail -f /var/log/nginx/error.log |
ดู Error Log |
สรุป: Nginx Reverse Proxy
Nginx Reverse Proxy เป็นเครื่องมือที่ทรงพลังสำหรับ IT Admin ช่วยจัดการ Traffic, SSL, Load Balancing, Caching และ Security ในจุดเดียว ทุก Production Environment ควรมี Reverse Proxy เป็น Front-End รับ Traffic ก่อนส่งไป Backend Server