Home > Operation and Maintenance > Nginx > How to configure Nginx to list directories and files and control access permissions with passwords

How to configure Nginx to list directories and files and control access permissions with passwords

WBOY
Release: 2023-05-17 21:49:04
forward
1606 people have browsed it

Use nginx to list directories

Preparation - install nginx and create some directories and files:

Copy the code The code is as follows:


$ sudo apt-get install nginx
$ cd /usr/share/nginx
$ mkdir web
$ sudo mkdir directory{1,2,3}
$ sudo touch file {1,2,3}
$ ls
directory1 directory2 directory3 file1 file2 file3


##nginx configuration:

Copy code The code is as follows:

$ sudo vim /etc/nginx/conf.d/list.conf

server {

listen 80;
server_name 192.168.1.201;

root /usr/share/nginx/web;

index index.html;

autoindex on;

autoindex_exact_size off;
autoindex_localtime on;
}

$ sudo service nginx restart/reload


When accessing http://192.168.1.201, the screen displays:

How to configure Nginx to list directories and files and control access permissions with passwords

Now You can use a web server to share files; but the prerequisite is that there must be no index.html file in the root directory.

Authorize access to specified directories

Some directories do not want everyone to see them. They can only be accessed by designated users after entering their passwords. For example, directory can only be accessed by test1:

#step1Install htpasswd

Copy code The code is as follows:

$ sudo apt-get install apache2-utils

step2 Create a password file

Copy code The code is as follows:

$ sudo htpasswd -c /home/test/. htpasswd test1
new password:
re-type new password:
adding password for user test1

step3Modify nginx configuration file

Copy code The code is as follows:

$ sudo vim /etc/nginx/conf.d/list.conf

server {

listen 80;
server_name 192.168.1.201;

root /usr/share/nginx/web;

index index.html;

autoindex on;

autoindex_exact_size off;
autoindex_localtime on;

location ^~/directory1/ {

auth_basic "input you user name and password";
auth_basic_user_file /home/test/.htpasswd;
}
}

After restarting the nginx service, you will be prompted to enter login information when accessing the directory1 directory:

How to configure Nginx to list directories and files and control access permissions with passwords

The above is the detailed content of How to configure Nginx to list directories and files and control access permissions with passwords. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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