ANS Documentation

Improve This Doc
  • Cloud
  • Domains and DNS management
  • Backup and High Availability
  • eCommerce Stacks
    • Magento
      • Magento 1
      • Magento 2
    • Shopware
    • WooCommerce
    • AeroCommerce
  • Security
  • Email
  • Monitoring and usage management
  • Networking
  • Operating systems
  • Webcelerator
  • MyUKFast
  • Home >
  • eCommerce Stacks >
  • Magento >
  • Magento 2 >
  • Magento 2 in a Sub Directory

Magento 2 in a Sub Directory¶

If you have Magento 2 in a sub directory within your document root you will need to edit your domain’s NGINX configuration (Example: /etc/nginx/conf.d/example.com.conf).

For this example we have Magento 2 in the sub directory /var/www/vhosts/example.com/htdocs/shop/. You need to replace the instance of replacemebackend with the PHP-FPM configuration pool name (This should be defined at the top of your NGINX configuration file)

location ~ ^/shop/static/version {
    expires max;

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

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

      if (!-f $request_filename) {
        rewrite ^/shop/static/(version\d*/)?(.*)$ /shop/static.php?resource=$2 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/(version\d*/)?(.*)$ /shop/static.php?resource=$2 last;
      }
    }

    if (!-f $request_filename) {
      rewrite ^/shop/static/(version\d*/)?(.*)$ /shop/static.php?resource=$2 last;
    }

    add_header X-Frame-Options "SAMEORIGIN";
  }

location ~ ^/shop/ {
  index index.php index.html index.htm;
  try_files $uri $uri/ @shophandler;
  expires 30d;

  location ~ (index|get|static|report|404|503|health_check)\.php$ {
    try_files $uri =404;
    fastcgi_pass replacemebackend;
    fastcgi_buffers 1024 4k;
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    #fastcgi_param HTTPS $my_https; # Uncomment the below for SSL offloading
    #fastcgi_param SERVER_PORT $my_port; # Uncomment the below for SSL offloading

    #fastcgi_param  MAGE_RUN_CODE <REPLACEME>; # Uncomment the below to set multistore run code
    #fastcgi_param  MAGE_RUN_TYPE store; # Uncomment the below to set multistore run type

    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
     }
  }

  location @shophandler {rewrite / /shop/index.php; }

To implement this change you need to reload the NGINX service. First perform a configuration test with the following command:

 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there are no errors in the configuration test proceed to reload the NGINX server with the following command:

 ~]$ nginx -s reload

Next Article > Adding Domain(s) To The Stack

  • Useful Links
  • SMB
  • Enterprise
  • Channel
  • Public Sector
  • ANS Data Centres
  • About ANS
  • Careers
  • Blog
  • Get in touch
  •  
  • Sales 0800 458 4545
  • Support 0800 230 0032
  • Get in touch

© ANS Group Limited | Terms and Conditions | Corporate Guidance | Sitemap
ANS Group Limited, registered in England and Wales, company registration number 03176761, registered office 1 Archway, Birley Fields, Manchester M15 5QJ