Conflicting location blocks

I have this config -

The location block to set expires headers breaks the alias location.
Is there a way to get them to both work?

Everything that matches should issue an expires header.

However, I want to alias /icommon to another path. According to the
wiki this is the right syntax, but once again I am stuck with location
confusion :slight_smile:

server {
listen 80;
server_name test.com;
index index.php;
root /home/test/web/test.com/rfdev;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
expires max;
}
location /icommon/ {
alias
/home/test/web/test.com/rfdev/core/common/;
}
}

I have to comment out the expires block for now, or it won’t work. Is
this a bug? How else can I do an alias? I tried setting “root” for
that location, and it “sorta” works, the problem is is it tries to
append the location, which doesn’t work for me, unless I rename the
file paths and set the “root” to the directory above it…

            location /icommon/ {
                    root /home/test/web/test.com/rfdev/core/common/;
            }

Maps to this then /home/test/web/test.com/rfdev/core/common/icommon/;

Would have to set root to /home/test/web/test.com/rfdev/core/, and
then rename “icommon” to “common” on the filesystem…

I think there might be a slight bug in how “alias” handling is done?
Shouldn’t it basically be like redefining a “root” ?

Thanks

Hello!

On Mon, Sep 15, 2008 at 05:00:04PM -0700, mike wrote:

server {
listen 80;
server_name test.com;
index index.php;
root /home/test/web/test.com/rfdev;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {

Unrelated comment: there is no sense in leading ‘^.+’.

  •            location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    
  •            location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    
                   expires max;
           }
           location /icommon/ {
                   alias /home/test/web/test.com/rfdev/core/common/;
           }

}

I have to comment out the expires block for now, or it won’t work. Is
this a bug?

You misunderstood how locations work. Only one location will be
used for particular request. Regex locations isn’t an exception -
if they match, only their configuration is used. So with your
config any match on regex will leave you with configuration
without alias defined.

See http://wiki.codemongers.com/NginxHttpCoreModule#location for
details on location matching.

Would have to set root to /home/test/web/test.com/rfdev/core/, and
then rename “icommon” to “common” on the filesystem…

The root directive defines filesystem path for ‘/’ request (i.e.
root). All requests are mapped to filesystem by appending request
uri to root, i.e. /icommon/blah → /icommon/blah. It
doesn’t matter where root was defined.

I think there might be a slight bug in how “alias” handling is done?
Shouldn’t it basically be like redefining a “root” ?

These beasts are completely different. While root defines fs path
for ‘/’, the alias defines fs path for the location itself, i.e:

 location /blah/ {
     alias  /path/to/files/;
 }

will map ‘/blah/xxx’ to ‘/path/to/files/xxx’. Note that
‘/blah/’ (the location itself) was stripped from uri before it was
appended to path.

With root,

 location /blah/ {
     root  /path/to/files/;
 }

will map the same request (‘/blah/xxx’) to ‘/path/to/files/blah/xxx’.

The only problem with alias is that it can’t be used in regex
locations (well, not really problem - it doesn’t make sense in
regex locations), and thus your configuration above can’t be easily
converted to correct one be defining one more regex location for
images in /icommon/. Workaround is to use ‘root’ and ‘rewrite …
break’ instead of alias, e.g.:

 location /icommon/ {
     alias ...;
 }
 location ~* ^/icommon/.*\.(jpg|...)$ {
     rewrite ^/icommon/(.*)  /common/$1  break;
     expires ...;
 }
 location ~* \.*(jpg|...)$ {
     expires ...;
 }

(alternatively, you may use expiremental nested locations, but
there are issues with alias)

But generally better aproach is to restructure site so you may
just add expires to normal location and doesn’t bother with regex
locations at all.

Maxim D.

Actually I think I’ve got this working, although I will take your
regexp for the expires block into account.

    server {
            listen 80;
            server_name test.com;
            index index.php;
            root /home/test/web/test.com/rfdev;
            rewrite /icommon/(.*) /common/$1 last;
            location /common {
                    root /home/test/web/test.com/rfdev/core;
                    location ~ \.php$ {
                            fastcgi_pass 127.0.0.1:11004;
                    }
                    location ~* 

^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
expires max;
}
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:11004;
}
}

Hello!

On Tue, Sep 16, 2008 at 01:18:18AM -0700, mike wrote:

Actually I think I’ve got this working, although I will take your
regexp for the expires block into account.

   server {
           listen 80;
           server_name test.com;
           index index.php;
           root /home/test/web/test.com/rfdev;
           rewrite /icommon/(.*) /common/$1 last;

Rewrite at server level will be executed against all requests, so
it’s not recommended. It’s better to use something like

              location /icommon/ {
                      rewrite ^/icommon/(.*)  /common/$1  last;
              }

Maxim D.

On Tue, Sep 16, 2008 at 01:18:18AM -0700, mike wrote:

                    root /home/test/web/test.com/rfdev/core;
    }

Why not use simple:

             # static only
             location /icommon/ {
                     alias 

/home/test/web/test.com/rfdev/core/common/;
expires max;
index index.php;
}

             location ~ \.php$ {
                     fastcgi_pass 127.0.0.1:11004;
             }

On Tue, Sep 16, 2008 at 12:37:12PM +0400, Maxim D. wrote:

          index index.php;
          root /home/test/web/test.com/rfdev;
          rewrite /icommon/(.*) /common/$1 last;

Rewrite at server level will be executed against all requests, so
it’s not recommended. It’s better to use something like

             location /icommon/ {
  •              location /icommon/ {
    
  •              location ^~ /icommon/ {

On Tue, Sep 16, 2008 at 1:36 AM, Igor S. [email protected] wrote:

             location ^~ /icommon/ {
                     rewrite ^/icommon/(.*)  /common/$1  last;
             }

Thanks both of you guys. I wound up doing that rewrite and was able to
get it to work, but I will take these pointers with me. My problem
was:

  1. I couldn’t get alias + php to work.
  2. I couldn’t set the root (so I could do a nested fastcgi block if
    needed) to work since the directory name was “common” and the request
    was “icommon”

For our apps the icommon folder was temporary. We’ll be switching it
out so it isn’t needed soon. But I was trying to get this to work
properly anyway…

Hopefully all this thrash will be done soon enough :slight_smile: