Forum: NGINX if $request_uri , then ...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
02a35d31be48f389d76abfbb8b39191f?d=identicon&s=25 Joe Aston (Guest)
on 2008-11-08 02:34
(Received via mailing list)
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!
E290e8dc6bd615bbce6c5ec47c7a436d?d=identicon&s=25 cynix (Guest)
on 2008-11-08 07:47
(Received via mailing list)
Joe Aston <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;
}
02a35d31be48f389d76abfbb8b39191f?d=identicon&s=25 Joe Aston (Guest)
on 2008-11-08 13:12
(Received via mailing list)
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;
      }
B06eeb3c664683b78f1bd62a80cd8ca4?d=identicon&s=25 Roxis (Guest)
on 2008-11-08 13:45
(Received via mailing list)
On Saturday 08 November 2008, Joe Aston 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 {
    ...
}
02a35d31be48f389d76abfbb8b39191f?d=identicon&s=25 Joe Aston (Guest)
on 2008-11-08 14:39
(Received via mailing list)
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?
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2008-11-11 11:35
>          #  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.
02a35d31be48f389d76abfbb8b39191f?d=identicon&s=25 Joe Aston (Guest)
on 2008-11-11 13:08
(Received via mailing list)
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
This topic is locked and can not be replied to.