Nginx + Magento in subfolder

Nginx + Magento in subfolder

August 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Nginx + Magento in subfolder

Moving Magento to Nginx: A Comprehensive Guide

Hello developers! Today, we’re going to tackle an issue that many of us face when migrating our applications from one server environment to another - moving a Magento installation from Apache to Nginx.

The Challenge: Magento and Nginx in a Subfolder

Steven here has set up a WordPress site on his main domain and an instance of Magento in a subfolder, but he’s running into 404 errors when navigating within the shop. Let’s dive into the details and find a solution together.

Understanding Steven’s Current Setup

Steven has configured his Nginx server block as follows:

server {
    # ... other configurations ...

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

    location /shop {
        index index.html index.php;
        try_files $uri $uri/ @handler;
        expires 30d;
    }

    location ~* .php/ { rewrite ^(.*.php)/ www last; }

    # ... other configurations ...
}

The Root Cause: Misconfigured Location Blocks

The issue lies in the way Steven has configured his location blocks for PHP handling. Let’s break it down:

  1. The main location block (location /) handles all requests that don’t match any other location block. It tries to serve static files first, then falls back to processing them through index.php if necessary.

  2. The /shop location block is designed to handle requests for the Magento shop. However, it’s configured to try serving static files (index.html) before falling back to PHP processing, which isn’t ideal for a PHP-based application like Magento.

  3. The regex-based location block (location ~* .php/) attempts to match URLs that end with ’/’. This is likely causing the 404 errors when Steven tries to access non-root pages within his shop.

The Solution: Refining Location Blocks for Magento

To resolve this issue, we need to refine our location blocks to handle Magento’s routing requirements:

  1. Update the main location block (location /) by adding a check for the /shop subfolder before falling back to processing through index.php:
location / {
    try_files $uri $uri/ /index.php?$args;
}

location ~ ^/(?!shop) {
    try_files $uri /index.php?$args;
}
  1. Remove or comment out the /shop location block. We don’t need it since we’re handling Magento’s routing in our main location block now.

  2. Update the regex-based location block (location ~* .php/) by removing the trailing slash from the regular expression to match all URLs ending with ‘.php’:

location ~ [^/]\.php(/|$) {
    try_files $uri /index.php?$args;
}
  1. Ensure that your Magento installation is configured correctly for running under Nginx. This includes updating your base_url in the core_config_data table to match the URL of your shop.

Best Practices for Running Magento on Nginx

To ensure optimal performance when running Magento on Nginx, consider these best practices:

  1. Enable caching: Magento generates a lot of dynamic content, so enabling server-side and/or browser caching can significantly improve load times.

  2. Optimize image sizes: Large images can slow down your site’s loading speeds. Consider compressing and optimizing your product images to reduce their size without sacrificing quality.

  3. Configure Apache-like modules: Nginx doesn’t support all the features that Apache has, but you can configure some Apache-like modules like fastcgi_param and rewrite to achieve similar functionality in Nginx.

Wrapping Up

Migrating Magento from one server environment to another can be a complex process, but with careful attention to detail and proper configuration of your location blocks, you can ensure a smooth transition to Nginx.

Remember to test your setup thoroughly after making any changes, as it’s crucial to catch and fix any potential issues before they affect your live site. Don’t hesitate to reach out if you have further questions or need additional guidance!

Thank you for reading, and happy coding! For more articles on Magento development and best practices, be sure to check out our blog at Laravel Company.

Conclusion

In this post, we’ve explored the challenges of running a Magento installation in a subfolder under Nginx. We’ve identified the root cause of Steven’s 404 errors and provided a step-by-step solution to resolve them.

By refining our location blocks to handle Magento’s routing requirements and following the best practices for running Magento on Nginx, we can ensure optimal performance and a smooth user experience for our e-commerce sites. Don’t forget to test your setup thoroughly after making any changes!

If you found this article helpful or have further questions, feel free to share your thoughts in the comments below. Thanks for joining us today!

Indexing Configuration Errors

Our network