Nginx VPS Config: Reverse Proxy và Load Balancing
Nginx không chỉ là web server - nó là reverse proxy, load balancer, HTTP cache và SSL terminator đẳng cấp enterprise chạy hoàn toàn miễn phí. Trên VPS offshore AnubizHost, Nginx có thể serve hàng chục nghìn concurrent connections với mức RAM rất thấp nhờ kiến trúc event-driven non-blocking. Hướng dẫn này đi sâu vào các cấu hình production thực tế từ upstream configuration đến rate limiting.
Need this done for your project?
We implement, you ship. Async, documented, done in days.
Cấu Hình Upstream và Load Balancing
Nginx có thể phân phối traffic tới nhiều backend servers:
# /etc/nginx/conf.d/upstream.conf
upstream backend_pool {
# Round-robin (default)
server 127.0.0.1:3001 weight=3;
server 127.0.0.1:3002 weight=2;
server 127.0.0.1:3003 weight=1;
# Hoặc least_conn để phân phối đều hơn
least_conn;
keepalive 32;
}
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://backend_pool;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}SSL Termination và HTTP/2
Cấu hình HTTPS với HTTP/2 tối ưu:
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Modern SSL config
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_stapling on;
ssl_stapling_verify on;
# Security headers
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
}
server {
listen 80;
server_name yourdomain.com;
return 301 https://$server_name$request_uri;
}Rate Limiting và DDoS Protection
Giới hạn request rate để chống abuse và DDoS nhỏ:
# Trong http block của nginx.conf
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;Áp dụng trong server block:
location /api/ {
limit_req zone=api burst=20 nodelay;
limit_conn addr 10;
proxy_pass http://backend_pool;
}
location /auth/login {
limit_req zone=login burst=5;
proxy_pass http://backend_pool;
}Block các User-Agents độc hại:
map $http_user_agent $bad_bot {
default 0;
~*(sqlmap|nikto|masscan|zgrab) 1;
}
if ($bad_bot) {
return 403;
}Nginx Proxy Cache
Cấu hình proxy cache để giảm tải backend:
# Trong http block
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;Áp dụng cache trong location block:
location /api/public/ {
proxy_cache my_cache;
proxy_cache_valid 200 5m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating;
proxy_cache_background_update on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://backend_pool;
}Kiểm tra cache status qua response header X-Cache-Status: HIT/MISS. Nginx cache có thể giảm 90% requests xuống backend cho các API endpoints public.
Related Services
Why Anubiz Host
Ready to get started?
Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.