Disable Directory Listing on Apache & Nginx
Directory listing is a web server feature that displays the contents of a folder when no index file (like index.html) exists. While sometimes useful for public file sharing, it can expose sensitive files and data to anyone who knows the URL. This guide shows you how to turn it off on both Apache and Nginx, step by step, with configuration examples and troubleshooting tips.
What You’ll Need
- Access to your server’s terminal (SSH) or a control panel that allows editing server configuration files.
- For Apache: the ability to edit
.htaccessfiles or the main Apache configuration (e.g.,httpd.conforapache2.conf). - For Nginx: the ability to edit the server block configuration (usually inside
/etc/nginx/sites-available/or/etc/nginx/conf.d/). - Root or sudo privileges to restart the web server after changes.
- Basic familiarity with the command line and text editors (like nano or vim).
Step-by-Step
Disabling Directory Listing on Apache
- Locate the configuration file. For most shared hosting, you can use an
.htaccessfile in the directory you want to protect. For full control (e.g., a VPS), edit the Apache virtual host file or the globalhttpd.conf/apache2.conf. - Add the directive. In the chosen file, add the line
Options -Indexes. If you want to disable listing for a specific directory, place it inside a<Directory /path/to/folder>block or in the.htaccessfile inside that folder. - If you already have an
Optionsline (e.g.,Options Indexes FollowSymLinks), simply removeIndexesfrom that line, or change it toOptions -Indexesto explicitly turn it off. - Save the file.
- Restart or reload Apache using a command like
sudo systemctl reload apache2(for Debian/Ubuntu) orsudo systemctl reload httpd(for RHEL/CentOS). If you only edited.htaccess, no restart is needed—changes take effect immediately.
Disabling Directory Listing on Nginx
- Open the server block configuration for your site. This is usually in
/etc/nginx/sites-available/your-siteor/etc/nginx/conf.d/your-site.conf. - Find the
locationblock that handles the directory you want to protect. Often it’s thelocation /block, but you may have specific ones likelocation /uploads. - Add the directive
autoindex off;inside that location block. If the lineautoindex on;exists, change it tooff. - Save the file.
- Test the configuration with
sudo nginx -tto ensure there are no syntax errors. - Reload Nginx with
sudo systemctl reload nginx(orsudo service nginx reload).
Common Mistakes
- Editing the wrong file. On Apache, if you use
.htaccess, make sure the file is named exactly that (including the leading dot) and thatAllowOverridepermitsOptionsin the server config. On Nginx, editing a file that isn’t included in the active site config will have no effect. - Forgetting to restart/reload. Changes to main config files won’t apply until the server reloads.
.htaccesschanges are immediate, but Nginx and Apache main config changes require a reload. - Placing the directive in the wrong scope. On Nginx, putting
autoindex off;outside alocationblock (e.g., in thehttpblock) applies globally, which may be fine, but if you want per-directory control, it must be inside the appropriatelocation. - Using
Options -Indexesin Apache but havingAllowOverrideset toNone. This prevents.htaccessfrom working. You’ll need to add the directive to the virtual host or main config instead.
Tips
- After disabling directory listing, test by visiting a URL that points to a folder without an index file. You should see a 403 Forbidden error instead of a file list.
- If you have many directories, consider disabling listing globally and then re-enabling it only for specific public file-sharing folders (using
Options +Indexesorautoindex on;in those locations). - For Apache, you can also add a custom error document for 403 errors to make the page more user-friendly.
- On Nginx, you can return a custom 403 page using the
error_pagedirective. - Regularly audit your server configuration to ensure no accidental re-enabling of directory listing after updates or migrations.
Troubleshooting
- Directory listing still appears. Check that you edited the correct file and that the server reloaded. For Apache, verify that
AllowOverrideis set toAllif using.htaccess. For Nginx, confirm the location block you edited is the one being used—usenginx -Tto dump the full effective configuration. - You get a 500 Internal Server Error after editing Apache config. This often means a syntax error. Run
apachectl configtestto check. For Nginx, runnginx -t. - Changes not applied to a subdirectory. On Apache, if a subdirectory has its own
.htaccesswithOptions +Indexes, it overrides the parent setting. Remove or edit that file. On Nginx, a more specific location block (e.g.,location /subdir) may haveautoindex on;—check all relevant blocks. - After disabling, you get a 403 but you wanted a custom error page. Add an
ErrorDocument 403 /custom-403.htmlin Apache orerror_page 403 /custom-403.html;in Nginx, and ensure the file exists.
Frequently Asked Questions
Does disabling directory listing affect existing files?
No, it only prevents the server from generating a list of files. Files remain accessible if you know their exact URLs.
Can I disable directory listing for just one folder?
Yes. In Apache, place Options -Indexes in that folder’s .htaccess or a <Directory> block. In Nginx, add autoindex off; inside the specific location block for that folder.
What is the difference between autoindex off; and removing autoindex entirely?
In Nginx, the default value of autoindex is off, so removing the line has the same effect. However, explicitly writing autoindex off; makes your intention clear and guards against future changes.
Does this work on all hosting providers?
Most shared hosts allow .htaccess for Apache, but some may restrict certain directives. Nginx is rarely used on shared hosting without root access, so you may need to contact your provider to change the config.
Will disabling directory listing improve security?
It reduces information disclosure, but it doesn’t protect files from being accessed if someone guesses the URL. For sensitive files, use proper authentication and access controls.
Can I still use directory listing for a public download area?
Yes, you can enable it for specific directories by adding Options +Indexes (Apache) or autoindex on; (Nginx) in those locations, while keeping it disabled globally.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.