Skip to content
BENIBIX Learn

Nginx Server Block

Infrastructure🌍 Web Serving & NginxServer Configuration

Also known as Server Block, Nginx server Directive

The Nginx settings block for one site: its port, its names and its folder.

Think of it like

It is like one drawer in a big desk. Each drawer holds the papers for one site.

Example

Jack's server has two server blocks. One answers example.com on port 443. The other sends plain HTTP on port 80 over to HTTPS.

Why it matters

Every site on an Nginx server is set up this way. Reading one is the first step in fixing one.

Definition

A server block is how Nginx writes a virtual host. It is a group of settings inside server { } in the config. The listen line says which port it answers on. The server_name line says which site names it is for. Inside, root points at the site's folder, and location blocks set rules for parts of the site.

Example code

nginx
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.html;
}
One server block. It answers example.com on port 80 and serves files from one folder.
Inside nginx.conf, each server block matches one name and points at one folder.

Common mistake

Leaving out server_name, or spelling it wrong. Nginx then sends the request to its default block, and the wrong site shows.

Key takeaways

  1. 1
    One site. Each block is one virtual host.
  2. 2
    listen and server_name. The port and the names it answers.
  3. 3
    root. The folder it serves from.