Alex Paterson
Alex Paterson

Reputation: 514

Wordpress and NGINX /wp-admin redirect loop

My nginx.conf has a server blog containing this:

    location ~ \.php$ {
            root /var/www/html/blog;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
    location /blog {
            root /var/www/html/blog;
            include /etc/nginx/mime.types;
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

But with these settings when I try to access /blog/wp-admin my browser gets stuck in some redirect loop.

If I change the root URLs in nginx.conf to /var/www/html, /blog/wp-admin works, but my post permalinks give me a 404 error.

My WP files are located in /var/www/html/blog. I have 'SSL Insecure Content Fixer' plugin installed because my images giving a mixed content error on my site, which has a Cloudflare page rule to always use SSL.

My WP address and WP home are both set to http://xxx/blog.

Anybody fixed something similar?

Thanks

Upvotes: 1

Views: 5487

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

I think that the main problem is an inconsistency with your root directive. Your PHP configuration has WordPress in /var/www/html/blog whereas your static configuration has WordPress in /var/www/html/blog/blog.

Assuming that WordPress is installed in the root of /var/www/html/blog and that the URIs should be prefixed with /blog/ for both real files and permalinks, the correct URI for the entry point should be /blog/index.php.

The nginx.conf file should probably be:

root /var/www/html;

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
}
location /blog {
    include /etc/nginx/mime.types;
    try_files $uri $uri/ /blog/index.php;
}

If you have a conflicting root directive within the outer server container, the above root directive could be placed inside the two location blocks unmodified.

I would try /blog/index.php rather than /blog/index.php?q=$uri&$args as the last element of try_files because in my experience, WordPress uses the REQUEST_URI parameter to route permalinks rather than the q argument as you have implied, but YMMV.

If you do have other applications in this servers root and would like to segregate the WordPress root more completely, you might nest the PHP location block like this:

location ^~ /blog {
    root /var/www/html;
    include /etc/nginx/mime.types;
    try_files $uri $uri/ /blog/index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
    }
}

Upvotes: 4

Related Questions