Skip to content
Follow
Ubuntu Linux

Disable Nginx Directory Listing on Ubuntu 24.04

Richard
Written by
Richard
Feb 19, 2025 Updated Sep 15, 2026 3 min read
Disable Nginx Directory Listing on Ubuntu 24.04
Disable Nginx Directory Listing on Ubuntu 24.04

Disabling Nginx directory listing on Ubuntu 24.04 stops visitors from browsing your server files and finding private data when a web folder lacks a main page like `index.html`. This feature, controlled by the `autoindex` rule, shows a file list to anyone who opens an empty folder unless you turn it off.

Advertisement

You can turn off this file browser by setting `autoindex off;` inside your Nginx configuration file. Once you save this change and restart the web server, anyone who tries to view an empty directory gets a 403 Forbidden error instead of seeing your files.

⚡ Quick Answer

Edit your Nginx configuration file, typically `/etc/nginx/nginx.conf`, and set `autoindex off;` within the http block. Alternatively, disable it for a specific site by adding `autoindex off;` within its server block’s location. Restart Nginx to apply changes.

Advertisement

Disable directory listing

You can disable Nginx directory listing across your entire Ubuntu 24.04 server by editing the main configuration file at /etc/nginx/nginx.conf. Add the command autoindex off; inside the http block to stop Nginx from showing your file folders to the public when an index page is missing.

You will find a line in the Nginx configuration file that controls directory listing. This directory listing feature is enabled by default in Nginx.

Run the command below to open the global configuration file.

sudo nano /etc/nginx/nginx.conf

The http {} block with the ‘autoindex‘ option enables or disables directory listing in Nginx globally.

Advertisement
http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;

autoindex on;

....
}

Remove or add the 'autoindex on;' line in the configuration to enable directory listing globally.

If you want to disable the directory listing for all content in Nginx, change the block above to the one below.

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;

autoindex off;

....
}

Save the exit the file.

Restart Nginx to apply your changes.

Advertisement

Add the 'autoindex on;' to re-enable the directory listing.

Disable directory listing selectively

You can disable Nginx directory listing for a single website without affecting your other projects by editing that specific site's configuration file in /etc/nginx/sites-available/. Open the file, add autoindex off; inside the server block, and reload the web server to hide your files for that domain only.

For example, here's a basic configuration for a website that disables directory listing.

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;
}
}

This configuration only impacts the /var/www/html/ website in Nginx's directory.

Advertisement

That should do it!

Conclusion:

  • Enabling directory listing allows users to see directory contents, which may be useful for public access.
  • Disabling directory listing enhances security by preventing unauthorized exploration of files.
  • The global setting in Nginx can easily be adjusted in the nginx.conf file.
  • Selective configuration in individual website files provides flexibility in managing directory listing.
  • Always remember to restart Nginx after making changes to apply the new settings.

Why is it beneficial to disable the webserver directory listing functionality?

Leaving directory listing enabled can expose critical information, such as hidden scripts, backups, or configuration files, which could be used in cyberattacks. Disabling it adds an extra layer of security, ensuring that unauthorized users cannot easily browse and analyze your server's structure.

What is directory listing enabled?

Directory listing is a web server function that can cause a vulnerability. When enabled, it displays the contents of a directory that has no index file. This function should always be turned off. It is dangerous to leave it enabled because it leads to information disclosure.

What are two remediation methods for preventing directory listing exploits?

Remediation: Directory listing This can normally be achieved in two ways: Configure your web server to prevent directory listings for all paths beneath the web root; Place into each directory a default file (such as index. htm) that the web server will display instead of returning a directory listing.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

Advertisement

📚 Related Tutorials

How to Setup WordPress Nginx Cloudflare Ubuntu Fast
CMS How to Setup WordPress Nginx Cloudflare Ubuntu Fast
How to Add or Remove Index Locations in Windows 11
Windows How to Add or Remove Index Locations in Windows 11
How to Setup Let's Encrypt with Nginx on Ubuntu Linux
Ubuntu Linux How to Setup Let's Encrypt with Nginx on Ubuntu Linux

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *