How to configure Basic Auth login authentication in Nginx

How to configure Basic Auth login authentication in Nginx

Sometimes we build a file server through nginx, which is generally public, but we hope that the server is not visible to others. Some people may build a login system, but it is too troublesome and unnecessary. A simpler way is to configure Basic Auth login authentication.

1. Make sure you have httpd-tools installed

yum install httpd-tools -y

2. Create an authorized user and password

htpasswd -c -d /usr/local/openresty/nginx/conf/pass_file magina

The storage path of this configuration file can be specified at will. Here I specify the nginx configuration file directory, where magina refers to the user name allowed to log in, which can be customized

3. Configure Nginx

The general configuration is as follows:

server {
  listen 80;  
  server_name res.yinnote.com;

  auth_basic "Login Authentication"; 
  auth_basic_user_file /usr/local/openresty/nginx/conf/pass_file;

  autoindex on;
  autoindex_exact_size on;
  autoindex_localtime on;

  root /mnt/html/resource;
  index index.html index.php;
}

Among them, auth_basic and auth_basic_user_file are authentication configurations. Note that the path of the password file must be the one generated above.

4. Use

# Use in the browser. Enter the address directly in the browser, and a user password input box will pop up. Enter it to access. # Use wget
wget --http-user=magina --http-passwd=123456 http://res.yinnote.com/xxx.zip

# Using curl
curl -u magina:123456 -O http://res.yinnote.com/xxx.zip

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of nginx basic auth configuration pitfalls
  • Tutorial on configuring Basic Authorization login authentication service certificate on Nginx

<<:  MySQL free installation version configuration tutorial

>>:  js canvas to realize the Gobang game

Recommend

JavaScript plugin encapsulation for table switching

This article shares the encapsulation code of Jav...

How to delete table data in MySQL

There are two ways to delete data in MySQL, one i...

CSS Sticky Footer Implementation Code

This article introduces the CSS Sticky Footer imp...

Mysql online recovery of undo table space actual combat record

1 Mysql5.6 1.1 Related parameters MySQL 5.6 adds ...

Summary of several implementations of returning to the top in HTML pages

Recently, I need to make a back-to-top button whe...

Add unlimited fonts to your website with Google Web Fonts

For a long time, website development was hampered...

Learn the operating mechanism of jsBridge in one article

Table of contents js calling method Android 1.js ...

Practice of dynamically creating dialog according to file name in vue+el-element

Table of contents background accomplish 1. Encaps...

Detailed explanation of Linux environment variable configuration strategy

When customizing the installation of software, yo...

Summary of using MySQL online DDL gh-ost

background: As a DBA, most of the DDL changes of ...

Using JavaScript difference to implement a comparison tool

Preface At work, I need to count the materials su...

Practical notes on installing Jenkins with docker-compose

Create a Directory cd /usr/local/docker/ mkdir je...

HTML user registration page settings source code

Design the web page shown above: <!DOCTYPE htm...