Restrict Access to Magento Admin Area in Nginx by IP

Restrict Access to Magento Admin Area in Nginx by IP

September 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Restrict Access to Magento Admin Area in Nginx by IP

Restricting Access to Magento Admin Area in Nginx by IP

Steve’s question highlights an important security concern - limiting access to your Magento admin area. This is particularly crucial for e-commerce platforms where unauthorized access can lead to catastrophic data breaches or financial losses. In this post, we’ll guide you through the process of restricting admin access using Nginx.

Understanding the Original Apache Configuration

Before diving into Nginx, let’s break down Steve’s original Apache configuration:

RewriteCond %{REMOTE_ADDR} !^116\.71\.8\.191
RewriteCond %{REQUEST_URI} admin [NC]
RewriteRule ^(.*)$ / [F,L]

This rule checks if the requested URI contains ‘admin’ and the remote IP address does not match 116.71.8.191. If both conditions are met, it redirects all requests to the home page (/) with a permanent redirect (status 301).

Implementing Similar Restrictions in Nginx

While Nginx uses a different configuration syntax compared to Apache, achieving similar results is straightforward. You can use the map and location directives to accomplish this.

  1. Create an IP Map

First, create a map that defines your allowed IP addresses:

map $remote_addr $allow_admin_access {
    default 0;
    116.71.8.191   1; # Add or remove IPs as needed
}

The map directive creates a new variable $allow_admin_access based on the remote address of incoming requests. If the IP matches any of the defined addresses, it sets the value to 1 (true); otherwise, it remains 0 (false).

  1. Restrict Access Using Location

Next, use the location directive to restrict access based on the map variable:

location /admin {
    allow $allow_admin_access;
}

This rule specifies that only users with $allow_admin_access set to 1 can access URLs starting with /admin. If a user attempts to access the admin area from an unauthorized IP, they will receive a 403 Forbidden error.

Complete Nginx Configuration

Combining both sections, your complete Nginx configuration for restricting Magento admin access by IP should look like this:

map $remote_addr $allow_admin_access {
    default 0;
    116.71.8.191   1; # Add or remove IPs as needed
}

location /admin {
    allow $allow_admin_access;
}

Conclusion: Enhancing Magento Security with Nginx

As you can see, restricting access to the Magento admin area in Nginx is quite similar to doing so in Apache. By using maps and location directives, you can effectively control which IP addresses have permission to access sensitive admin URLs.

Remember, security is a multi-layered approach. While this configuration limits IP-based access, it’s important to also implement other best practices like strong passwords, two-factor authentication, and regular Magento updates for optimal protection of your e-commerce store.

If you’re looking for more advanced Magento security solutions or expert consulting services, feel free to reach out to Laravel Company. Our team specializes in providing secure, high-performing Magento development services tailored to meet your specific business needs.

Security API Admin Configuration

Our network