How To Serve Static Files From Subdirectories Using Nginx?

Published September 16, 2024

Problem: Serving Static Files from Subdirectories in Nginx

Nginx is a web server that can be tricky to set up for serving static files from subdirectories. This task needs specific settings to allow proper file access and delivery. These settings may not be clear to people who are new to Nginx configuration.

Proposed Nginx Configuration Solution

Initial Configuration Attempt

The initial Nginx configuration attempt serves static HTML files from specific subdirectories. Here's a breakdown of the main parts:

  • Server block setup with listen directive and server names: The configuration starts with a server block that listens on port 8080 and responds to requests for www.mysite.com and mysite.com.

  • Location block for /public/doc/ directory: A location block handles requests for the /public/doc/ directory. It uses the alias directive to map these requests to the directory on the server (/home/www-data/mysite/public/doc/).

  • Error logging and custom 404 page setup: The configuration includes error logging to a file and sets up a custom 404 error page.

Here's the initial configuration:

server {
    listen        8080;
    server_name   www.mysite.com mysite.com;
    error_log     /home/www-data/logs/nginx_www.error.log;
    error_page    404    /404.html;

    location /public/doc/ {
        autoindex         on;
        alias             /home/www-data/mysite/public/doc/;
    }

    location = /404.html {
        alias             /home/www-data/mysite/static/html/404.html;
    }
}

This configuration should work for serving static HTML files from the specified directory. However, there's room for improvement, which we'll discuss in the next section.

Tip: Secure Your Nginx Configuration

Add security headers to your Nginx configuration to improve your website's security. Include the following lines within the server block:

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

These headers help protect against clickjacking, cross-site scripting (XSS), and MIME type sniffing attacks.

Optimizing the Nginx Configuration

Using the Root Directive for Better Performance

The initial configuration uses the 'alias' directive in the location block. Nginx documentation recommends using the 'root' directive instead when the location matches the last part of the directive's value. This change can improve performance.

Benefits of using 'root' instead of 'alias' in some cases:

  • Simplifies the configuration
  • May improve performance by reducing file system lookups
  • Follows Nginx best practices

Modifying the location block to use the root directive: Replace the 'alias' directive with 'root' in the location block for /public/doc/. The new configuration is:

location /public/doc/ {
    autoindex on;
    root  /home/www-data/mysite;
}

Adjusting file paths for the new configuration: When using the 'root' directive, Nginx adds the location to the path in the 'root' directive. You need to adjust the root path to serve files from the right location.

The optimized Nginx configuration now looks like this:

server {
    listen        8080;
    server_name   www.mysite.com mysite.com;
    error_log     /home/www-data/logs/nginx_www.error.log;
    error_page    404    /404.html;

    location /public/doc/ {
        autoindex on;
        root  /home/www-data/mysite;
    }

    location = /404.html {
        root /home/www-data/mysite/static/html;
    }
}

This optimized configuration keeps the same functionality as the original while following Nginx best practices for serving static files.

Tip: Test Your Nginx Configuration

After changing your Nginx configuration, always test it before reloading:

nginx -t

This command checks your configuration for syntax errors and issues. If the test passes, you can reload Nginx safely with:

nginx -s reload