# Matching server host name in location directive

**URL:** <https://www.ruby-forum.com/t/matching-server-host-name-in-location-directive/209069>\
**Category:** NGINX\
**Created:** [July 5, 2011, 7:50am UTC](https://www.ruby-forum.com/t/matching-server-host-name-in-location-directive/209069 "2011-07-05T07:50:13Z")\
**Posts on this page:** 5\
**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:** [July 5, 2011, 7:50am UTC](https://www.ruby-forum.com/t/matching-server-host-name-in-location-directive/209069/1 "2011-07-05T07:50:13Z")

</div>

Can someone help me answer this question at

[serverfault.com](http://serverfault.com)[http://serverfault.com/questions/286828/nginx-matching-server-host-name-in-location-directive](http://serverfault.com/questions/286828/nginx-matching-server-host-name-in-location-directive)?

* * *

Posted again

## I have nginx running multiple domains under a single server directive as server { listen 80; server\_name [www.domain.com](http://www.domain.com); server\_name [x.domain.com](http://x.domain.com); server\_name [y.domain.com](http://y.domain.com);

* * *

* * *

}

Now, I need to use location directive to match a subdomain and apply  
basic  
auth to it. The equivalent of

location [x.domain.com](http://x.domain.com) {  
auth\_basic “Admin Login”;  
auth\_basic\_user\_file /etc/nginx/.htpasswd;  
}

How do I do this using DRY?

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [July 5, 2011, 8:54am UTC](https://www.ruby-forum.com/t/matching-server-host-name-in-location-directive/209069/2 "2011-07-05T08:54:30Z")

</div>

On Tue, Jul 5, 2011 at 12:49 PM, Quintin P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> server_name x.domain.com;
> auth_basic "Admin Login";
> auth_basic_user_file /etc/nginx/.htpasswd;
> 
> ```
> 
> }
> 
> How do I do this using DRY?

server {  
server\_name [www.domain.com](http://www.domain.com);  
server\_name [y.domain.com](http://y.domain.com);  
include myapps.conf;  
}

server {  
server\_name [x.domain.com](http://x.domain.com);  
include myapps.conf;  
auth\_basic “Admin Login”;  
auth\_basic\_user\_file /etc/nginx/.htpasswd;  
}

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [July 5, 2011, 10:05am UTC](https://www.ruby-forum.com/t/matching-server-host-name-in-location-directive/209069/3 "2011-07-05T10:05:06Z")

</div>

On Tue, Jul 05, 2011 at 11:19:23AM +0530, Quintin P. wrote:

Hi there,

> I have nginx running multiple domains under a single server directive as  
> server {  
> listen 80;  
> server\_name [www.domain.com](http://www.domain.com);  
> server\_name [x.domain.com](http://x.domain.com);

> Now, I need to use location directive to match a subdomain and apply basic  
> auth to it. The equivalent of

location doesn’t match subdomains. server\_name matches subdomains. Use  
different server{}s for different things.

> location [x.domain.com](http://x.domain.com) {  
> auth\_basic “Admin Login”;  
> auth\_basic\_user\_file /etc/nginx/.htpasswd;  
> }
> 
> How do I do this using DRY?

make.

Or m4. Or a config file generator of your choice.

nginx doesn’t care how your .conf file came about, it just cares about  
what is in the .conf file when it is loaded.

Don’t worry about duplication or repetition in the generated output  
file. If you want to worry about repetition, do it in your input format.

Good luck,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [July 5, 2011, 10:29am UTC](https://www.ruby-forum.com/t/matching-server-host-name-in-location-directive/209069/4 "2011-07-05T10:29:47Z")

</div>

Thanks. This seems a bit crude, especially when you have a long server  
tag. A change/bug in one server tag will now have to to replicated to  
the x sub domains/server\_names.  
I was looking for an elegant solution without a lot of conf files.

-Quintin.

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [July 18, 2011, 5:30am UTC](https://www.ruby-forum.com/t/matching-server-host-name-in-location-directive/209069/5 "2011-07-18T05:30:59Z")

</div>

I prefer to use individual config for each site,

nginx.conf

…  
include /etc/nginx/sites-enabled/\*;  
…

/etc/nginx/sites-enable/somesite.conf

server {  
listen 80;  
server\_name [x.domain.com](http://x.domain.com);

location [x.domain.com](http://x.domain.com) {  
auth\_basic “Admin Login”;  
auth\_basic\_user\_file /etc/nginx/.htpasswd;  
}

}

Just my 2 cent 🙂 It’s helpful for me
