16

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.

anonymous coward
  • 615
  • 2
  • 8
  • 15

4 Answers4

14

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;
}
rkthkr
  • 8,503
  • 26
  • 38
4

It would appear that nginx does have a mod_rewrite equivelent and documentation can be found here. I've not used nginx myself, but the documentation looks like the configuration is totally different, but it shouldn't be too hard to understand what it's doing. The wiki appears to have plenty of examples.

I can't answer if you can have per directory configuration files like you can with apache. It wouldn't surprise me if you could, because people have come to expect that from using apache.

David Pashley
  • 23,151
  • 2
  • 41
  • 71
4

nginx does provide rewrite functionality, check NginxHttpRewriteModule

What do you want to do with .htaccess? You can setup nginx for Basic Authentication with NginxHttpAuthBasicModule but AFAIK configuration directives can only be stored in nginx.conf

zarkdav
  • 470
  • 2
  • 4
0

NGINX DOES support having configurations in many files through the 'include' directive. Basically, it loads a sub-configuration and put it in place. It also supports joker characters, so it's easy to load many of them in one shot.

The only limitation (in my opinion) is that you need to reload when the configuration is changed. So any user who has a piece of configuration might need rights to reload nginx config:

/etc/ini.d/nginx force-reload (on centos)

I don't really know if there is a way to avoid that or to do it differently because I use NGINX on many servers with lots of apps(virtual hosts) which are my own. It would mean that NGINX is not ideal on shared hosting.

Nevertheless I really love NGINX because configuration is just so much more legible than a nasty XML file. Question of taste I guess. The other good point is obviously speed, if it's important in your case.

Good luck mig