Home > Operation and Maintenance > Nginx > How to use Nginx to implement HTTP basic authentication

How to use Nginx to implement HTTP basic authentication

WBOY
Release: 2023-06-10 10:36:14
Original
2521 people have browsed it

Nginx is a high-performance, reliable web server and reverse proxy server. In addition to its powerful load balancing and caching capabilities, Nginx also supports HTTP basic authentication. HTTP basic authentication is a simple authentication method that verifies the user's identity by adding a username and password in the request header. In this article, we will learn how to implement HTTP basic authentication using Nginx.

1. Set username and password

First, we need to create a password file. We can use the htpasswd command to create this file. htpasswd is a command line tool for managing HTTP basic authentication passwords.

We can use the following command in the terminal to create a password file:

htpasswd -c /etc/nginx/.htpasswd username
Copy after login

Among them, -c means to create a new password file, /etc/nginx/.htpasswd is the path to the password file , username is the username we want to add. After running this command, you will be prompted for your password. After entering the password, htpasswd will hash the password and store it in the password file.

If you want to add additional users to the password file, use the following command:

htpasswd /etc/nginx/.htpasswd username2
Copy after login

This command does not have the -c option because our password file already exists. After entering the command, you will be prompted for your password. Once entered, htpasswd hashes and adds the username and password to a password file.

2. Apply basic authentication in Nginx

Now, we have created a password file containing username and password hashes. Next, we need to apply HTTP basic authentication in Nginx.

We need to add the following directives in the server block or location block:

auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
Copy after login

The first directive tells Nginx to perform basic authentication on the request and display "Restricted" in the pop-up login box as Description.

The second directive tells Nginx to use the /usr/share/nginx/.htpasswd file to authenticate the user.

For example, we can add the following code in Nginx's default server block:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}
Copy after login

Now, we have successfully added basic authentication to our Nginx server.

3. Test HTTP basic authentication

Now let’s test the authentication function we just set up. We can use curl or Firefox and other tools to test.

Test using curl:

curl -I http://localhost
Copy after login

The command line will prompt you for your username and password. After entering the correct username and password, you can see the following code included in the HTTP response header:

HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
...
Copy after login

Test with Firefox:

Enter the server address http://localhost in Firefox. Firefox will pop up a window with the word "Restricted" and a description, prompting you to enter your username and password. After entering the correct username and password, you will be redirected to your server default web page.

Summary

This article introduces how to use Nginx to implement HTTP basic authentication. HTTP Basic Authentication is a simple but effective authentication method that can be used to protect sensitive resources of a web server. We can easily add HTTP Basic Authentication to our Nginx server by first creating a password file using the htpasswd command and then applying the Basic Authentication directive in the Nginx configuration.

The above is the detailed content of How to use Nginx to implement HTTP basic authentication. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template