0
0
Nginxdevops~5 mins

Server block structure in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to host websites or web apps on a server, you need to tell the server how to handle different website addresses. Server blocks in nginx let you set rules for each website or app separately on the same server.
When you want to host multiple websites on one server with different domain names.
When you need to serve different content based on the website address visitors use.
When you want to set custom settings like SSL or redirects for a specific website.
When you want to separate logs for different websites on the same server.
When you want to run different web apps on different ports or paths but under one server.
Config File - example-site.conf
example-site.conf
server {
    listen 80;
    server_name example.com www.example.com;

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

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

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

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

server { ... } defines a server block for one website.

listen 80; tells nginx to listen on port 80 (standard web traffic).

server_name lists the domain names this block handles.

root sets the folder where website files are stored.

index lists default files to serve when a folder is requested.

location / { ... } defines how to handle requests to the root path.

error_page 404 sets a custom page for not found errors.

access_log and error_log set log file locations for this site.

Commands
Copy the server block config file to nginx's sites-available directory to prepare for enabling it.
Terminal
sudo cp example-site.conf /etc/nginx/sites-available/
Expected OutputExpected
No output (command runs silently)
Create a symbolic link in sites-enabled to activate the server block configuration.
Terminal
sudo ln -s /etc/nginx/sites-available/example-site.conf /etc/nginx/sites-enabled/
Expected OutputExpected
No output (command runs silently)
Test nginx configuration files for syntax errors before reloading to avoid downtime.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new server block configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Check the HTTP response headers from the server to verify the site is responding correctly.
Terminal
curl -I http://example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: each server block tells nginx how to handle requests for one website or domain.

Common Mistakes
Not creating a symbolic link in sites-enabled after adding the config file to sites-available.
nginx only loads configs from sites-enabled, so your server block won't be active.
Always create a symbolic link in sites-enabled using ln -s after placing your config in sites-available.
Forgetting to test nginx configuration with nginx -t before reloading.
Syntax errors can cause nginx to fail to reload, leading to downtime.
Run sudo nginx -t to check for errors before reloading nginx.
Setting the wrong root path or server_name in the server block.
Requests won't find the correct files or won't match the server block, causing errors or default site to show.
Double-check root and server_name values to match your website files and domain.
Summary
Create a server block config file defining how nginx handles a website's requests.
Enable the server block by linking it from sites-available to sites-enabled.
Test the configuration syntax with nginx -t before reloading nginx to apply changes.
Reload nginx to activate the new server block without downtime.
Verify the website responds correctly using curl or a browser.