# Custom configuration

**URL:** <https://www.ruby-forum.com/t/custom-configuration/218312>\
**Category:** NGINX\
**Created:** [March 27, 2012, 11:47pm UTC](https://www.ruby-forum.com/t/custom-configuration/218312 "2012-03-27T23:47:30Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![Adnan\_RIHAN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@Adnan\_RIHAN](https://www.ruby-forum.com/u/Adnan_RIHAN)\
**Post date:** [March 30, 2012, 8:14pm UTC](https://www.ruby-forum.com/t/custom-configuration/218312/21 "2012-03-30T20:14:59Z")

</div>

On Fri, Mar 30, 2012 at 05:45:05PM +0200, Adnan RIHAN wrote:

Hi there,

> Then, here is the vhost:

Great, this shows why things are working the way they are.

> > location ~\*  
> > ^.+.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|iso|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|ogv|ogg|flv|swf|mpeg|mpg|mpeg4|mp4|avi|wmv|js|css)$  
> > {

This is the first regex location that matches mp3 urls, so this is the  
one that will be used.

For what I think you want to do, you should remove “|mp3” from up there,  
and then have the separate “~\* .mp3$” location later.

> > expires 24h;  
> > root /home/megasoun/public\_html;  
> > error\_page 404 = @apache;  
> > access\_log /usr/local/apache/domlogs/mega-sound.be;  
> > log\_not\_found off;

Based on the rest of the config, you can probably move some of those to  
“outside all locations”, so that they will be inherited into all  
locations  
that don’t otherwise set them.

\> \> location / { \> \> proxy\_pass http://87.98.132.48:8888/; \> \> }

> > location @apache {  
> > proxy\_pass [http://87.98.132.48:8888](http://87.98.132.48:8888);  
> > }

> > include “/etc/cpnginx/custom/mega-sound.be”;  
> > }

> > root@shadow [/usr/local/nginx/conf]# cat /etc/cpnginx/custom/mega-sound.be

As above, because this is included last in the main file, the mp3  
location  
here can never match.

So if you remove the “|mp3” up there, then this one will match:

> > location ~\* .mp3$ {  
> > more\_set\_headers ‘X-MyHeader: .mp3’;

and now you can set your types, or do whatever else is needed, and have  
it take effect.

(If your default or server-level “root” is not correct for this  
location,  
you’ll want to set that in here too.)

Because of the nginx location matching rules, this next location won’t  
be used for /adata/123.mp3, but will be used for /adata/123.html. You  
can  
test that by putting a file of the right name in the right  
(“root”/adata)  
directory, and seeing the X- header appearing.

> > location /adata {  
> > more\_set\_headers ‘X-MyHeader: adata’;  
> > }

And these two are outside of all locations, so apply to everything that  
does not override them.

> > more\_set\_headers ‘X-MyMasterHeader: set’;  
> > more\_set\_headers ‘X-My2Header: $request\_uri’;

> Here is the HTTP Headers for the root URL (www.mega-sound.be):

This is expected to be proxy\_pass’ed to port 8888, and you can see that  
your “all” more\_set\_headers directives did apply.

> > HTTP/1.1 200 OK  
> > X-MyMasterHeader: set  
> > X-My2Header: /

> Here is the headers for an MP3 ([http://www.mega-sound.be/adata/362.mp3](http://www.mega-sound.be/adata/362.mp3)):

This would match your original long location, so will be the file  
/home/megasoun/public\_html/adata/362.mp3 if it exists, and proxied to  
port 8888 if it doesn’t. Again, your “all” more\_set\_headers apply.

> > HTTP/1.1 200 OK  
> > Content-Type: audio/mpeg  
> > X-MyMasterHeader: set  
> > X-My2Heade: /adata/362.mp3

If you remove “|mp3” from the first location; or just “include” this  
file  
at the top instead of the bottom of the server{} block, then your custom  
mp3 location would apply; and you should see your extra configuration  
being used. (Without “root” set right, though, you may just end up with  
a 404.)

[Module ngx\_http\_core\_module](http://www.nginx.org/r/location) for the details on how nginx chooses  
which one location{} block will be used for the current request, if  
it’s unclear.

All the best,

## f

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

[Previous page](https://www.ruby-forum.com/t/custom-configuration/218312.md?page=1)
