# Using auth\_basic in dynamic vhosts

**URL:** <https://www.ruby-forum.com/t/using-auth-basic-in-dynamic-vhosts/142401>\
**Category:** NGINX\
**Created:** [July 7, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/using-auth-basic-in-dynamic-vhosts/142401 "2008-07-07T16:52:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Brotherston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lee\_Brotherston](https://www.ruby-forum.com/u/Lee_Brotherston)\
**Post date:** [July 7, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/using-auth-basic-in-dynamic-vhosts/142401/1 "2008-07-07T16:52:25Z")

</div>

Hi,

I have an nginx installation which allows vhosts to be setup  
dynamically:

-------8\<-------

server {  
listen 10.1.2.3:80;  
root /data/www/$host;  
index index.php index.html;

```
    location ~ .php$ {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/.fastcgi.www/socket;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME 

```

/data/www/$host/$fastcgi\_script\_name;  
fastcgi\_param DOCUMENT\_ROOT /data/www/$host/;  
}

-------8\<-------

I had trouble writing a way to block access to certain directories,  
without applying them to all vhosts. However I eventually realised  
that you could do this:

-------8\<-------  
if ($host ~\* [specific-vhost.domain.com](http://specific-vhost.domain.com)) {  
rewrite ^/private.\* @403;  
rewrite ^/admin.\* @403;  
}

```
    location = @403 {
            return 403;
    }

```

-------8\<-------

This way I can restrict (in this case /private and /admin) on a  
specific vhost, and not apply the same config option to all the other  
vhosts.

However I have now come up against the problem of trying to implement  
something similar with auth\_basic. A couple of vhosts need to have  
auth\_basic applied to / … obviously this means that I cannot just  
use the location operative as this will apply to all vhosts.

I attempted to use a similar setup as above, with an “@auth” location  
and the appropriate rewrites, and whilst the authentication works  
properly, I cannot make the scripts run. Typically I either get “No  
Input Files” which indicates that fastcgi is not obtaining the right  
path to pass to php, or a 404 error, which is probably my fault 😉

If anyone can offer any advice it would be greatly appreciated.

Many thanks

Lee

---

<div class="post-metadata">

**Author:** ![Lee\_Brotherston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lee\_Brotherston](https://www.ruby-forum.com/u/Lee_Brotherston)\
**Post date:** [July 7, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/using-auth-basic-in-dynamic-vhosts/142401/2 "2008-07-07T17:47:03Z")

</div>

Lee Brotherston ha scritto:

> ```
> index index.php index.html;
> 
> location = @403 {
> return 403;
> }
> 
> ```
> 
> -------8\<-------

Isn’t it better to simply add complete server blocks for these specific  
hosts?

> […]

Manlio P.

---

<div class="post-metadata">

**Author:** ![Lee\_Brotherston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lee\_Brotherston](https://www.ruby-forum.com/u/Lee_Brotherston)\
**Post date:** [July 8, 2008, 9:44am UTC](https://www.ruby-forum.com/t/using-auth-basic-in-dynamic-vhosts/142401/3 "2008-07-08T09:44:36Z")

</div>

On Mon, Jul 07, 2008 at 05:38:13PM +0200, Manlio P. wrote:

> Isn’t it better to simply add complete server blocks for these specific  
> hosts?

Hi Manlio,

Whilst I agree that this is a way of achieving what I want, I don’t  
necessarily think that it is the best way. I am trying to avoid  
duplicating configuration (i.e. the rest of the server setup) across  
multiple vhosts, so that a change need only be applied in one place to  
affect all vhosts.

Thanks

Lee

---

<div class="post-metadata">

**Author:** ![Lee\_Brotherston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lee\_Brotherston](https://www.ruby-forum.com/u/Lee_Brotherston)\
**Post date:** [July 8, 2008, 11:29am UTC](https://www.ruby-forum.com/t/using-auth-basic-in-dynamic-vhosts/142401/4 "2008-07-08T11:29:06Z")

</div>

Hi Lee, you could try factoring chunks of what you want to do out into  
separate files and using “include” so that you only have to change it  
there
