Sitemap

Step-by-Step NGINX Server Block Setup

1 min readJun 10, 2025

--

Step 1: Install NGINX (if not already installed)

sudo apt update
sudo apt install nginx

Step 2: Create a Directory for Your Site

This will hold your website’s files.

sudo mkdir -p /var/www/example.com/html

Set ownership and permissions:

sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

Step 3: Add a Sample index.html (optional)

nano /var/www/example.com/html/index.html

Step 4: Create the NGINX Server Block File

Create a new config file in /etc/nginx/sites-available/:

sudo nano /etc/nginx/sites-available/example.com

Paste this configuration:

server {
listen 80;
listen [::]:80;

server_name example.com www.example.com;

root /var/www/example.com/html;
index index.html index.htm;

access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;

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


# reverse proxy for backend
location /api/ {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

Step 5: Enable the Server Block

Create a symlink to sites-enabled:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Step 6: Test NGINX Configuration

sudo nginx -t

sudo systemctl reload nginx

Bonus: Secure with SSL (Optional but Recommended)

Install Certbot and get a free SSL certificate:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

--

--

Ritik Chourasiya
Ritik Chourasiya

Written by Ritik Chourasiya

I’m a 22 year old, still undergraduate backend developer based in India, with 2 years of experience in the software development industry.

No responses yet