Skip to content

13. Configuring Nginx Server Blocks

Nginx server blocks (similar to Apache’s virtual hosts) allow you to host multiple websites on a single server. Each website can have its own configuration, document root, and domain name.

A server block is defined within the server { } directive in Nginx configuration files. Let’s break down the components of a server block by building one step by step:

The most basic server block is simply an empty block:

server {
# Configuration directives go here
}

Next, we specify which port Nginx should listen on (typically port 80 for HTTP):

server {
listen 80;
# More configuration directives
}

The server_name directive tells Nginx which domain names should be handled by this server block:

server {
listen 80;
server_name example.com www.example.com;
# More configuration directives
}

The root directive defines where the website files are located:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
# More configuration directives
}

The index directive specifies which file should be served when a directory is requested:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
# More configuration directives
}

The location / block with try_files directive is crucial for WordPress to handle pretty permalinks:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# More configuration directives
}

Finally, we add a location block to handle PHP files and pass them to PHP-FPM:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}

13.3. Enhancing Server Blocks with Performance Optimizations

Section titled “13.3. Enhancing Server Blocks with Performance Optimizations”

A basic server block works well, but we can enhance it with performance optimizations like logging, browser caching, and FastCGI tuning.

For better monitoring and troubleshooting, add custom access and error logs for each site:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
# Custom logging configuration
access_log /var/log/nginx/access_example.com.log combined buffer=256k flush=60m;
error_log /var/log/nginx/error_example.com.log;
}

The buffer and flush parameters optimize log writing performance by buffering log entries in memory and writing them to disk periodically.

Browser caching significantly improves website performance by telling browsers to store static assets locally. Create a reusable configuration file for browser caching:

# Create a directory for includes if it doesn't exist
sudo mkdir -p /etc/nginx/includes/
# Create the browser caching configuration file
sudo vim /etc/nginx/includes/browser_caching.conf

Add the following content to the file:

# Cache images and media files for 1 year
location ~* \.(webp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
access_log off;
}
# Cache JavaScript files for 30 days
location ~* \.(js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
access_log off;
}
# Cache CSS files for 30 days
location ~* \.(css)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
access_log off;
}
# Cache web fonts for 30 days
location ~* \.(eot|svg|ttf|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
access_log off;
}

FastCGI is the protocol used to communicate between Nginx and PHP-FPM. Optimizing these settings can improve performance:

# Create the FastCGI optimization configuration file
sudo vim /etc/nginx/includes/fastcgi_optimize.conf

Add the following content to the file:

# Connection timeout settings
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
# Buffer settings for handling responses
fastcgi_buffer_size 512k;
fastcgi_buffers 512 16k;
fastcgi_busy_buffers_size 1m;
fastcgi_temp_file_write_size 4m;
fastcgi_max_temp_file_size 4m;
# Error handling
fastcgi_intercept_errors on;

13.4. Creating a Complete Server Block Configuration

Section titled “13.4. Creating a Complete Server Block Configuration”

Now let’s put everything together to create a complete server block configuration for a WordPress site:

# Create a new server block configuration file
sudo vim /etc/nginx/sites-available/example.com.conf

Add the following complete configuration:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.php;
# WordPress pretty permalinks
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# PHP handling with optimized FastCGI settings
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
include /etc/nginx/includes/fastcgi_optimize.conf;
}
# Include browser caching rules
include /etc/nginx/includes/browser_caching.conf;
# Custom logging
access_log /var/log/nginx/access_example.com.log combined buffer=256k flush=60m;
error_log /var/log/nginx/error_example.com.log;
}

After creating the server block configuration, you need to activate it by creating a symbolic link in the sites-enabled directory:

# Check existing sites
cd /etc/nginx/
ls sites-available/ sites-enabled/
# Create a symbolic link to enable the site
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
# Verify the link was created
ls -l sites-enabled/

13.6. Testing and Applying the Configuration

Section titled “13.6. Testing and Applying the Configuration”

Before reloading Nginx, it’s important to test the configuration to ensure there are no syntax errors:

# Test the Nginx configuration
sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

# Reload Nginx
sudo systemctl reload nginx

Finally, make sure the web root directory exists and has the correct permissions:

# Create the web root directory if it doesn't exist
sudo mkdir -p /var/www/example.com/public_html
# Set the correct ownership (adjust username as needed)
sudo chown -R www-data:www-data /var/www/example.com/public_html
# Set the correct permissions
sudo chmod -R 755 /var/www/example.com/public_html
# Verify the directory
ls -la /var/www/example.com/public_html/

With these steps completed, your Nginx server is now configured to serve your WordPress site. In the next section, we’ll cover how to install WordPress in this prepared environment.