0

I am trying to restrict access to the admin area by IP address, but it's not working. When I type in the admin url, it redirects me to the home page. If I change the IP address being whitelisted, it comes up with a 403 forbidden message. Here is my current whitelist configuration for Nginx:

   location ~* ^/(index\.php/admin|admin) {
     allow 67.166.183.12;
    try_files $uri $uri/ /index.php?$args;
location ~* \.php$ { try_files /dummy @proxy; }
deny all;

   }

Everything I have seen online says to use this configuration for whitelisting the admin page for Magento 2. If I don't have the whitelist active, I can access the admin page. My site uses Nginx and Varnish for caching.

0

You can set up IP whitelisting for Magento 2 admin area using NGINX by allocating a separate location, and unconditionally route requests through index.php.

Citing the article linked:

location ~ "^/admin_" {

    allow 1.2.3.4;
    allow 1.2.3.5;
    allow 1.2.3.6;
    deny all;

    fastcgi_pass   unix:/var/run/php-fpm/example.com.sock;
    fastcgi_buffers 1024 4k;
    fastcgi_buffer_size 128k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=4G \n max_execution_time=600 \n max_input_vars=100000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_param  SCRIPT_FILENAME  $document_root/index.php;
    include        fastcgi_params;
}

Since all requests to admin area will unconditionally go through PHP-FPM, we have eliminated try_files, and by this made things faster in terms of accessing admin area. The extra benefit of allocating this block, is of course, the one we want – being able to apply additional configuration

Needless to say, you will need to adjust value to fastcgi_pass to point to your actual PHP-FPM socket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.