Just started with Nginx but stuck in redirect loops

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
Anonymous's picture

hey,

just about to move from lighttpd to nginx. but for some reason i can't manage to get it to work. i used perusio config from github but always end up in redirect loops. i got the feeling that i need to learn a bit more about this config and tried a fresh drupal setup with a very minimal nginx conifg out perusio version. but even then i only manage to get redirect loops between / and install.php

can anyone help me to get a minimal working nginx.config for a fresh drupal setup without any fancy stuff?

my nginx.conf:

user nginx nginx;
worker_processes 1;

error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;

events {
   worker_connections 4096;
   multi_accept on;
   use epoll;
}

http {
   ## MIME types.
include /etc/nginx/mime.types;
default_type application/octet-stream;

    ## Default log and error files.
    access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

   server {

      listen 80; # IPv4
      server_name test.dev;

     access_log /var/log/nginx/dev.test.access.log;
     error_log /var/log/nginx/dev.test.error.log;

      root /srv/http/dev.test/;
      index index.php;

      location / {
           include /etc/nginx/fastcgi.conf;
           fastcgi_pass localhost:9000;
       }

     location ~* ^.+.(?:css|cur|js|jpe?g|gif|ico|png|html|xml)$ {
          access_log off;
            expires 30d;
           tcp_nodelay off;
           open_file_cache max=3000 inactive=120s;
            open_file_cache_valid 45s;
         open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }
  }
}

my fastcgi.conf:

### Generic fastcgi configuration.
fastcgi_param  QUERY_STRING       q=$uri&$args;
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  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  SCRIPT_FILENAME    $document_root/index.php;

Comments

got it running

sint's picture

finally i got it running with a different config, i've found on stackexchange:

http://drupal.stackexchange.com/questions/56462/is-there-a-common-nginx-...

nginx.conf reads like this:

server {
    server_name example.org;
    root /home/me/sites/example.org;

    index index.html index.htm index.php;

    access_log /var/log/nginx/example.org.access.log;
    error_log /var/log/nginx/example.org.error.log;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    # For drush
    location = /backup {
            deny all;
    }

    # Prevent user from accessing settings.php directly
    location ~ ^/sites/[^/]+/settings.php$ {
            deny all;
    }

    ## Replicate the Apache <FilesMatch> directive of Drupal standard
    ## .htaccess. Disable access to any code files. Return a 404 to curtail
    ## information disclosure. Hide also the text files.
    location ~* ^(?:.+.(?:htaccess|make|txt|log|engine|inc|info|install|module|profile|po|sh|.sql|theme|tpl(?:.php)?|xtmpl)|code-style.pl|/Entries.|/Repository|/Root|/Tag|/Template)$ {
            return 404;
    }

    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&$args;
            rewrite ^ /index.php last;
    }

    # Use an SSH tunnel to access those pages. They shouldn't be visible to
    # external peeping eyes.
    location = /install.php {
            allow 127.0.0.1;
            deny all;
    }

    location = /update.php {
            allow 127.0.0.1;
            deny all;
    }

    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:/var/run/php5-cgi/php5.sock;
    }

    ## Drupal 7 generated image handling, i.e., imagecache in core. See:
    ## https://drupal.org/node/371374
    location ~
/sites/./files/styles/ {
            access_log off;
            expires 30d;
            try_files $uri @rewrite;
    }

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

fastcgi_params:

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  SCRIPT_NAME        $fastcgi_script_name;
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  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

everything runs fine for now. the first thing i noticed is the difference in the query string and rewriting to the config from perusio. i wonder whats the big difference?

You should ask yourself the question why

perusio's picture

that config (the one on the Nginx wiki) is not recommended here. It was once here but it was removed because it has too many problems. It has been discussed here many times.

Your redirect loop probably came from having some module installed that provokes that.

On drupal 6, Global Redirect causes that problem.

Also the rewrite is unnecessary. It's just one additional processing step that serves no purpose.

thanks for your reply

sint's picture

i didn't know this config is the one from the nginx wiki as i found it on stackexchange without mentioning the wiki. but you're right its this config and i've seen some discussion about it here. there is no real reason using it except its the one running on my test system right now. it even has problems with the images created in 7.20 and later.

your config seems to be very good but also very advanced for a start. if you get stuck at some point and don't know how some stuff might affect parts of drupal. i had those loops with a default drupal 7 without any additional modules. just copied it into the folder for testing. i haven't tried any drupal 6.

i give your config another try. maybe i'll figure out what causing those loops.

Use the

perusio's picture

debug log if you hit the redirect loop again. Post your debug log on a pastie or on a Gist so that we can help you.

Nginx

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: