I would like to enable clean urls in drupal 7 running under nginx server.

I have tried alot of suggestions from the internet, but none worked.

Here is my /etc/nginx/sites-available/default code

server {
listen   80;
server_name  default;
access_log  /var/log/nginx/localhost.access.log;
error_log /var/log/nginx/error.log;

root /usr/share/nginx/html;
index index.php;

 ## serve imagecache files directly or redirect to drupal if they do not exist.
location ~* files/styles {
  access_log off;
  expires 30d;
  try_files $uri @drupal;

## serve imagecache files directly or redirect to drupal if they do not exist.
location ~* ^.+.(xsl|xml)$ {
  access_log off;
  expires 1d;
  try_files $uri @drupal;

## Default location
location / {
    try_files $uri $uri/ @drupal;
    index index.php;

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

## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
  access_log        off;
  expires           30d;

## Parse all .php file in the /usr/share/nginx/html directory
location ~ .php$ {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(.*)$;

    # Gives 503 Bad Gateway
    #fastcgi_pass   backend;

    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    fastcgi_intercept_errors        on;
    fastcgi_ignore_client_abort     off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
    deny  all;

upstream backend {

I am running the above server on my local machine on Ubuntu 14.04

  • Have your tried the config here?
    – Felix Eve
    Apr 30, 2015 at 7:26
  • I read somewhere online that the url rewrites do not work for localhost and suggested to create a vhost. So i tested by creating a new vhost in nginx and installing drupal 7 again and enabling the clean urls. it worked without any problem.
    – Ibn Saeed
    Apr 30, 2015 at 8:23


Browse other questions tagged or ask your own question.