Nginx infinite redirect loop

nginxreverse-proxyWordpress

Why is http://compassionpit.com/blog/ going through an infinite redirect loop? Here's my nginx conf file. The site is run by a nodejs server on port 8000 and Apache serves up the blog (wordpress) and the forum (phpBB). The forum is resolving just fine, at http://www.compassionpit.com/forum/

 server {
        listen          80;
        server_name     www.compassionpit.org;
        rewrite         ^/(.*) http://www.compassionpit.com/$1  permanent;
    }


    server {
        listen       80;                # your server's public IP address
        server_name  www.compassionpit.com;
        index        index.php index.html;

        location ~ ^/$ {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location @blogphp {
            internal;
            root /opt/blog/;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root/index.php;
            fastcgi_index  index.php;
            fastcgi_pass   127.0.0.1:8080;
        }

        location ~ ^/(forum|blog)/($|.*\.php) {
            root /opt/;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_index  index.php;
            fastcgi_pass   127.0.0.1:8080;
        }

        location ~ ^/(forum|blog) {
            root /opt/;
            try_files $uri $uri/ @blogphp;
        }

        location ~ ^/(forum|blog)/ {
           root /opt/;
        }


        location @backend {
            internal;
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ~ / {
            root /opt/chat/static/;
            try_files $uri $uri/ @backend;
        }

    }

Best Answer

I think you probably have Wordpress configured without the 'www' subdomain in it's URLs, and this is conflicting with the way you have Nginx configured (with server_name www.compassionpit.com).

So, when you visit compassionpit.com/blog/ Nginx redirects you to www.compassionpit.com/blog/, this request then hits Wordpress which redirects you back to the bare domain compassionpit.com/blog/.