Ruby Forum NGINX > Need help with converting this htaccess to nginx rewrite

Posted by Brian Zerangue (bzerangue)
on 13.02.2009 22:56
<p>I'm really interested in utilizing Nginx on my Slicehost account, but
my main holdout is understanding how to adjust <code>.htaccess</code>
files so that my site can function properly on Nginx.</p>

<p>I've made an attempt, but I'm just not a regular expressions guy and
am new to this part altogether. Unfortuantely, I'm having a difficult
time moving over to Nginx rewrite rules. I'll post my attempt at the
Nginx rewrite rules, but first I'm going to post the mod_rewrite rules
of my <a href="http://symphony21.com">Symphony CMS</a> site, so we have
the direct comparison. If anyone knows Nginx... your help is much
appreciated!!!</p>

<p>Here's the regular mod_rewrite for the index.php in document root
folder for Symphony...</p>

<h3>Original Symphony 2 <code>.htaccess</code></h3>

<pre><code>### DO NOT APPLY RULES WHEN REQUESTING "favicon.ico"
RewriteCond %{REQUEST_FILENAME} favicon.ico [NC]
RewriteRule .* - [S=14]

### IMAGE RULES
RewriteRule
^image\/3\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-f0-9]{3,6})(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8 [L]
RewriteRule
^image\/2\/([0-9]+)\/([0-9]+)\/([1-9])(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7 [L]
RewriteRule
^image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6 [L]
RewriteRule ^image(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=0:0:0:0:0:$2:$3.$4 [L]

### CHECK FOR TRAILING SLASH - Will ignore files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]

### MAIN REWRITE - This will ignore directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/$ /index.php?page=$1&amp;%{QUERY_STRING} [L]
</code></pre>

<h3>My Nginx attempt</h3>

<p>I don't think this is right, but would love to get some input.</p>

<pre><code>location / {
    if ($request_filename="favicon.ico") {
        rewrite  ^(.*)$  break;
    }

    ### IMAGE RULES
    rewrite
^image\/3\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-f0-9]{3,6})(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8 last;
    rewrite
^image\/2\/([0-9]+)\/([0-9]+)\/([1-9])(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7 last;
    rewrite
^image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6 last;
    rewrite ^image(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=0:0:0:0:0:$2:$3.$4 last;

    ### CHECK FOR TRAILING SLASH - Will ignore files
    if (!-f $request_filename) {
        rewrite ^/(.+)$ /$1/ permanent;
    }

    ### MAIN REWRITE - This will ignore directories
    if (!-d $request_filename) {
        rewrite ^/(.+)$ /index.php?url=$1 last;
    }
}
</code></pre>
Posted by Brian Zerangue (bzerangue)
on 13.02.2009 22:59
Sorry, I didn't realize that this was plain-text only... forgive me for 
the HTML in the last post. Here's my post in plain-text.


I'm really interested in utilizing Nginx on my Slicehost account, but my 
main holdout is understanding how to adjust .htaccess files so that my 
site can function properly on Nginx.

I've made an attempt, but I'm just not a regular expressions guy and am 
new to this part altogether. Unfortuantely, I'm having a difficult time 
moving over to Nginx rewrite rules. I'll post my attempt at the Nginx 
rewrite rules, but first I'm going to post the mod_rewrite rules of my 
Symphony CMS site, so we have the direct comparison. If anyone knows 
Nginx... your help is much appreciated!!!

Here's the regular mod_rewrite for the index.php in document root folder 
for Symphony...

Original Symphony 2 .htaccess

### DO NOT APPLY RULES WHEN REQUESTING "favicon.ico"
RewriteCond %{REQUEST_FILENAME} favicon.ico [NC]
RewriteRule .* - [S=14]

### IMAGE RULES
RewriteRule 
^image\/3\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-f0-9]{3,6})(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8 [L]
RewriteRule 
^image\/2\/([0-9]+)\/([0-9]+)\/([1-9])(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7 [L]
RewriteRule 
^image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6 [L]
RewriteRule ^image(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=0:0:0:0:0:$2:$3.$4 [L]

### CHECK FOR TRAILING SLASH - Will ignore files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]

### MAIN REWRITE - This will ignore directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/$ /index.php?page=$1&%{QUERY_STRING} [L]
My Nginx attempt

I don't think this is right, but would love to get some input.

location / {
    if ($request_filename="favicon.ico") {
        rewrite  ^(.*)$  break;
    }

    ### IMAGE RULES
    rewrite 
^image\/3\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-f0-9]{3,6})(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8 last;
    rewrite 
^image\/2\/([0-9]+)\/([0-9]+)\/([1-9])(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7 last;
    rewrite 
^image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6 last;
    rewrite ^image(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
/symphony/image.php?param=0:0:0:0:0:$2:$3.$4 last;

    ### CHECK FOR TRAILING SLASH - Will ignore files
    if (!-f $request_filename) {
        rewrite ^/(.+)$ /$1/ permanent;
    }

    ### MAIN REWRITE - This will ignore directories
    if (!-d $request_filename) {
        rewrite ^/(.+)$ /index.php?url=$1 last;
    }
}
Posted by Jim Ohlstein (Guest)
on 13.02.2009 23:52
(Received via mailing list)
I think you're pretty close.

Try

location / {
    if ($request_filename ~ "favicon\.ico$") {
        rewrite  ^(.*)$  last;
    }

    ### IMAGE RULES
    rewrite 
^/image\/3\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-f0-9]{3,6})(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8 last;
    rewrite 
^/image\/2\/([0-9]+)\/([0-9]+)\/([1-9])(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7 last;
    rewrite 
^/image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6 last;
    rewrite ^/image(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$
/symphony/image.php?param=0:0:0:0:0:$2:$3.$4 last;

    ### CHECK FOR TRAILING SLASH - Will ignore files
    if (!-f $request_filename) {
        rewrite ^/(.+)$ /$1/ permanent;
    }

    ### MAIN REWRITE - This will ignore directories
    if (!-d $request_filename) {
        rewrite ^/(.+)$ /index.php?url=$1 last;
    }
}

I'm not sure this will work but clearly you are missing some forward 
slashes in the image rules. Good luck!

Jim
Posted by Igor Sysoev (Guest)
on 14.02.2009 13:00
(Received via mailing list)
On Fri, Feb 13, 2009 at 10:59:23PM +0100, Brian Zerangue wrote:

> moving over to Nginx rewrite rules. I'll post my attempt at the Nginx 
> RewriteCond %{REQUEST_FILENAME} favicon.ico [NC]
> ^image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> ### MAIN REWRITE - This will ignore directories
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)\/$ /index.php?page=$1&%{QUERY_STRING} [L]

 server {

     root ...

     location = /favicon.ico {
     }

     location / {
         if (!-d $request_filename) {
             rewrite ^/(.+)$ /index.php?url=$1 last;
     }

     location ^~ /image/2/ {
         rewrite 
^/image/2/(\d+)/(\d+)/(\d)(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$
                  /symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7;
     }

     location ^~ /image/3/ {
         rewrite
       "^/image/3/(\d+)/(\d+)/(\d)/(\w{3,6})(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$"
            /symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8;
     }

     location ~ ^/image/ {

         rewrite 
^/image/1/(\d+)\/(\d+)(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$
                  /symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6
                  last;

         rewrite  ^/image(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$
                  /symphony/image.php?param=0:0:0:0:0:$2:$3.$4;
     }

     location ~ \.php$ {
         proxy_pass  ...
     }
}

However, I do not understand what this part is intended for:

> ### CHECK FOR TRAILING SLASH - Will ignore files
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_URI} !/$
> RewriteCond %{REQUEST_URI} !(.*)/$
> RewriteRule ^(.*)$ /$1/ [L,R=301]
> 
> ### MAIN REWRITE - This will ignore directories
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)\/$ /index.php?page=$1&%{QUERY_STRING} [L]

Probably this may be served something else better than ugly:

     location / {
         if (!-d $request_filename) {
             rewrite ^/(.+)$ /index.php?url=$1 last;
     }
Posted by Brian Zerangue (bzerangue)
on 14.02.2009 19:57
Jim and Igor -

Thank you both for your help and input. Unfortunately, I need to spend 
more time in a local environment trying to learn how to get this up and 
running.

I kept getting Server Errors (500) when even running phpinfo(); just to 
test php. I couldn't get it running ever.

NGINX is really impressive. It would be nice to have a "NGINX for 
dummies" tutorial that goes step by step, even showing the basic setup 
to get PHP up and running properly. I followed the "Perfect setup 
tutorial", 
http://www.mensk.com/webmaster-toolbox/perfect-ubuntu-hardy-nginx-mysql5-php5-wordpress/. 
As well as some in the English wiki. Everyone, I kept getting 500 
errors. Or I'd get "no valid input" notification.

Again, I'm sure it's something I'm doing. But if y'all know of the 
simplest "NGINX for dummies" tutorial to get PHP working and basic 
virtual host setup for PHP. That would be helpful.

Again, thank you both for your quick replies. It was very helpful!

BZ
Posted by Jim Ohlstein (Guest)
on 14.02.2009 20:40
(Received via mailing list)
I'd be willing to host a forum (English and Russian if desired) and 
provide a vBulletin license and articles mod if there was enough 
interest and willingness to participate.

Jim
Posted by Brian Zerangue (bzerangue)
on 15.02.2009 07:22
Jim Ohlstein wrote:
> I'd be willing to host a forum (English and Russian if desired) and 
> provide a vBulletin license and articles mod if there was enough 
> interest and willingness to participate.
> 
> Jim

I'd be interested in that. Thanks Jim for replying and for your help. 
Also, Igor, thank you for your help!

BZ
Posted by Tobias lott (Guest)
on 15.02.2009 18:10
(Received via mailing list)
On Sun, 15 Feb 2009 07:22:07 +0100
Brian Zerangue <lists@ruby-forum.com> wrote:

> BZ
Isnt that the same what the wiki (http://wiki.codemongers.com/Main) is
for?
Posted by Jim Ohlstein (Guest)
on 15.02.2009 19:29
(Received via mailing list)
No. A wiki is a wiki and a forum is a forum. They each have a place and 
a
purpose.

See http://en.wikipedia.org/wiki/Wiki and
http://en.wikipedia.org/wiki/Internet_forum.

Like I said, I'm more than happy to donate the space, bandwidth, and
vBulletin license if there's enough interest.

People can contact me privately if they prefer.

Jim