0

I have the following rewrite rules for nginx.

The rules seem to not function correctly, for example the rule for games is supposed to rewrite http://thegamesdb.net/game/2/ to http://thegamesdb.net/index.php?tab=game?id=2 hover when I navigate to /game/2/ the browser is downloading a file called simply download.

My rewrite rules are as follows:

# nginx configuration
    location /game {
        rewrite ^/game/([0-9]+)(/?)$ /index.php?tab=game&id=$1 break;
        rewrite ^/game-edit/([0-9]+)(/?)$ /index.php?tab=game-edit&id=$1 break;
    }
    location /search {
        rewrite ^/search/([a-z0-9\-\ /\+]+)(/?)$ index.php?tab=listseries&string=$1&function=Search break;
        rewrite ^/search(/?)$ /index.php?tab=listseries&function=Search break;
    }
    location /browse {
     rewrite ^/browse/([0-9+]*)(/?)$ /index.php?tab=listplatform&stringPlatform=$1&function=Browse+By+Platform break;
    }
    location /platform {
        rewrite ^/platform/([0-9]+)(/?)$ /index.php?tab=platform&id=$1 break;
        rewrite ^/platform/([a-z0-9\-]+)(/?)$ /index.php?tab=platform&alias=$1 break;
        rewrite ^/platform-edit/([0-9]+)(/?)$ /index.php?tab=platform-edit&id=$1 break;
    }
    location /favorites {
        rewrite ^/favorites(/?)$ /index.php?tab=favorites break;
    }
    location /message {
    rewrite ^/message/([0-9]+)(/?)$ /index.php?tab=message&messageid=$1 break;
    }
    location /adminstats {
        rewrite ^/adminstats/([a-z0-9\-]+)(/?)$ /index.php?tab=adminstats&statstype=$1 break;
    }
    location /blog {
        rewrite ^/blog(/?)$ /blog/ break;
    }
    location /wiki {
        rewrite ^/wiki(/?)$ /wiki/ break;
    }
    location /forums {
        rewrite ^/forums(/?)$ /forums/ break;
    }
    location /phpmyadmin {
        rewrite ^/phpmyadmin(/?)$ /phpmyadmin/ break;
    }
    location / {
        rewrite ^/([a-z0-9\-\ /\+]+)(/?)$ index.php?tab=$1 break;
        if (!-e $request_filename){
         rewrite ^(.*)$ /index.php break;
        }
    }

I can't see what is wrong with the rules, am I missing something?

By the way, I'm using Ajenti-V and the rules above are enetered into the custom config panel of the Ajenti-V website.

Update:

It seems that my current re-write rules need to be routing through to php-fpm as they are currently being treated as just a static file.

How would I adapt the code above to accomplish this?

flexage
  • 113
  • 5

1 Answers1

1

You have no location block forwarding requests for php files to a processing backend.

Add a location block for php files and use nginx fastcgi_module into it, then convert rewrites to php files from break to last.

You can find alot of examples on server fault : search.

Update : root location block fix as asked in comments.

Your regex already matches / so /? will never match anything e.g. for /stats/. You have to setup two rewrite rules :

location / {

    rewrite "^/(.*)/$" /$1;
    rewrite "^/([- +a-z0-9/]+)$" /index.php?tab=$1 last;

    [ ... ]


}
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
  • Thanks Xavier, I checked the ajenti-v autogenerated nginx.config file, and I already had a location block for forwarding php files to php-fpm. Changing the `break;` to `last;` on all rewrites in my custom config includes did the trick. Thanks very much for your fast help! – flexage Oct 06 '14 at 20:48
  • Actually, I tell a lie, it worked for all rules apart from the very last one (catch all on root to the $tab parameter) can you spot anything wrong with that one? I simply get a page saying "page not found" when i attempt to go to /stats/ for example – flexage Oct 06 '14 at 20:51
  • @flexage That's because you missed a `/` before `index.php` in your first rewrite rule so the new URI doesn't contain a `/` and it's resolved as a physical path appendend to nginx's `$document_root`. By the way nginx never enters your if block. – Xavier Lucas Oct 06 '14 at 21:14
  • Thanks, that definitely helped! Final little issue though... if i go to `/stats` it rewites to `tab=stats` which is the deired behaviour, but if my urls have a `/` appended (`/stats/`) then it rewrites to `tab=stats/`. The final slash is not desired on the tab parameter itself, but hundreds of urls on the site have already been configured to have a traling slash... Is there any way to not have it pass the slash over to the `tab=$i`.... I'm really sorry, I'm new to nginx having been an apache2 user for many years. Your help is very much appreciated – flexage Oct 06 '14 at 21:32
  • @flexage Your capture group matches `/` so `/?` will never match the trailing slash . Check updated answer. – Xavier Lucas Oct 06 '14 at 23:25