Nginx – From Apache to Nginx: Error 310, too many redirects

apache-2.2nginxredirectWordpress

I'm moving a WordPress blog from a Apache based webserver to a Nginx based webserver. Easy steps, I thought:

  • scp of the source WordPress folder,
    in this example,
    /var/www/example.com, which is
    transferred to a folder with the same
    name on the remote webserver. No
    problem with that.

  • mysqldump of the source WordPress
    database. No problem with that.

  • On the nginx webserver, I create a
    database (with the same name as the
    source one, just to keep everything
    equal).

  • I setup a default WP blog to see if
    everything is fine at this point. I
    create a proper
    /etc/nginx/sites-available/example.com
    config file (and link to
    sites-enabled, of course). That
    config file points to a diferent
    folder (/var/www/exampletemp)

  • Modify my Windows hosts file in order to test the new blog on the domain I'm interested in. The blog at example.com shows fine, the default WP blog of course, but with the right URLs.

  • And then the crucial steps. First I
    edit
    /etc/nginx/sites-available/example.com
    so it points now to the real backup
    folder (/var/www/example.com) ->
    (just change root locations and the
    fastcgi directive).

  • Then I create another database,
    restore the source database on this
    new one, edit wp-config.php file on
    the /var/www/example.com just in case
    there's something missing, and
    restart nginx.

What do I get?

A gorgeous, beautiful "ERROR 310: Too many redirects" 🙁

I can see the login page on www.example.com/wp-login.php, but my source users don't work and I can't get no access to the WordPress Dashboard. I've modified for example an admin account from phpMyadmin, setting up a new password and selecting the MD5 encryption, and although that has worked when I moved from Apache to Apache or Nginx to Nginx, this time it hasn't.

I really don't get why there is that Too many redirects error. I have try to disable all plugins (renaming the /wp-content/plugins to /wp-content/oldplugins, or disabling all of them with a SQL query) but no luck again.

Sorry for the long question. I hope I've made myself clear…

Edit: This is the nginx config file, just in case 🙂

server {

            listen   80;
            server_name  www.example.com;
            rewrite ^/(.*) http://example.com/$1 permanent;

           }


server {
       listen   80;
       access_log  /var/www/example.com/log/access.log;
       error_log      /var/www/example.com/log/error.log info;
       server_name     example.com;
       root /var/www/example.com;

       location / {
          index index.php;
          # if the requested file exists, return it immediately
          if (-f $request_filename) {
                  break;
          }

        # all other requests go to WordPress
        if (!-e $request_filename) {
           rewrite . /index.php last;
           }
      }

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


## Parse all .php file in the /var/www directory
    location ~ .php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  HTTP_HOST        example.com;
        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     on;
        fastcgi_read_timeout 180;

    }


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

I'm thinking in another answer to my question: The source WP folder on the Apache webserver has apache:apache as user:group, and after 'scping' that folder to my new Nginx webserver, that user and that group is no longer valid. I made the proper chown -R www-data:www-data to the new WP folder, but… could that be a hint to the answer?

Best Answer

Try:

location / {
    try_files $uri $uri/ /index.php;
}