Nginx – WordPress Redirect Loop

I’m using nginx as a frontend for apache2. Apache2 will handle all dynamic content.

This is my main file with nginx being a frontend for apache and having all dynamic pages forwarded to apache2:

server {
    listen 80; 

    root /var/www/websites/main/htdocs; 
    index index.html index.php index.htm;

    server_name *removed*;

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

    location ~ \.php$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:81;
    }

    location ~ /\.ht {
        deny all;
    }
}

This results in a redirect loop unfortunately with WordPress.

When I use the following configuration, the website loads fine, although nginx is handling all static and dynamic content which is against the point.

server {
    listen 80; 

    root /var/www/websites/main/htdocs;
    index index.html index.php index.htm;

    server_name *removed*;

    location ~* ^.+\.(ico|jpg|jpeg|gif|png|css|txt|js|flv|swf|html|htm|eot|woff|ttf|svg)$
    {
        access_log off;
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

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

    location ~ /\.ht {
        deny all;
    }
}

Using Chrome Developer tools I find that this is a 301 redirect loop. I can access wp-admin without any problems.

Answer

add to your wordpress themes function.php ->

remove_filter('template_redirect', 'redirect_canonical');

Attribution
Source : Link , Question Author : ssabetan , Answer Author : Balazs

Leave a Comment