I am trying to set up magento 2 in a subdirectory via nginx ? i want my url to be


If I run my website with base url : http://mywebsite.com/ .It works but when I add /shop/ it doesn't. I have tried a lot many things but still not able to figure out.

FYI : I am not trying to make a store here as other stack overflow solutions have suggested. I just want the subdirectory path in the url. Also I am using the nginx.conf.sample

I kept mostly everything same in nginx.conf but with this addition :

location /shop {
    try_files /shop/pub /shop/pub /index.php$is_args$args;


location /shop/static/ {
    # Uncomment the following line in production mode
    # expires max;

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/shop/static/version {
        rewrite ^/shop/static/(version[^/]+/)?(.*)$ /static/$2 last;

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|json)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/shop/static/?(.*)$ /static.php?resource=$1 last;
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;

        if (!-f $request_filename) {
           rewrite ^/shop/static/?(.*)$ /static.php?resource=$1 last;
    if (!-f $request_filename) {
        rewrite ^/shop/static/?(.*)$ /static.php?resource=$1 last;
    add_header X-Frame-Options "SAMEORIGIN";

The static resource loads fine but the page is giving me 404 error. Don't understand what might be the issue here ?

  • Did you checked with the database core_config table for the URL of your website? OR did you tried to change it from the backend configuration section? Commented Feb 15, 2019 at 11:17
  • I did both but couldn't get this working Commented Feb 15, 2019 at 14:10
  • Did you see my question and answer here? magento.stackexchange.com/questions/195175/… Commented Feb 18, 2019 at 16:04

3 Answers 3


I'm not an nginx expert, but Servers for Hackers has provided a superb walkthrough of nesting one PHP application within another. I've broken down their instructions in a way that might help, or at least point you in the right direction. I assume here that you're serving Magento from pub as Magento recommends, so that your directory structure looks like /var/www/shop/pub.

Add to your conf:

 location /shop { // if subdomain /shop/ is requested, then do this stuff
    alias /var/www/shop/pub; // declare /shop/ as an alias for your Magento instance
    try_files $uri $uri/ @shop; // resolve requests to /shop/ using a @shop variable, defined below

    location ~ \.php$ { // assuming you pass requests to fastcgi
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME   $request_filename; // reset SCRIPT_FILENAME to our Magento alias
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

location @shop { // resolve /shop/ requests, accounting for Magento subdomains
    rewrite /shop/(.*)$ /shop/index.php?/$1 last; // rewrite /shop/subdomain requests appropriately
  • can u explain in context of this file ? github.com/magento/magento2/blob/2.2/nginx.conf.sample Commented Jan 29, 2019 at 17:44
  • I am adding the following code in nginx.conf.sample : ..... location /shop { alias /Users/sarasranglt/Projects/websterweb/webster2_cloud/shop; try_files $uri $uri/ @shop; } location @shop { rewrite /shop/(.*)$ /shop/index.php?/$1 last; } location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ { .... its erroring out area code not found Commented Jan 29, 2019 at 17:46
  • Does the error say area code not found, or area code not set? Can you copy and paste the exact error text?
    – kookaburra
    Commented Jan 29, 2019 at 17:55
  • Fatal error: Uncaught Magento\Framework\Exception\LocalizedException: Area code is not set in Commented Jan 29, 2019 at 17:57
  • So can we chat somewhere else to better debug this? I would really appreciate your help. Commented Jan 29, 2019 at 17:58

I can think of two possible solutions. I didn't test both of them. So I would be glad to hear if they work.

  • Did you try to litterally add the /shop/ part in the base url? You can do that in the backend or directly in database in table core_config_data. Try checking/setting these web/unsecure/base_url, web/secure/base_url, web/unsecure/base_link_url and web/secure/base_link_url.
  • Did you try to move the /pub directory in a new /shop directory which you made? After this you should change the symlink of your www root to /shop/pub instead of just /pub. Also you should add another level up ../ on line 13 of index.php in the that directory so Magento could be loaded. It would look like this require __DIR__ . '/../../app/bootstrap.php';

These are suggestions. They might be incomplete or just part of the solution. If you could try them and give feedback I can take another look from there.


If you are trying to create a store only then you need to copy index.php and .htaccess from main website and then change path of

require __DIR__ . '/app/bootstrap.php';

To :

require '../app/bootstrap.php';

Then change $params value

$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = '<store_name>';

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';

.htaccess :

## path relative to web root

#RewriteBase /magento/

SetEnvIf Host .*<store_name>.* MAGE_RUN_CODE=<store_name>

SetEnvIf Host .*<store_name>.* MAGE_RUN_TYPE=website

OR :

Update index.php from pub folder

 * Public alias for the application entry point
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

try {
    require realpath(__DIR__) . '/../app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>

$params = $_SERVER;
    DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
    DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
    DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
    DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],

switch($_SERVER['HTTP_HOST']) {
    case "<domain>":
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = '<store_code>';
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');

Reference : How to run Magento 2 from a subdirectory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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