13. Configuring Nginx Server Blocks
13.1. Understanding Nginx Server Blocks
Section titled “13.1. Understanding 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.
13.2. Anatomy of a Server Block
Section titled “13.2. Anatomy of a Server Block”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:
13.2.1. Basic Server Block Structure
Section titled “13.2.1. Basic Server Block Structure”The most basic server block is simply an empty block:
server { # Configuration directives go here}13.2.2. Adding a Listening Port
Section titled “13.2.2. Adding a Listening Port”Next, we specify which port Nginx should listen on (typically port 80 for HTTP):
server { listen 80; # More configuration directives}13.2.3. Setting the Server Name
Section titled “13.2.3. Setting the Server Name”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}13.2.4. Specifying the Document Root
Section titled “13.2.4. Specifying the Document Root”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}13.2.5. Setting the Default Index File
Section titled “13.2.5. Setting the Default Index File”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}13.2.6. Configuring URL Processing
Section titled “13.2.6. Configuring URL Processing”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}13.2.7. Processing PHP Files
Section titled “13.2.7. Processing PHP Files”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.
13.3.1. Adding Logging Configuration
Section titled “13.3.1. Adding Logging Configuration”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.
13.3.2. Setting Up Browser Caching
Section titled “13.3.2. Setting Up Browser Caching”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 existsudo mkdir -p /etc/nginx/includes/
# Create the browser caching configuration filesudo vim /etc/nginx/includes/browser_caching.confAdd the following content to the file:
# Cache images and media files for 1 yearlocation ~* \.(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 dayslocation ~* \.(js)$ { expires 30d; add_header Cache-Control "public, no-transform"; access_log off;}
# Cache CSS files for 30 dayslocation ~* \.(css)$ { expires 30d; add_header Cache-Control "public, no-transform"; access_log off;}
# Cache web fonts for 30 dayslocation ~* \.(eot|svg|ttf|woff|woff2)$ { expires 30d; add_header Cache-Control "public, no-transform"; access_log off;}13.3.3. Optimizing FastCGI Settings
Section titled “13.3.3. Optimizing FastCGI Settings”FastCGI is the protocol used to communicate between Nginx and PHP-FPM. Optimizing these settings can improve performance:
# Create the FastCGI optimization configuration filesudo vim /etc/nginx/includes/fastcgi_optimize.confAdd the following content to the file:
# Connection timeout settingsfastcgi_connect_timeout 60;fastcgi_send_timeout 180;fastcgi_read_timeout 180;
# Buffer settings for handling responsesfastcgi_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 handlingfastcgi_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 filesudo vim /etc/nginx/sites-available/example.com.confAdd 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;}13.5. Activating the Server Block
Section titled “13.5. Activating the Server Block”After creating the server block configuration, you need to activate it by creating a symbolic link in the sites-enabled directory:
# Check existing sitescd /etc/nginx/ls sites-available/ sites-enabled/
# Create a symbolic link to enable the sitesudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
# Verify the link was createdls -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 configurationsudo nginx -tIf the test is successful, reload Nginx to apply the changes:
# Reload Nginxsudo systemctl reload nginx13.7. Preparing the Web Root Directory
Section titled “13.7. Preparing the Web Root Directory”Finally, make sure the web root directory exists and has the correct permissions:
# Create the web root directory if it doesn't existsudo 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 permissionssudo chmod -R 755 /var/www/example.com/public_html
# Verify the directoryls -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.