Community Documentation

Clean URLs with NGINX

Last updated June 6, 2011. Created by ghoti on November 19, 2010.
Log in to edit this page.

Most existing Drupal documentation, and even the built-in .htaccess files, assume you're using Apache as your web server, but site administrators who want better performance than Apache can provide have moved in droves to "smaller" web server applications like Lighttpd and Nginx.

Nginx provides a flexible configuration language and powerful rewrite commands.

From http://wiki.nginx.org/Drupal, the recommended nginx configuration for Drupal looks like this:

  server {
    server_name example.com;
    root /path/to/drupal; ## <-- Your only path reference.
 
    location = /favicon.ico {
      log_not_found off;
      access_log off;
    }

    location = /robots.txt {
      allow all;
      log_not_found off;
      access_log off;
    }

    # This matters if you use drush
    location = /backup {
      deny all;
    }

    # Very rarely should these ever be accessed outside of your LAN
    location ~* \.(txt|log)$ {
      allow 10.0.0.0/8;
      allow 172.16.0.0/12;
      allow 192.168.0.0/16;
      deny all;
    }

    location ~ \..*/.*\.php$ {
      return 403;
    }

    location / {
      # This is cool because no php is touched for static content
       try_files $uri @rewrite;
    }

    location @rewrite {
      # Some modules enforce no slash (/) at the end of the URL
      # Else this rewrite block wouldn't be needed (GlobalRedirect)
       rewrite ^/(.*)$ /index.php?q=$1;
    }

    location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
      fastcgi_pass unix:/tmp/phpcgi.socket;
    }

    # Fighting with ImageCache? This little gem is amazing.
    location ~ ^/sites/.*/files/imagecache/ {
      try_files $uri @rewrite;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
      expires max;
      log_not_found off;
    }
  }

This replaces the now-deprecated previous recommendation on this page, which was shorter but less functional. The idea is to have nginx assume that anything that isn't a reference to a specific file should be handled by Drupal via the rewrite rule:

 server {
     listen       80;
     server_name  example.org;

     location / {
         root   /path/to/drupal;
         index  index.php;
         error_page 404 = @drupal;
     }

     location @drupal {
         rewrite ^(.*)$ /index.php?q=$1 last;
 ...
     }
 }

Another debrecated alternative is to add the following to your server configuration block:

if (!-e $request_filename) {
    rewrite ^/(.*)$ /index.php?q=$1 last;
}

This may have mixed results. If you experience problems with either of these last two methods, please try the first one.

Comments

do not work , please help

my setting :

server {
listen 8077;
server_name localhost;

location / {
root E:\www-test\mywwwroot\drupal-7.0;
index index.php index.html index.htm;
error_page 404 = @drupal;
}

location @drupal {
rewrite ^(.*)$ /index.php?q=$1 last;

}

location ~ \.php$ {
root E:\www-test\mywwwroot\drupal-7.0;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME E:\www-test\mywwwroot\drupal-7.0$fastcgi_script_name;
include fastcgi_params;
}
}

Page status

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x
Audience
Site administrators
Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here