Would you like to learn how to configure the PAM authentication on the Nginx server? In this tutorial, we are going to show you how to configure the Nginx service to authenticate users using the Plugable Authentication module also known as PAM on a computer running Ubuntu Linux.

• Ubuntu 20
• Ubuntu 19
• Ubuntu 18
• Nginx 1.18.0

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Nginx - PAM authentication

Install the Nginx server and the required packages.

Copy to Clipboard

In our example, we are going to request authentication to users trying to access a directory named TEST.

Create a directory named TEST and give the user named www-data permission over this directory.

Copy to Clipboard

Configure the Nginx server to request the PAM authentication to users trying to access this directory.

Edit the Nginx configuration file for the default website.

Copy to Clipboard

Add the following configuration to this file.

Copy to Clipboard

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

The Nginx server was configured to request password authentication to access the directory named TEST.

The Nginx service was configured to authenticate user accounts using the PAM authentication module.

Create the PAM configuration file.

Copy to Clipboard

Here is the file content.

Copy to Clipboard

In our example, we are going to authenticate the Nginx service access using the Linux accounts.

Authorize the Nginx service to read the SHADOW file.

Copy to Clipboard

Restart the Nginx service.

Copy to Clipboard

Congratulations! You successfully configured the Nginx authentication.

Nginx - PAM authentication Test

Create a Linux account to be used in the authentication test.

Copy to Clipboard

In our example, we created a Linux account named ADMIN.

The password configured was 123qwe.

Create an HTML page to be used in the authentication test.

Copy to Clipboard

In our example, we created an HTML page named TEST.

Open your browser and enter the IP address of your Nginx web server.

In our example, the following URL was entered in the Browser:

• http://192.168.15.11

The Nginx default page will be displayed.

nginx welcome

Open your browser and enter the IP address of your web server plus /test.

In our example, the following URL was entered in the Browser:

• http://192.168.15.11/test/test.html

On the login screen, Enter a local username and its password.

• Username: admin
• Password: 123qwe

Apache login form

After a successful login, you will be authorized to access the directory named TEST.

Nginx authentication test

Congratulations! You have configured the PAM authentication on the Nginx server.