Custom configuration

Francis Daly francis at
Fri Mar 30 18:14:35 UTC 2012

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/;
> > 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;
> >         }

> > location @apache {
> >         proxy_pass;
> >         }

> > include "/etc/cpnginx/custom/";
> > }

> > root at shadow [/usr/local/nginx/conf]# cat /etc/cpnginx/custom/

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 (

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 (

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.) 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,

Francis Daly        francis at

More information about the nginx mailing list