Community Documentation

Clean URLs with NGINX

Last updated November 21, 2012. Created by ghoti on November 19, 2010.
Edited by mitchell. Log in to edit this page.

Nginx provides powerful rewrite commands in the servers' configuration, so it is flexible in its support for Clean URLs. The recommended settings from the Nginx Drupal Documentation are:

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;
     }
}

The key change is with the 404 line under location / and the location @drupal configuration section. This works by assuming that anything that isn't a reference to a specific file should be handled by Drupal via the rewrite rule.

If you are looking for more information about installing Nginx, see Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 12.04 LTS.

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;
}
}

I had to tweak the code a lil

I had to tweak the code a lil to get it working on my ec2. Replaced the above php location directive with the following

location ~ \.php$
        {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
        }

Works using try_files

I got it to work by adding

location / {
try_files $uri $uri/ /index.php;
}

Taken from here: http://blog.martinfjordvald.com/2010/07/nginx-primer/

My setup:

server {
        listen       80;
        server_name  localhost;

        root   /path/to/drupal;
        index  index.php;

       location / {
            try_files   $uri $uri/ /index.php;
        }

        location ~ \.php$ {
            fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index  index.php;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME /path/to/drupal$fastcgi_script_name;
            fastcgi_intercept_errors on;
        }
}

Page status

Needs copy/style review

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x
Audience
Site administrators
Drupal’s online documentation is © 2000-2013 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. Comments on documentation pages are used to improve content and then deleted.