I am trying to clean up my nginx rewrite rules for my vBulletin forum which has some modifications and additional software within the same site which causes problems. I have things working as they should, but according to nginx If is Evil I am concerned and would like to try and convert these few rules into try_files instead.

Currently, there is

  1. A rule for static images and files so they do not get passed to the seo mod (e.g. .gif,.ico, even .css)

  2. A rule for the subfolder mobiquo which is aka: tapatalk plugin. In order for that to work, I had to exclude the whole directory from rewrites.

  3. If the file does not exist. I'm not sure how important that is, but it seems like a good idea. Maybe it's to lower the work of the seo mod.

The nginx rewrite rules in the apparently risky If block form:

this is above the /forum/ block because I wanted to give it priority, if this is improperly done I would love to know.

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            # Some basic cache-control for static files to be sent to the browser
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";

    location /forum/ {

            try_files $uri $uri/ /forum/dbseo.php?$args;

            if ($request_uri ~* ^/forum/mobiquo) {

            if (-f $request_filename) {
            expires 30d;

            if ($request_filename ~ "\.php$" ) {
                    rewrite ^(/forum/.*)$ /forum/dbseo.php last;

            if (!-e $request_filename) {
                    rewrite ^/forum/(.*)$ /forum/dbseo.php last;



Somewhere in my searches I found a template which I tried to adapt, but since I'm not understanding regex's I failed :)

location / {

            # if you're just using wordpress and don't want extra rewrites
            # then replace the word @rewrites with /index.php

try_files $uri $uri/ /index.php;


location @rewrites {

            # Can put some of your own rewrite rules in here
            # for example rewrite ^/~(.*)/(.*)/? /users/$1/$2 last;
            # If nothing matches we'll just send it to /index.php

try_files $uri $uri/ /forum/dbseo.php?$args;

rewrite ^ /index.php last;

rewrite ^(/.php)$ /forum/dbseo.php last;


  • I guess your issue with the mod dbseo not the block itself!! – Digital site Jun 29 '14 at 16:49

Try to cleanse you question, especially the end of it shere you are shouting instead of providing code.

Based on the configuration you provided at the top of you question, I ended up with that:

location /forum/ {
    index dbseo.php; # You obviously wish to send everything erroneous/inexistent to dbseo.php, any index.php file would suffer the regex location below
    try_files $uri $uri/ /forum/dbseo.php?$args; # Any inexistent file/directory will be handled over to /forum/dbseo.php

    location ^~ /forum/dbseo.php { # Avoids matching the regex location below (performance)

    location ^~ /forum/mobiquo { # Avoids matching any other rules

    location ~* \.php$ {
        try_files /forum/dbseo.php =404;
        # Be careful here, try to secure your location since the regex can still be manipulated for arbitrary code execution

Nested locations are a good thing for isolation of potentially conflicting location blocks. Keep in mind that regex locations are evaluated seuqentially, thus to avoid the location blocks order having an impact (which is a mess just like Apache configuration), try to always enclose regex locations in prefix ones to avoid several of them following each other.

You can learn about location modifiers on its documentation page.

Maybe are there more subtelties, but you have all the basic information you need in my example. THe job is yours to understand/improve it to better suit your needs. :o)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.