# Conditional auth\_basic

**URL:** <https://www.ruby-forum.com/t/conditional-auth-basic/196003>\
**Category:** NGINX\
**Created:** [October 5, 2010, 7:34am UTC](https://www.ruby-forum.com/t/conditional-auth-basic/196003 "2010-10-05T07:34:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cedjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7993a0/32.png) [@cedjo](https://www.ruby-forum.com/u/cedjo)\
**Post date:** [October 5, 2010, 7:34am UTC](https://www.ruby-forum.com/t/conditional-auth-basic/196003/1 "2010-10-05T07:34:26Z")

</div>

Hello,

I have a special setup involving a wildcard :  
server {  
server\_name \*.domain.ltd  
access\_log /var/log/nginx/itnux\_acces.log main;  
error\_log /var/log/nginx/itnux\_error.log info;  
add\_header X-Devel “$host - production site”;

location / {  
proxy\_pass [http://localhost:9095](http://localhost:9095);  
proxy\_set\_header Host $host;  
}  
}

I’d like to add some auth\_basic, only if $host is NOT “demo.domain.ltd”.

I thought I could make it like that:  
location / {  
proxy\_pass [http://localhost:9095](http://localhost:9095);  
proxy\_set\_header Host $host;  
if ($host != ‘demo.domain.ltd’) {  
auth\_basic “Not for everyone ;)”;  
auth\_basic\_user\_file /var/www/vhosts/domain.ltd/htaccess;  
}  
}

But it seems that nginx doesn’t like it:  
nginx -t  
[emerg]: “auth\_basic” directive is not allowed here in  
/etc/nginx/vhosts/wildcard.domain.ltd.conf:17  
configuration file /etc/nginx/nginx.conf test failed

How can I do this ? Do I have to declare “demo.domain.ltd”, and then the  
wildcard in two distinct servers ?

Thank you in advance.

Best regards,

C.

---

<div class="post-metadata">

**Author:** ![cedjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7993a0/32.png) [@cedjo](https://www.ruby-forum.com/u/cedjo)\
**Post date:** [October 5, 2010, 1:41pm UTC](https://www.ruby-forum.com/t/conditional-auth-basic/196003/2 "2010-10-05T13:41:24Z")

</div>

Hello!

On Tue, Oct 05, 2010 at 07:33:40AM +0200, Cedric J. wrote:

> location / {  
> proxy\_pass [http://localhost:9095](http://localhost:9095);  
> configuration file /etc/nginx/nginx.conf test failed
> 
> How can I do this ? Do I have to declare “demo.domain.ltd”, and then the wildcard in two distinct servers ?

Yes, use two distinct server{} blocks.

server {  
server\_name \*.domain.ltd;

auth\_basic …

…  
}

server {  
server\_name demo.domain.ltd;

…  
}

Maxim D.

---

<div class="post-metadata">

**Author:** ![cedjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7993a0/32.png) [@cedjo](https://www.ruby-forum.com/u/cedjo)\
**Post date:** [October 5, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/conditional-auth-basic/196003/3 "2010-10-05T14:01:19Z")

</div>

Ok, thank you for your answer.

A bit ugly, knowing we can put some conditions in nginx config file ;).

But this way is working.

Cheers,

C.

On Tue, 5 Oct 2010 15:40:30 +0400
