Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 15 down vote accepted

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 urls in nginx:

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

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

share|improve this answer
Essentially I was wondering about per-directory settings. I'll go R the FM now. =) – anonymous coward Jun 11 '09 at 17:20

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.

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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