Rewrite before regex location

Joyce Babu joyce at joycebabu.com
Fri May 6 20:35:54 UTC 2016


>
> That's because your fastcgi_split_path_info pattern does not match -
> .php is not followed by / in your rewritten url.
>
> Because of the location{} you are in, it is probably simplest to just
> replace the second capture part of that pattern with (.*)$.
>

Thank you Francis. It worked after following your suggestions.

But I have found a different approach, that is working for all the cases
that I tested. As per your suggestions, I have removed the if statement
with try_files and have used the ~^ modifier to ensure that the location
blocks get higher precedence than the regex block.




   1. index index.php;
   2.
   3. location ~ /\.ht {
   4.     deny all;
   5. }
   6.
   7. # .php catchall regex block
   8. location ~ [^/]\.php$ {
   9.     # Handle .php uris, except when they are overridden below
   10.     include inc/php-fpm.conf;
   11. }
   12.
   13. location / {
   14.     if ($request_uri ~ "^[^?]*?//") {
   15.         # Remove double slashes from url
   16.         rewrite "^" $scheme://$host$uri permanent;
   17.     }
   18.
   19.     # Handle non .php files and directory indexes
   20.     try_files $uri $uri/index.php;
   21. }
   22.
   23. location  /generated/imgs/ {
   24.     # Serve file if it exists, else use rewriting (for dynamically
   generated and cached files)
   25.     # This ensures that rewrite rules are not applied to existing
   files
   26.     try_files $uri @rewrite_generated_imgs;
   27. }
   28.
   29. location  @rewrite_generated_imgs {
   30.     # Since extension is changed from .jpg to .php, use last.
   31.     # This will pass the rewritten uri to the .php regex block for
   execution
   32.
   33.     rewrite "^/generated/imgs/([0-9]+)/(.*).jpg$"
    /generated/imgs/pic.php?width=$1&height=$1&pic=$2&mode=proportionate
   last;
   34.     rewrite "^/generated/imgs/([0-9]+)x([0-9]+)/(.*).jpg$"
    /generated/imgs/pic.php?width=$1&height=$2&pic=$3&mode=exact last;
   35.
   36.     try_files $uri $uri/index.php;
   37. }
   38.
   39. # The ^~ modifier ensures that .php uris are not caught by the .php
   catchall regex block
   40. location ^~ /banking/ {
   41.     rewrite "^/banking/sitemap-(\d+).xml.gz$"
    /banking/sitemap-generator.php?id=$1 last;
   42.
   43.     try_files $uri $uri/index.php;
   44.
   45.     location ~ [^/]\.php$ {
   46.         rewrite "^/banking/([A-Za-z0-9-]+)/branches.php$"
    /banking/branches.php?name=$1 break;
   47.         rewrite "^/banking/([A-Za-z0-9-]+)/atm.php$"
    /banking/atm.php?name=$1 break;
   48.
   49.         include inc/php-fpm.conf;
   50.     }
   51. }
   52.
   53. location  ^~ /news/ {
   54.     # Check for file before applying rewrite rules. If not, rewrite
   55.     # This ensures that rewrite rules are not applied to existing
   files
   56.     try_files $uri $uri/index.php @rewrite_news;
   57. }
   58.
   59. location @rewrite_news {
   60.     # Rewrite non PHP urls in outer location block.
   61.     # Use last so that it will be passed to the .php regex block
   after rewrite.
   62.     # This ensures that the php code is executed and not downloaded
   as is
   63.
   64.     rewrite "^/news/articles/?$" /news/ permanent;
   65.     rewrite "^/news/articles/a([0-9]+)\.html$" /news/article.php?id=
   $1&mode=article last;
   66.     rewrite "^/news/page-([0-9]+)\.html$" /news/index.php?cat=
   home&page=$1 last;
   67.     rewrite "^/news/([a-z-]+)/rss.xml$" /news/rss/$1.xml break;
   68.
   69.     try_files $uri $uri/index.php;
   70.
   71.     location ~ [^/]\.php$ {
   72.         # Rewrite PHP urls
   73.         # Use break so that the rewrite rules are not applied again
   74.
   75.         rewrite "^/news/print.php$" /news/article.php?mode=print
   break;
   76.
   77.         include inc/php-fpm.conf;
   78.     }
   79. }
   80.
   81. ================
   82. # inc/php-fpm.conf
   83.
   84. # /cms/cms.php is the fallback script that will handle all missing
   uris
   85. try_files $uri /cms/cms.php =404;
   86.
   87. fastcgi_pass 127.0.0.1:9000;
   88. fastcgi_index index.php;
   89. include fastcgi_params;



Does this look fine? Is it a bad idea to use named locations like this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20160507/5113ebe5/attachment.html>


More information about the nginx mailing list