Drupal clean URLs with nginx – the simple way
I know, it’s somehow old topics that i write these days – nginx is there since late 2004, but it’s the fact that all articles online doesn’t mention some important information about the installation and the configuration and some others tries to make it so complex plus the fact that the default configuration included with most dists. makes it confusing to understand it.
Simply – you must create a virtual host – new server { entry for drupal directory to get clean urls working !! – yes apache is so smart .
in the configuration file you can add the following block of lines and modify it to meet your env. and that’s damn fine:
server {
listen 80;
server_name Server2;
root /var/www/html/drpl;
index index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}}
Note: the server_name value must differ from the main virtual host name – and must be defined in /etc/hosts with different IP and it should look like this:
127.0.0.1 localhost.localdomain localhost
192.168.1.10 server2.mylan server2
finally don’t forget to restart nginx
.


another good way to get clean urls working with durpal without a server/virtual host entry is using this:
location /drupal {
if (!-e $request_filename) {
rewrite ^(/drpl)(.*)$ /drupal/index.php?q=$2 last;
# _____|_$1_|_$2____________________^
}
}
the rewrite line splits the URI to base and args using ()s – and PHP assigns $1 to the 1st () and $2 to the second () which is the args then sends the $2 to the index.php .