If $request_uri , then


#1

How can I achieve the following?

I want to redirect all non-file requests to a script, like this:

      if ($request_uri !~* (js|css|images|etc)$) {
        rewrite ^/(.+)$ /index.php?q=$1 last;
        break;
      }

…but this does not work. I do not want to use “if (!-f
$request_filename)” to avoid disk overhead.

Can anyone make a suggestion?

Thanks!


#2

Joe A. <joe@…> writes:

…but this does not work. I do not want to use “if (!-f
$request_filename)” to avoid disk overhead.

Can anyone make a suggestion?

Thanks!

location / {
error_page 404 = @handler;
log_not_found off;
}

location @handler {
fastcgi_pass 127.0.0.1:1234;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param QUERY_STRING q=$request_uri&$query_string;
}


#3

Sorry, I did not make myself clear. I already have a fastcgi handler
section.

I think the problem is with the regular expression in " if
($request_uri !~* (js|css|images|etc)$) ":

  location / {
     root /var/www/domain.com/public;
     index  index.php;

     #  DO NOT WANT TO USE THIS METHOD (WHICH WORKS):
     #
     #  If the file exists as a static file serve it
     #  directly without running all
     #  the other rewite tests on it
     #
     # if (-f $request_filename) {
     #   break;
     # }
     # if (!-f $request_filename) {
     #   rewrite ^/(.+)$ /index.php?q=$1 last;
     #   break;
     # }

     #  WANT TO USE THIS METHOD INSTEAD (WHICH ISN'T WORKING):
     #
     # If request doesn't match js / css / images / etc
     # send request to fastcgi handler
     #
      if ($request_uri !~* (js|css|images|etc)$) {
        rewrite ^/(.+)$ /index.php?q=$1 last;
        break;
      }
  }

  location ~ \.php$ {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME

/var/www/domain.com/public$fastcgi_script_name;

     fastcgi_param  QUERY_STRING       $query_string;
     fastcgi_param  REQUEST_METHOD     $request_method;
     fastcgi_param  CONTENT_TYPE       $content_type;
     fastcgi_param  CONTENT_LENGTH     $content_length;

     fastcgi_param  PATH_INFO          $fastcgi_script_name;
     fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
     fastcgi_param  REQUEST_URI        $request_uri;
     fastcgi_param  DOCUMENT_URI       $document_uri;
     fastcgi_param  DOCUMENT_ROOT      $document_root;
     fastcgi_param  SERVER_PROTOCOL    $server_protocol;

     fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
     fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

     fastcgi_param  REMOTE_ADDR        $remote_addr;
     fastcgi_param  REMOTE_PORT        $remote_port;
     fastcgi_param  SERVER_ADDR        $server_addr;
     fastcgi_param  SERVER_PORT        $server_port;
     fastcgi_param  SERVER_NAME        $server_name;
  }

#4

Thanks - that didn’t work, but it at least pointed me in the right
direction (using location directives).

After 45 minutes of toiling with regular expressions, I’ve got the
following working (ish):

  location ~ ^/(js|css|images|etc) {
     root /var/www/domain.com/public;
     access_log off;
     expires max;
  }

The only problem is that within those folders I have scripts packaging
and compressing CSS and JavaScript i.e:

domain.com/js/compress.php?modules=this,and,that

So nginx pushes the SOURCE CODE of compress.php as a file to the
browser. Not good!

I’ll have to rewrite my CSS and JavaScript compression scripts to run
as cron jobs writing to cached files (which I was going to do one day
anyway)

Unless there’s an alternative?


#5
     #  DO NOT WANT TO USE THIS METHOD (WHICH WORKS):
     #
     #  If the file exists as a static file serve it
     #  directly without running all
     #  the other rewite tests on it
     #
     # if (-f $request_filename) {
     #   break;
     # }
     # if (!-f $request_filename) {
     #   rewrite ^/(.+)$ /index.php?q=$1 last;
     #   break;
     # }

I am curious about why you don’t want to use this method. What’s wrong
with it?

For your regexp problem you can force the accepted extensions too:

  location ~ ^/(js|css|images|etc)/(.+)\.(png|gif|css|js)$ {
     root /var/www/domain.com/public;
     access_log off;
     expires max;
  }

This way php files will not match the regexp and will not get served.


#6

On Saturday 08 November 2008, Joe A. wrote:

Sorry, I did not make myself clear. I already have a fastcgi handler
section.

I think the problem is with the regular expression in " if
($request_uri !~* (js|css|images|etc)$) ":

location / {
rewrite ^/(.+)$ /index.php?q=$1 last;
}

location ~ (js|css|images|etc)$ {

}

location ~ .php {

}


#7

Thanks for your suggestion Fernando.

Surely ‘if (!-f $request_filename)’ performs a stat check to see if the
file
exists - thereby increasing disk overhead?

Couldn’t the regexp be reversed to exclude matching ‘.php’?

Does this make sense:

location ~ ^/(js|css|images|etc)/(.+)(?<!.php)$ {
root /var/www/domain.com/public;
access_log off;
expires max;
}

(I know that’s almost certainly incorrect - but is that kind of regular
expression possible?)

Thanks again