Nginx vs Apache – Support for .htaccess and Rewrite Rules

apache-2.2migrationnginxweb-server

I've been working with Apache http servers for quite some time, and finally making the move to static-content servers alongside the others dynamic-content machines.

I was wondering, does nginx support ".htaccess" files, and things like mod_rewrite?

As I'm very used to the syntax, I was wondering what the (syntax) differences were, and what the learning curve is like moving from Apache configs to nginx.

Best Answer

nginx rewrite syntax is much cleaner then the counterpart in mod_rewrite:

mod_rewrite rule:

 RewriteRule ^[a-z0-9_-]*-f([0-9]+)/?(p([0-9]+)\.html)?$ /viewforum.php?f=$1&start=$3 [QSA,L,NC]

Becomes in nginx:

rewrite ^/[a-z0-9_-]*-f([0-9]+)/?(p([0-9]+)\.html)?$ /viewforum.php?f=$1&start=$3 last;

But no .htaccess support...

Edit: Another example how to support http://example.com/~username/ urls in nginx:

location ~ /~([a-zA-Z0-9]*)/(.*) {
    root /home/;
    autoindex on;
    index index.html;
    rewrite ^/~([a-zA-Z0-9]*)/(.*)$ /$1/www/$2 break;
}