Nginx configuration for Magento running in subfolder

Nginx configuration for Magento running in subfolder

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Nginx configuration for Magento running in subfolder

Configuring Nginx for Magento Installed in a Subfolder

When you need to install an additional instance of Magento within a subfolder of your existing setup while maintaining URL rewrites, you’ll want to adjust the default Nginx configuration accordingly. Here’s how to modify the standard Magento Nginx configuration found at LaravelCompany to accommodate your new subdirectory installation.

Understanding the Default Configuration

Before making changes, let’s review the key sections of the default Nginx configuration:

server {
    # Main server block for domain
}

server {  ## Secondary Domain
    listen 80 default;
    # SSL directives might go here
    root /var/www/vhosts/DOMAIN.com;

    location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }

    # ... Other location blocks ...

    location @handler { ## Magento uses a common front handler
        rewrite / /index.php;
    }
}

Modifying the Configuration for Subfolder Installation

To install Magento in a subfolder (e.g., /magento2):

  1. Create a new server block: Add a new server block below the existing ones to handle requests for your subdirectory.
server {  ## Secondary Domain - Subfolder
    listen 80;
    server_name DOMAIN.com/magento2;

    location /magento2 {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }

    # ... Other location blocks for subfolder ...

    location @handler { ## Magento uses a common front handler
        rewrite /magento2//index.php;
    }
}
  1. Update the root and document_root: Make sure to point these to your new Magento installation directory.
root /var/www/vhosts/DOMAIN.com/magento2/public_html;

document_root $root;
  1. Configure PHP with appropriate fastcgi_param values:
location ~ \.php$ {
    # ... existing configuration ...

    fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
    fastcgi_param  MAGE_RUN_TYPE store;
}
  1. Add necessary location blocks: Include the appropriate location blocks to handle static files and disable access to sensitive directories.
location ^~ /magento2/app/                { deny all; }
location ^~ /magento2/includes/           { deny all; }
location ^~ /magento2/lib/                { deny all; }

Testing and Validating the Configuration

After making these changes, save your configuration file and reload Nginx using the appropriate command for your system. For example:

sudo service nginx reload

Make sure to validate that both your primary Magento installation and the new subdirectory instance are functioning as expected.

Remember, it’s essential to keep your server secure by following best practices such as restricting access to sensitive directories and enabling proper error handling.

Conclusion

Configuring Nginx for a Magento installation within a subfolder requires careful modification of the default configuration. By creating a new server block, updating the root paths, and adjusting PHP parameters, you can successfully deploy your additional Magento instance while maintaining URL rewrites.

If you find yourself needing more advanced configuration options or encountering issues during the setup process, don’t hesitate to refer to LaravelCompany’s comprehensive guides on Magento configurations.

Magento 2 Indexing Configuration SEO

Our network