Nginx Virtual Host Configuration: Ready-to-Use Examples

A virtual host in Nginx is a server block that tells the web server how to handle requests for a particular domain. A single VPS can host any number of sites, each with its own block. Below are three ready-to-use templates and the steps to enable them on Ubuntu and Debian.
Where the configuration lives
/etc/nginx/sites-available/— one file per site;/etc/nginx/sites-enabled/— symlinks to the sites that are switched on;/etc/nginx/nginx.conf— global server settings.
Replace example.com with your own domain in the examples. Its A record must point to your server’s IP address.
Static site
Create the site directory and the configuration file:
sudo mkdir -p /var/www/example.com
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
PHP site
For WordPress and other PHP applications, requests for .php files are passed to PHP-FPM:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
The socket name depends on the PHP version: php8.3-fpm.sock on Ubuntu 24.04, php8.2-fpm.sock on Debian 12. Run ls /run/php/ to see the exact name.
Reverse proxy
This is how you publish applications that listen on a local port: Node.js, Python, Docker containers.
server {
listen 80;
listen [::]:80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
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_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
The last two lines are needed by applications that use WebSocket.
Enable the site
Create the symlink, test the syntax and reload the configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Never skip nginx -t: with a broken config, reload keeps the old settings, but restart takes every site on the server down.
Free SSL certificate
Certbot obtains a Let’s Encrypt certificate and adds the HTTPS block and the HTTP redirect to your configuration:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
The certificate renews automatically. Test the renewal with sudo certbot renew --dry-run.
Redirect from www
To serve the site from a single address, add a separate block:
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
and remove www.example.com from server_name in the main block.
Common problems
- The “Welcome to nginx” page appears. The request hit the default site: check
server_nameand that the symlink exists insites-enabled. - 502 Bad Gateway. The application or PHP-FPM is not running, or the port or socket is wrong.
- 403 Forbidden. The
www-datauser cannot read the site directory, or the index file is missing. - Changes are not applied. Run
sudo nginx -t— the configuration most likely contains an error.
If you would rather not edit files by hand, Nginx Proxy Manager does the same through a web interface.


