How to configure Nginx UserDir in Ubuntu Linux

|

|

The post details how to create a UserDir feature on an Nginx HTTP server in Ubuntu Linux, similar to the Apache2 UserDir module. This allows users with accounts on the system to share content from their home directories through Nginx. The post provides a step-by-step guide on installation, setting up UserDir, creating user directories, and…

This post describes creating a UserDir feature with an Nginx HTTP server in Ubuntu Linux.

Like the Apache2 UserDir module, Nginx can also be configured to do the same, albeit differently.

In some school environments, you’ll see examples where teachers are given their website to publish materials for students to access. Instead of creating multiple server blocks, the system administrator can use the UserDir feature.

The URL to reach the teacher’s page is usually the domain name followed by /~teacher_name.

User Directory, or Userdir for short, is a feature for the Nginx web server that allows user-specific directories to be accessed via Nginx. 

For example, when you enable this feature in Nginx, users with accounts on the system can share content in their home directories with the world via Nginx.

This tutorial assumes that you already have an Nginx web server installed. If you haven’t, you may want to do that before continuing below.

Install Nginx HTTP Server

First, run the commands below to install the Nginx HTTP Server on Ubuntu.

sudo apt install nginx

Setup UserDir on Nginx

Unlike Apache2, there is no module to enable or install. Therefore, Nginx implementation is a bit different.

To enable the Nginx Userdir feature, open the default website configuration file. Or create a new one for the domain you want to have user share their directories.

The commands below open the Nginx default site configuration file.

sudo nano /etc/nginx/sites-available/default

Then add the highlighted block of code to the settings below:

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
        server_name example.com www.example.com;

         location ~ ^/~(.+?)(/.*)?$ {
         alias /home/$1/public_html$2;
         index index.html index.htm;
         autoindex on;
           }

}

Creating User Directories

Now that the feature is enabled, all users have to do is run the commands below to create a folder in their home directories called public_html by running the commands below.

mkdir ~/public_html

In the ~/public_html folder, create HTML documents to be shared and accessed via the web server.

Restart the Nginx web server to load the settings.

sudo systemctl restart nginx.service

Test it by browsing the server hostname or IP address followed by the username.

example: http://example.com/~richard

This is how the Nginx Userdir feature is configured for users to allow users with accounts on the systems to share content from their home directories.

That’s it!

Like this:



One response to “How to configure Nginx UserDir in Ubuntu Linux”

  1. Hans Avatar
    Hans

    Nice – easy to follow.

    One little correction will make it work with all umask (Debian default):

    mkdir -m 0755 ~/public_html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.