Cant get into wp-admin page (cloudflare, nginx)

xPalis asked:

I have a wordpress website that is running on Nginx behind cloudflare DNS. I have used “set_real_ip_from” for all cloudflare ips in nginx.conf. When i try to access my website i can see my real ip in the logs. I have changed the default wordpress admin login directory from wp-admin to my directory name. I only want specific ips to be allowed to login to my wordpress but with this in my websites config, server block: location ~ /(wp-admin|wp-login|mydirectory) {
allow my.real.ip;
deny all;
return 404;
i cant seem to access wordpress admin and i get 404. If i delete this block everything is working fine so i guess im typing something wrong ? i looked around on this forum with some different variations but none of them seem to work. They either allow me to access to wp-admin but then anyone can access or it blocks everyone.

Unfortunately i couldnt get it to work no matter what i tried so what i did was deleted the location ~ /(wp-admin|wp-login|mydirectory) block, reloaded nginx config, logged in to wordpress, disabled wp-admin change directory plugin, then went back and re-added the location ~ /(wp-admin|wp-login|mydirectory) block inside it i added location ~ .php$ { as @Michael Hampton suggested and now i can access my wordpress admin page by using “wp-admin” url. Dont know why it didnt work with the custom url but i guess this will do since nginx will block anyone else trying to access it tho it would have been interesting to know why it didnt work :/

My answer:

You get a 404 because you specifically asked for a 404 for all requests matching that location.

    return 404;

You will need to remove this statement if you want to receive anything other than a 404.

Further, you will need to place a nested location within that location to process PHP files.

location ~ /(wp-admin|wp-login|mydirectory) {
    location ~ .php$ {

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.