Rewrite issue

I’ve got an odd configuration issue I cannot solve, using this config:

server {
listen 80;
access_log logs/leadgen.access main;
error_log logs/leadgen.error;
root /opt/leadgen;
index index.php;

# If its referencing a resource, check other locations
location /res {
    rewrite ^/res/(.+)$

/code/script.CustomResource.php?File=$1 last;
}
location / {
rewrite ^/Blocked$
/code/Scripts/Blocked.php last;
}

location ~* \.php {
    include fastcgi;
}

}

Going to the following url: /res/xyz.gif properly gets rewritten to
/code/script.CustomResource.php?File=xyz.gif and the php processor
properly handles the php file.

But going to this url: /Blocked properly gets rewritten to
/code/Scripts/Blocked.php but the php processer never handles the php
file, nginx returns the .php source code.

Any idea what I’m doing wrong?

Thanks,

-Clint

Mon, Sep 29, 2008 at 10:35 AM, Clint P. [email protected]
wrote:

  • location /res {
  rewrite ^/res/(.+)$   /code/script.CustomResource.php?File=$1   last;
  • }
  • location / {
  rewrite ^/Blocked$     /code/Scripts/Blocked.php        last;
  • }

location ~* .php {
include fastcgi;
}

you’d need to nest the fastcgi location too inside of that block, if
it -has- to be in a location {} - but i don’t think you need those
location original blocks at all.

On Mon, Sep 29, 2008 at 12:35:23PM -0500, Clint P. wrote:

location /res {
}
}

Going to the following url: /res/xyz.gif properly gets rewritten to
/code/script.CustomResource.php?File=xyz.gif and the php processor
properly handles the php file.

But going to this url: /Blocked properly gets rewritten to
/code/Scripts/Blocked.php but the php processer never handles the php
file, nginx returns the .php source code.

It should work. Could you make debug log ?

Also this location

location / {
    rewrite ^/Blocked$      /code/Scripts/Blocked.php     last;
}

is better to divide to

location / {
}

location = /Blocked {
    rewrite ^     /code/Scripts/Blocked.php      last;
}

dunno. but for auth and other locations i’ve typically had to use the
nested approach.

either way i don’t see a need for any of those location blocks besides
the .php one in your config

I would agree with you, but one way works without the extra include... any idea why?

mike wrote:
 Mon, Sep 29, 2008 at 10:35 AM, Clint P. <[email protected]> 
wrote:
  • location /res {
      rewrite ^/res/(.+)$ 
/code/script.CustomResource.php?File=$1   last;
    
-   }
-   location / {
  
      rewrite ^/Blocked$     /code/Scripts/Blocked.php 
last;
    
-   }
  
  location ~* \.php {
      include fastcgi;
  }
    
you'd need to nest the fastcgi location too inside of that block, if
it -has- to be in a location {} - but i don't think you need those
location original blocks at all.
  

Igor S. skrev:

location = /Blocked {
    rewrite ^     /code/Scripts/Blocked.php      last;
}

What is the difference between these two blocks in practice? I can
imagine less regular expressions to handle, and this will cause and
increase in the performance on sites with a large amount of traffic. Is
this true, or is just best practices for nginx?

Okay, I've tried it with this config as well:
  rewrite ^/res/(.+)$ 
/code/script.CustomResource.php?File=$1   last;
  rewrite ^/Blocked$     /code/Scripts/Blocked.php        last;

location ~* .php {
include fastcgi;
}


Same result…





mike wrote:

dunno. but for auth and other locations i've typically 
had to use the
nested approach.

either way i don’t see a need for any of those location blocks besides
the .php one in your config

On Mon, Sep 29, 2008 at 11:19 AM, Clint P. <[email protected]>
wrote:

I would agree with you, but one way works without the 
extra include... any
idea why?

mike wrote:

Mon, Sep 29, 2008 at 10:35 AM, Clint P. <[email protected]>
wrote:

  • location /res {

    rewrite ^/res/(.+)$ /code/script.CustomResource.php?File=$1
    last;

  • }

  • location / {

    rewrite ^/Blocked$ /code/Scripts/Blocked.php last;

  • }

location ~* .php {
include fastcgi;
}

you’d need to nest the fastcgi location too inside of that block, if
it -has- to be in a location {} - but i don’t think you need those
location original blocks at all.

</pre>
  

On Tue, Sep 30, 2008 at 04:36:12PM +0200, P??l-Kristian Hamre wrote:

}

location = /Blocked {
    rewrite ^     /code/Scripts/Blocked.php      last;
}

What is the difference between these two blocks in practice? I can
imagine less regular expressions to handle, and this will cause and
increase in the performance on sites with a large amount of traffic. Is
this true, or is just best practices for nginx?

Yes, this is perfomance issue and therefore this is best practice.