WordPress and nginx infinite loop when using a reverse proxy

I have recently given my server setup a bit of an upgrade moving from an oldish version of lighttpd to nginx 1.6 compiled with the google pagespeed module. I wrote about the speed benefits in a seperate post.

When I was upgrading I had both servers setup side by side with nginx running on port 82 and setup a quick lighttpd reverse proxy from lighttpd to nginx so that I could keep all my sites up whilst experimenting with nginx config. When forwarding from lighttpd to nginx at a wordpress install it seemed to get itself into an infinite loop redirecting over and over to the homepage.  The problem could be tracked down to the redirect_canonical() function in wp-includes/canonical.php.

The check to see if we were on the canonical version of the url was failing and redirecting to itself.  I believe it was because the port in PHPs’ $_SERVER variable were different so the url did not match and WordPress would continue redirecting to the url forever. A quick fix was to remove the redirect_canonical filter with the following filter:

remove_filter('template_redirect', 'redirect_canonical');

(credits http://www.violato.net/blog/php/88-wordpress-did-infinite-301-redirect-loop)

The more permanent solution for me which may not be appropriate for everyone was to have all traffic go directly to nginx and remove the reverse proxy after I had finished testing other areas of the site. Using the filter above amy end up with you having multiple versions of the same page indexed by google if you have several versions of the same page setup for translations etc.

One thought on “WordPress and nginx infinite loop when using a reverse proxy”

Leave a Reply to Matheus Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>