Creating Server Blocks
1. Defining Server Block
| Directive | Description | Example |
|---|---|---|
| server_name | Defines the domain name(s) for the server block | server_name example.com www.example.com; |
| listen | Specifies the port and address to listen on | listen 80; |
| root | Sets the root directory for the server block | root /var/www/html; |
| index | Defines the default file to serve | index index.html index.htm; |
Example: Basic Server Block Configuration
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
2. Configuring Listen Directive
| Directive | Values | Description |
|---|---|---|
| listen | 80 |
Default HTTP port |
| listen | 443 ssl |
HTTPS port with SSL |
| listen | localhost:8080 |
Listen on specific host and port |
| listen | 192.168.1.1:80 |
Listen on specific IP and port |
| listen | unix:/var/run/nginx.sock |
Unix socket for internal communication |
Example: Basic HTTP server block with listen directive
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Example: HTTPS server block with SSL listen directive
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
3. Setting Server Name
| Directive | Values | Description |
|---|---|---|
| server_name | example.com |
Defines the domain name(s) for this server block |
| server_name | www.example.com |
Can include subdomains and wildcards like *.example.com |
| server_name | example.com example.org |
Multiple domains can be specified in one directive |
Example: Basic server_name configuration
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.html;
}
Note: The
server_name directive is required for each server block to match incoming requests.
Warning: If
server_name is not set, Nginx will use the first server block as default, which can lead to unexpected behavior.
4. Using Wildcard Server Names
| Directive | Values | Description |
|---|---|---|
| server_name | *.example.com |
Matches any subdomain of example.com |
| server_name | www.*.example.com |
Matches www followed by any subdomain of example.com |
Example: Wildcard server name configuration
server {
listen 80;
server_name *.example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Note: Wildcard server names can match multiple subdomains but do not support IP address wildcards.
5. Setting Default Server
| Directive | Values | Description |
|---|---|---|
| server_name | localhost |
Defines the server name for this block |
| listen | 80 |
Port to listen on for HTTP requests |
Example: Default Server Configuration
server {
listen 80;
server_name localhost;
root /var/www/default;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Note: The
server_name directive is used to define the server name for this block. When no server_name matches a request, the default server is used.6. Configuring Multiple Server Blocks
Example: Basic Multiple Server Blocks Setup
# /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
# /etc/nginx/sites-available/blog.example.com
server {
listen 80;
server_name blog.example.com;
root /var/www/blog.example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
| Directive | Description | Usage |
|---|---|---|
| server | Defines a server block configuration | Used to define each virtual host |
| listen | Specifies the port and address to listen on | listen 80;, listen [::]:80; |
| server_name | Defines the domain names for this server block | server_name example.com; |
| root | Defines the root directory for requests | root /var/www/example.com; |
Note: Server blocks must be enabled by linking them to
/etc/nginx/sites-enabled/ or including them in the main config.
Workflow: Enabling Multiple Server Blocks
- Create a configuration file for each domain in
/etc/nginx/sites-available/ - Link each file to
/etc/nginx/sites-enabled/or include them in the main config - Test the configuration with
nginx -t - Reload Nginx to apply changes:
systemctl reload nginx
7. Setting Root Directory
| Directive | Values | Description |
|---|---|---|
| root | /var/www/html |
Sets the root directory for requests to the specified path. |
Example: Setting Root Directory
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html index.htm;
}
Note: The
root directive affects how Nginx resolves file paths for requests. It is important to ensure that the directory has appropriate permissions for Nginx to read files.
8. Configuring Index Files
| Directive | Values | Description |
|---|---|---|
| index | index.html index.htm |
Specifies the default files to serve when a directory is requested |
Example: Setting Index Files
server {
listen 80;
server_name example.com;
location / {
index index.html index.htm;
}
}
9. Using Server Name Matching
| Directive | Values | Description |
|---|---|---|
| server_name | example.com |
Defines the domain name(s) that this server block handles. |
| server_name | www.example.com |
Matches the www subdomain for the same domain. |
| server_name | example.com www.example.com |
Matches both the root and www subdomain. |
| server_name | ~^www\.(.*)$ |
Uses regex to match any www subdomain. |
| server_name | _ |
Matches any hostname that does not match others. |
Example: Basic server_name usage
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
}
Example: Regex server_name
server {
listen 80;
server_name ~^www\.(.*)$;
return 301 http://$1$request_uri;
}
Note: Nginx matches server blocks based on the
server_name directive. The first matching block is used for handling requests.
Warning: If no
server_name is specified, the server block will match any request (default server).
10. Setting Server-specific Variables
| Variable | Default Value | Description | Example |
|---|---|---|---|
| server_name | localhost |
Name of the server block | |
| root | /var/www/html |
Root directory for the server | root /var/www/example.com; |
| index | index.html index.htm |
Default files to serve | index index.html index.htm; |
| access_log | /var/log/nginx/access.log |
Path to access log file | access_log /var/log/nginx/example.com.access.log; |
| error_log | /var/log/nginx/error.log |
Path to error log file | error_log /var/log/nginx/example.com.error.log; |
Example: Setting Custom Variables
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
11. Understanding Server Block Selection
| Criteria | Description | Behavior |
|---|---|---|
| server_name | example.com |
If the request's Host header matches exactly, this server block is selected. |
| server_name | www.example.com |
Matches subdomain and domain combination. |
| server_name | .example.com |
Matches any subdomain of example.com (e.g., api.example.com, test.example.com). |
| server_name | example.com and www.example.com |
Multiple names can be specified to match the same server block. |
| server_name | example.com and example.org |
Different domains can be handled by the same server block if configured. |
| server_name | example.com and example.org |
Each domain can be handled by a different server block if needed. |
Example: Matching server_name directive
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.html;
}
Note: If no
server_name matches the request, Nginx uses the first server block defined in the configuration file.
12. Implementing Catch-all Server Block
| Directive | Description | Example |
|---|---|---|
| server_name | Defines the domain name(s) for this server block. Use _ to match any hostname. |
|
Note: Returning
444 closes the connection without sending a response. It's useful for blocking unwanted traffic or acting as a "black hole" for invalid hostnames.