Skip to main content
To host multiple websites on one Nginx server, create one server block per domain, each with its own server_name and root. On Ubuntu and Debian, save it in /etc/nginx/sites-available/example.com and link it into /etc/nginx/sites-enabled/; on AlmaLinux and Rocky, save it as /etc/nginx/conf.d/example.com.conf. Run sudo nginx -t, then sudo systemctl reload nginx.

Before you start

  • Install Nginx first: install Nginx.
  • Point each domain’s DNS A record at your server: point a domain.
  • Use a user with sudo. If you are root, leave out sudo.
A server block is Nginx’s name for one site’s configuration (Apache calls it a virtual host). Nginx reads the Host header of each request and picks the server block whose server_name matches. All your sites can share one IP address and port 80.

How to add a website with a server block

This example adds example.com. Repeat it for each domain, such as example.net.
1

Create the web root

2

Write the server block

Ubuntu / Debian: /etc/nginx/sites-available/example.com. AlmaLinux / Rocky: /etc/nginx/conf.d/example.com.conf.
3

Enable the site (Ubuntu / Debian only)

On AlmaLinux / Rocky every *.conf file in conf.d is loaded automatically.
4

Test and reload

On AlmaLinux / Rocky with SELinux enforcing, label a new web root with sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?" and sudo restorecon -Rv /var/www/example.com, or Nginx returns 403 Forbidden.

How to check it worked

Ask Nginx for each site from the server itself, using the Host header:
Each command should return that site’s own page. To see every server block Nginx loaded, run:

Which site does Nginx show for an unknown domain?

If no server_name matches (for example, someone opens your bare IP), Nginx uses the default server for that port: the block marked default_server, or the first one it loaded. On Ubuntu and Debian that is the default site with the Nginx welcome page. To stop unknown hostnames from showing one of your real sites, add a catch-all block and remove the default link:
return 444 closes the connection without a reply. Only one block per port may have default_server.

How to disable or remove a site

The file in sites-available stays, so you can enable it again later.

Frequently asked questions

Nginx says “could not build server_names_hash”. How do I fix it?

You have long or many domain names. In /etc/nginx/nginx.conf, inside http { }, set server_names_hash_bucket_size 64; (Ubuntu and Debian ship this line commented out), then run sudo nginx -t.

Every domain shows the same site. Why?

The server_name does not match, so Nginx falls back to the default server. Check spelling, include both example.com and www.example.com, and confirm the file is enabled with sudo nginx -T.

How do I add HTTPS to each site?

Run Certbot once per site or with several -d options: sudo certbot --nginx -d example.com -d www.example.com. See free SSL with Certbot.

Can a server block run PHP or proxy to an app?

Yes. Add a PHP location (LEMP stack) or a proxy_pass (reverse proxy) to the block.

VPS and dedicated server guides

Every server guide in one place.

Install Nginx

Install and start Nginx.

Free SSL with Certbot

HTTPS for each domain.

Nginx reverse proxy

Send a domain to an app.

Point a domain to your server

DNS records for each site.

Checked against the official nginx documentation on server names and Ubuntu Server nginx guide on 4 November 2026; server blocks, the catch-all block and nginx -t tested on Ubuntu 24.04 LTS (Nginx 1.24.0). Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026