Understanding NGINX Server Blocks
In NGINX, a server block is the configuration section that tells NGINX how to handle a particular domain, subdomain, or website. If Apache uses virtual hosts, NGINX uses server blocks. They are one of the most important parts of NGINX hosting because they decide which domain points to which website folder, which SSL certificate is used, and what rules apply to that site.
A basic server block usually includes a listen line, a server_name line, a root path, an index file list, and one or more location blocks. For example, server_name example.com www.example.com tells NGINX that this block is responsible for those hostnames. The root line tells NGINX where the website files live, such as /var/www/example.com/public.
Step-by-step setup:
-
Create a website folder: sudo mkdir -p /var/www/example.com/public
-
Add a test index file: echo “Website working” | sudo tee /var/www/example.com/public/index.html
-
Create a new NGINX config: sudo nano /etc/nginx/sites-available/example.com
-
Add your server block, save it, then enable it: sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
-
Test NGINX: sudo nginx -t
-
Reload NGINX: sudo systemctl reload nginx
Useful troubleshooting command:
sudo nginx -T
This prints the full loaded configuration, including included files, which is brilliant when you are not sure which server block is active.
A good habit is to use one config file per domain. Avoid putting every website into one large file. Clean server blocks are easier to manage, safer to edit, and better for long-term hosting maintenance.