February 8, 2009

Global Redirect module not working on Drupal and nginx server?

Filed under: Tips — Tags: , , , — Webopius @ 7:16 pm

nginx is a small, powerful and lightweight web server often used as an alternative to Apache Tomcat. It was written by Igor Sysoev and has been used on a number of heavily loaded Russian websites.

Drupal and nginx work well together (see the previous VPS, Drupal, nginx post here) but you might discover that a few modules cause problems because of .htaccess or redirects written for Apache that do not work as expected on nginx.

Drupal’s Global redirect module is one possible problem area when using nginx. You type in an ‘old’ format /node/3456 and the content appears as you’d expect. However, the URL remains in the ‘old’ format.

This can potentially cause duplicate content issues in search engines.

The fix

To get Global Redirect module and nginx working correctly together, find the file /sites/all/modules/globalredirect/globalredirect.module.

At around line 46, you’ll see the line:

if ($_SERVER['SCRIPT_NAME'] != $GLOBALS['base_path'] .'index.php') return FALSE;

With nginx, this check doesn’t match and global redirect returns FALSE. So just comment this line out and all should be well.

  • Popular Tags

  • Add to Technorati Favorites

    6 Comments »

    1. Hey, I was experiencing the same problem as you with the NGINX and Global Redirect Module. I also traced the problem to the $_SERVER['SCRIPT_NAME'] variable, but when I tried commenting it out, I got the Redirect Loop error. I found your post after this but I still can’t figure out what to do now. Any ideas? Thanks

      Comment by Leighton Whiting — May 3, 2009 @ 2:39 am

    2. Hi Leighton,
      If you contact me directly (info@webopius.com), I’d be happy to take a look at your site and nginx config to see what the problem might be. Initial thoughts are the site config or fastcgi configuration.

      I’m sure you have it, but remember to include this line in your nginx site configuration file:

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

      Also, this is mt fastcgi configuration file used by nginx:
      fastcgi_param GATEWAY_INTERFACE CGI/1.1;
      fastcgi_param SERVER_SOFTWARE nginx;
      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_param REQUEST_URI $request_uri;
      fastcgi_param DOCUMENT_URI $document_uri;
      fastcgi_param DOCUMENT_ROOT $document_root;
      fastcgi_param SERVER_PROTOCOL $server_protocol;
      fastcgi_param REMOTE_ADDR $remote_addr;
      fastcgi_param REMOTE_PORT $remote_port;
      fastcgi_param SERVER_ADDR $server_addr;
      fastcgi_param SERVER_PORT $server_port;
      fastcgi_param SERVER_NAME $server_name;

      Comment by Webopius — May 3, 2009 @ 10:09 am

    3. Hi,

      Same with me i can’t make global redirect works. im using nginx version: nginx/0.7.62 and global redirect 6.x-1.2..

      why its now working even if i have this config in nginx:
      if (!-e $request_filename) {
      rewrite ^/(.*)$ /index.php?q=$1 last;
      break;
      }

      Please help.

      Thank you.
      Burnz

      Comment by Burnz — November 20, 2009 @ 6:54 am

    4. Son of a b**** -

      I’ve been stumped for 6 hours now. I’ve dropped and re-dropped the system tables. I’ve been all over the house trying to work out what’s going on. And now it was this?

      For f*** sake

      At least it’s working now.

      Thank you

      Comment by Ronald — March 10, 2010 @ 2:18 am

    5. In one word… THANKS!
      I fought with this a lot. Turns out that I was doing everything without an if. After all – IfIsEvil (from the nginx wiki). I guess this is one time where IfIsNotThatEvil.

      Comment by Michael Lustfield — June 23, 2010 @ 2:36 pm

    6. I take that back… If is still not needed…

      location / {
      try_files $uri $uri/ /index.php?q=$request_uri;
      }

      Comment by Michael Lustfield — June 23, 2010 @ 2:44 pm

    RSS feed for comments on this post. TrackBack URL

    Leave a comment