How Do I Install and Configure Nginx on a DigitalOcean Droplet?
Install Nginx on Ubuntu, create a site config, enable it, and serve your first web page from a DigitalOcean Droplet.
Why Nginx?
Nginx is a fast, lightweight web server used to serve static files, reverse-proxy to Node.js or Python apps, and terminate SSL. It is the default choice for most Droplet deployments because it handles thousands of concurrent connections with minimal memory.
Step 1 — Install Nginx
SSH into your Droplet and run:
sudo apt update
sudo apt install nginx -y
Verify it is running:
sudo systemctl status nginx
Visit your Droplet's IP in a browser — you should see the default Nginx welcome page.
Step 2 — Allow HTTP Through the Firewall
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
Nginx Full opens ports 80 (HTTP) and 443 (HTTPS).
Step 3 — Create a Site Directory
sudo mkdir -p /var/www/mysite
sudo chown -R $USER:$USER /var/www/mysite
echo '<h1>Hello from DigitalOcean</h1>' | tee /var/www/mysite/index.html
Step 4 — Write a Server Block
Create a config file at /etc/nginx/sites-available/mysite:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/mysite;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 5 — Enable the Site
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
nginx -t validates syntax before applying changes — always run it before reloading.
Reverse Proxy for Node.js Apps
If you run an app on port 3000, proxy traffic through Nginx:
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;
}
Useful Commands
sudo systemctl reload nginx— apply config changes without downtimesudo tail -f /var/log/nginx/error.log— watch error logs livesudo nginx -t— test configuration syntax
Once Nginx is serving your site over HTTP, add SSL with Certbot to enable HTTPS.