# Autoindex directive in 'if' section

**URL:** <https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151>\
**Category:** NGINX\
**Created:** [January 26, 2012, 8:17pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151 "2012-01-26T20:17:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 26, 2012, 8:17pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/1 "2012-01-26T20:17:38Z")

</div>

Hello,

I would like to use the following in my conf file:  
location a/ {  
if ($host ~\* “”) {  
root “”;  
autoindex on;  
}  
}

But that doesn’t work: ‘nginx: [emerg] “autoindex” directive is not  
allowed  
here’

What should I do?

## Thanks,

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [January 26, 2012, 8:21pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/2 "2012-01-26T20:21:57Z")

</div>

Hello!

On Thu, Jan 26, 2012 at 02:16:22PM -0500, B.R. wrote:

> But that doesn’t work: ‘nginx: [emerg] “autoindex” directive is not allowed  
> here’
> 
> What should I do?

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [January 27, 2012, 9:44pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/3 "2012-01-27T21:44:13Z")

</div>

I know this Wiki page…  
I guess return/rewrite are useless here: I only want to change the root  
and activate the directory listing when the user is on a specific  
directory of a specific domain.

Any more help ? :\

Thx,  
B.R.

* * *

De: Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Envoy le : Jeudi 26 janvier 2012 14h21  
Objet: Re: autoindex directive in ‘if’ section

Hello!

On Thu, Jan 26, 2012 at 02:16:22PM -0500, B.R. wrote:

> But that doesn’t work: ‘nginx: [emerg] “autoindex” directive is not allowed  
> here’
> 
> What should I do?

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [January 27, 2012, 11:43pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/4 "2012-01-27T23:43:03Z")

</div>

Thanks for the help!  
Needed some other tricks playing with the path…

## Many thanks again! ;o)

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [January 27, 2012, 10:52pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/5 "2012-01-27T22:52:21Z")

</div>

28 января 2012, 00:44 от “removed\_email\_address@domain.invalid” : \> I know this Wiki  
page… \> I guess return/rewrite are useless here: I only want to change  
the root and \> activate the directory listing when the user is on a  
specific directory of a \> specific domain. \> \> Any more help ? :\ I just  
explained how this is handled here:  
[How to proxy\_pass POST with headers](http://mailman.nginx.org/pipermail/nginx/2012-January/031622.html) In  
your case use the rewrite inside the if block and and then change root  
and set autoindex inside the internal URI block. Max

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [January 28, 2012, 12:03am UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/6 "2012-01-28T00:03:27Z")

</div>

Erk…  
Didn’t make all my tests :o)

To end the job, I need to get rid of the ‘post\_rewrite’ part of the  
path,  
so my files get served as ‘/path/to/files’ and not  
‘/post\_rewrite/path/to/files’.  
I tried rewrite, but of course that gets me out of the current location  
block…

## Should I replace the content of $uri? Is there any variable regex substitution mechanism in nginx? Couldn’t find that on Google nor wiki. Maybe I am not searching for the right thing.

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [January 28, 2012, 9:21pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/7 "2012-01-28T21:21:09Z")

</div>

## So simple… Thanks for everything Max, it works perfectly! ;o)

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [February 1, 2012, 10:47pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/8 "2012-02-01T22:47:08Z")

</div>

Hello,

Finally, a little something still annoying me…

Since the location is based on the new /post\_redirect/ folder, the  
indexing  
page shows:  
Index of /post\_redirect/\*\*\*\*\*\*\*

## It is unaesthetic to me, since I would like the original URI there. :o Any trick on that?

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [January 28, 2012, 6:40pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/9 "2012-01-28T18:40:33Z")

</div>

28 января 2012, 03:11 от “B.R.” : \> To end the job, I need to get rid of  
the ‘post\_rewrite’ part of the path, \> so my files get served as  
‘/path/to/files’ and not \> ‘/post\_rewrite/path/to/files’. \> I tried  
rewrite, but of course that gets me out of the current location \>  
block… \> \> Should I replace the content of $uri? Is there any  
variable regex \> substitution mechanism in nginx? \> Couldn’t find that  
on Google nor wiki. Maybe I am not searching for the \> right thing.  
[http://wiki.nginx.org/HttpCoreModule#alias](http://wiki.nginx.org/HttpCoreModule#alias) Use alias instead of root, it  
does exactly what you need: location ~ ^/post\_rewrite/(.\*)$ { internal;  
autoindex on; alias /sandbox/$1; } Max

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [February 2, 2012, 6:10pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/10 "2012-02-02T18:10:56Z")

</div>

02 февраля 2012, 01:46 от “B.R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Since the location is based on the new /post\_redirect/ folder, the indexing page  
> shows:  
> Index of /post\_redirect /\*\*\*\*\*\*\*
> 
> It is unaesthetic to me, since I would like the original URI there. :o  
> Any trick on that?

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Set whatever log\_format you want inside the /post\_redirect/ location  
block.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

You can also turn log\_subrequest off inside the if block  
to prevent the rewrite from showing up in the logs.

Max

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [February 2, 2012, 6:54pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/11 "2012-02-02T18:54:17Z")

</div>

## OK, thanks for the answer!

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [February 2, 2012, 6:32pm UTC](https://www.ruby-forum.com/t/autoindex-directive-in-if-section/216151/12 "2012-02-02T18:32:02Z")

</div>

02 февраля 2012, 01:46 от “B.R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Since the location is based on the new /post\_redirect/ folder, the indexing  
> page shows:  
> Index of /post\_redirect/\*\*\*\*\*\*\*
> 
> It is unaesthetic to me, since I would like the original URI there. :o  
> Any trick on that?

Sorry, I misunderstood your question. If you want autoindex to mask  
the directory name, you’ll have to change the source code yourself:

File src/http/modules/ngx\_http\_autoindex\_module.c  
Function: ngx\_http\_autoindex\_handler(ngx\_http\_request\_t \*r)  
Variable: dir

You could extend the module to include a new command for  
such masking (autoindex\_mask\_dir “/dir\_to\_display/”), it shouldn’t  
take more than 20 minutes to code.

Max
