vi

Cài Đặt Nginx trên VPS: Hướng Dẫn Đầy Đủ

Nginx là web server mạnh mẽ và phổ biến nhất hiện nay, phù hợp cho cả website nhỏ lẫn ứng dụng doanh nghiệp. Hướng dẫn này giúp bạn cài đặt và cấu hình Nginx trên VPS offshore của AnubizHost từ đầu đến khi hoạt động hoàn chỉnh, bao gồm HTTPS, virtual host và reverse proxy.

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

Cài Đặt Nginx trên Ubuntu/Debian

Sau khi kết nối SSH vào VPS, chạy các lệnh sau để cài đặt Nginx:

apt update && apt upgrade -y
apt install -y nginx
systemctl enable nginx
systemctl start nginx

Kiểm tra trạng thái:

systemctl status nginx

Mở trình duyệt và truy cập IP của VPS - bạn sẽ thấy trang chào mừng mặc định của Nginx. Đây xác nhận Nginx đã hoạt động thành công.

Mở firewall để cho phép HTTP/HTTPS:

ufw allow 'Nginx Full'
ufw enable

Cấu Hình Virtual Host (Server Block)

Virtual host cho phép bạn chạy nhiều website trên cùng một VPS. Tạo file cấu hình cho domain của bạn:

nano /etc/nginx/sites-available/yourdomain.com

Nội dung cơ bản:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com/html;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

Kích hoạt virtual host:

ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

Cấu Hình Nginx Làm Reverse Proxy

Nginx thường được dùng làm reverse proxy cho Node.js, Python Flask, hay backend khác. Cấu hình điển hình:

server {
    listen 80;
    server_name api.yourdomain.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_cache_bypass $http_upgrade;
    }
}

Cấu hình này chuyển toàn bộ traffic từ port 80 đến ứng dụng đang chạy trên port 3000. Reload Nginx sau mỗi thay đổi cấu hình: systemctl reload nginx

Tối Ưu Hiệu Suất Nginx

Điều chỉnh file cấu hình chính tại /etc/nginx/nginx.conf để tối ưu hiệu suất:

worker_processes auto;
worker_connections 1024;

gzip on;
gzip_types text/plain text/css application/json application/javascript;
gzip_min_length 256;

client_max_body_size 50M;
keepalive_timeout 65;

Bật caching cho nội dung tĩnh:

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
}

Sau mỗi thay đổi luôn kiểm tra cú pháp bằng nginx -t trước khi reload.

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

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.

Anubiz Chat AI

Online