Configure Nginx Userdir Feature on Ubuntu 16.04 LTS Servers

Like Apache2 Userdir module, Nginx can also be configured to do the same.. albeit in a different way…

In most school environments is where you get to see Nginx Userdir feature in action… each teacher is given his/her own website where he/she can publish materials for students to access… 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 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 will be able to share content in their home directories with the world via Nginx..

This brief tutorial shows students and new users how to enable and configure it on Ubuntu 16.04 LTS Servers using Nginx…

To get started with enabling Userdir on Ubuntu 16.04 LTS, the steps below is a good place to start:

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

Step 1: Install Nginx HTTP Server

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

sudo apt install nginx

Step 2: Setup Userdir on Nginx

Unlike Apache2, there are no module to enable or installed.. Nginx implementation is a bit different…

To enable 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 opens Nginx default site configuration file..

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

Then add the highlighted block of code 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;
           }

}

Step 3: 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 webserver.

Restart Nginx webserver to load the settings.

sudo systemctl restart nginx.service

Now test it out by browsing to the server hostname or IP address followed by the username.

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

apache2 user dir

 

This is how 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!

Enjoy!

You may also like the post below:

Setup Apache2 Userdir Module on Ubuntu 16.04 LTS Servers

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.