Nginx Directory Listing - Restrict by IP Address

Friscia, Michael michael.friscia at
Fri May 18 11:17:52 UTC 2018

I think you need to change this a little

map $remote_addr $allowed {
    default         “off”;         “on”;         “on:;
and then in in the download location block

 autoindex $allowed;
I use similar logic on different variables and try at all costs to avoid IF statements anywhere in the configs.

Michael Friscia
Office of Communications
Yale School of Medicine
(203) 737-7932 - office
(203) 931-5381 - mobile<>

From: nginx <nginx-bounces at> on behalf of PRAJITH <prajithpalakkuda at>
Reply-To: "nginx at" <nginx at>
Date: Friday, May 18, 2018 at 2:16 AM
To: "nginx at" <nginx at>
Subject: Re: Nginx Directory Listing - Restrict by IP Address

Hi Satish,
There are "if" constructs in nginx, please check<>. if you want to allow multiple IP addresses, it might be better idea to use map. eg:

map $remote_addr $allowed {
    default         0;         1;         1;
and then in in the download location block

 if ($allowed = 1) {
        autoindex on;

On 18 May 2018 at 05:35, Sathish Kumar <satcse88 at<mailto:satcse88 at>> wrote:

Hi Team,

We have a requirement to allow directory listing from few servers and disallow from other ip addresses and all IP addresses should be able to download all files inside the directory.

Can somebody provide the correct nginx config for the same.

location / {

root /downloads;

autoindex on;


deny all;


If I use the above config, only on IP address can directory list from this server and can file download but from other IP addresses download shows forbidden, due to IP address restriction

Is there a way to overcome this issue, thanks.

Thanks & Regards

nginx mailing list
nginx at<mailto:nginx at><>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list