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 2 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.

🐧Bash / Shell
sudo nano /etc/nginx/nginx.conf

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

Advertisement
💻Code
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.

💻Code
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.

💻Code
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.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

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 *