Drupal Clean URL on Nginx

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.

source:http://drupal.org/node/976392

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s