
Do you want to disable directory listing on your Nginx server but don’t know how? We’ll configure the Nginx server to disable directory browsing in this tutorial.
First, we need to install the Nginx server.
apt-get update
apt-get install nginx
Next, edit the Nginx configuration file for the default website.
vi /etc/nginx/sites-available/default
Add the following line to the configuration file.
autoindex off;
This is the file, before our configuration.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
This is the file, after our configuration.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
autoindex off;
try_files $uri $uri/ =404;
}
}
Restart the Nginx service.
service nginx restart
In our example, the directory listing feature was disabled on the Nginx server.
From a remote computer, open the browser and try to access a directory of the Nginx server.
• http://192.168.2.113/test
The Nginx server will display an error message.
