Nginx is the world's most popular high-performance web server, reverse proxy, and load balancer. Known for its exceptional speed, low memory footprint, and ability to handle thousands of concurrent connections, Nginx is the ideal choice for serving websites and applications on your SakuraHost VPS.
Prerequisites: A SakuraHost VPS running Ubuntu 22.04 or later with sudo access. If you have not completed initial server setup, follow our guide: Getting Started with Your SakuraHost VPS: Initial Server Setup.
1. Installing Nginx
Update your package index and install Nginx:
sudo apt update
sudo apt install nginx -y
Verify the installation and check the version:
nginx -v
sudo systemctl status nginx
Nginx starts automatically after installation. Open your browser and navigate to http://YOUR_SERVER_IP - you should see the default Nginx welcome page.
Allow Nginx Through the Firewall
sudo ufw allow 'Nginx Full'
sudo ufw status
The Nginx Full profile opens both port 80 (HTTP) and port 443 (HTTPS). Use Nginx HTTP or Nginx HTTPS if you only need one protocol.
2. Understanding the Nginx File Structure
Nginx organizes its configuration across several directories. Understanding this structure is essential for effective management:
/etc/nginx/nginx.conf - Main configuration file with global settings
/etc/nginx/sites-available/ - Configuration files for all virtual hosts
/etc/nginx/sites-enabled/ - Symlinks to active virtual host configurations
/etc/nginx/snippets/ - Reusable configuration fragments
/var/log/nginx/ - Access and error log files
/var/www/ - Default web root directory
3. Creating a Server Block (Virtual Host)
Server blocks allow you to host multiple websites on a single server. Each site gets its own configuration file.
Create the web root directory for your domain:
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
Create a sample index page:
nano /var/www/example.com/html/index.html
Add your HTML content or a simple test page to verify the configuration works.
Create the server block configuration:
sudo nano /etc/nginx/sites-available/example.com
Add the following configuration:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm index.php;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
# Logging
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
# Deny access to hidden files
location ~ /. {
deny all;
}
# Cache static assets
location ~* .(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
Enable the server block and test the configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Always test before reloading: Running nginx -t validates your configuration syntax. Never reload Nginx without testing first - a syntax error will take down all hosted sites.
4. Optimizing Nginx Performance
Edit the main configuration file to optimize Nginx for your VPS resources:
sudo nano /etc/nginx/nginx.conf
Key Performance Settings
# Match worker_processes to your CPU cores
worker_processes auto;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
# Enable gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css application/json
application/javascript text/xml application/xml
application/xml+rss text/javascript;
# Buffer settings
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 50M;
large_client_header_buffers 4 8k;
# Timeouts
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
}
5. Setting Up a Reverse Proxy
Nginx excels as a reverse proxy for Node.js, Python, or other application servers. Here is a basic reverse proxy configuration:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://localhost: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_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
This forwards all requests to a local application running on port 3000. For Node.js deployment with PM2, see our guide: How to Deploy a Node.js Application on Your VPS with PM2.
6. Common Nginx Commands
# Start, stop, restart Nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
# Reload configuration without downtime
sudo systemctl reload nginx
# Test configuration syntax
sudo nginx -t
# View real-time access logs
sudo tail -f /var/log/nginx/access.log
Next Steps: Secure your Nginx server with free SSL certificates from Let's Encrypt. Read our guide:
How to Set Up Let's Encrypt SSL on Your VPS (Certbot Guide). For the official documentation, visit
nginx.org/en/docs. Need assistance? Contact support at
billing.sakurahost.co.tz.