env details:

  • LEMP stack
    • Ubuntu 17.04
    • NGINX 1.10.3
    • MySQL 14.14 Distrib 5.7.20, for Linux (x86_64)
    • PHP 7.1.12-3+ubuntu17.04.1+deb.sury.org+1 (cli)

I am trying to run Magento 2 in a subfolder e.g. http://xxx.xxx.xxx.xxx/magento2

I have configured http and https values in Magento2's core_config_data table as:

http://xxx.xxx.xxx.xxx/magento2/
https://xxx.xxx.xxx.xxx/magento2/

Here is the configuration file for the virtual host:

## Maps config file
include conf_m2/maps.conf;

server {
    listen 80 reuseport;
    server_name xxx.xxx.xxx.xxx;

    access_log  /var/log/nginx/access.log  main if=$writelog;
    error_log   /var/log/nginx/error.log  error;

    if ($bad_client) { return 444; }

    root /var/www/html;

    ## Nginx and php-fpm status
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }

    location ~ ^/(status|ping)$ {
        include conf_m2/php_backend.conf;
        fastcgi_intercept_errors on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }

    ## Static location
    rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
    location ~ ^/static/(version\d*/)?(.*)$ {
        ## images css js
        location ~* \.(jpg|jpeg|png|gif|svg|js|css|ico|txt)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "www.example.com, cdn.example.com";
                add_header Cache-Control "public";
        try_files $uri $uri/ @static;
        }
        ## fonts
        location ~* \.(swf|eot|ttf|otf|woff|woff2)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "www.example.com, cdn.example.com";
                add_header Cache-Control "public";
        try_files $uri $uri/ @static;
        }
    try_files $uri $uri/ @static;
    }

    ## Media location
    location /media/ {
    ## images css js
        location ~* \.(jpg|jpeg|png|gif|svg|js|css|ico|txt)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "www.example.com, cdn.example.com";
                add_header Cache-Control "public";
        try_files $uri $uri/ @media;
        }
        ## fonts
        location ~* \.(swf|eot|ttf|otf|woff|woff2)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "www.example.com, cdn.example.com";
                add_header Cache-Control "public";
        try_files $uri $uri/ @media;
        }
    try_files $uri $uri/ @media;
    }

    rewrite ^/magento2$ /magento2/$1 permanent;

    location ~ /magento2/ {
        ##################################
        ## Set Magento root folder
        set $MAGE_ROOT /var/www/html/magento2;
        ## Set main public directory /pub
        root $MAGE_ROOT/pub;
        ##################################

        # Deny all internal locations, except phpmyadmin
        location ~ ^/(app|bin|var|tmp|phpserver|vendor|magento_version|pma)/? { deny all; } 

        try_files $uri $uri/ /magento2/index.php$is_args$args;
        index index.php index.html index.htm;

        location ~ /@static { rewrite /static/(version\d*/)?(.*)$ /static.php?resource=$2 last; }
        location ~ /@media { try_files $uri $uri/ /get.php$is_args$args; }  

        ## Protect extra directories
        include conf_m2/extra_protect.conf;

        ## Process php files (strict rule, define files to be executed)
        location ~ /(index|health_check|get|static|report|404|503)\.php$ {
            add_header X-UA-Compatible 'IE=Edge,chrome=1';
            add_header Link "; rel=\"canonical\"" always;
            ## php backend settings
            fastcgi_pass            127.0.0.1:${MAGE_ROUTE};
            fastcgi_index           index.php;
            include                 fastcgi_params;
            fastcgi_keep_conn       on;         

            ## Microcache
            fastcgi_no_cache $no_cache;
            fastcgi_cache_bypass $no_cache;
            fastcgi_cache microcache;
            fastcgi_cache_key $scheme|$host|search|$arg_q;
            fastcgi_cache_valid 200 301 302 2h;
            fastcgi_cache_use_stale updating error timeout invalid_header http_500;
            fastcgi_pass_header Set-Cookie;
            fastcgi_pass_header Cookie;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        }
    }

    rewrite ^/magento2/my_custom_admin$ /magento2/my_custom_admin/$1 permanent;
}

The error logged by nginx is:

FastCGI sent in stderr: "Unable to open primary script: /var/www/html/magento2/pub/magento2/index.php (No such file or directory)"

Could anyone shed some light on where i'm going wrong here?

EDIT Added: rewrite ^/magento2$ /magento2/$1 permanent; as suggested here , Nginx log:

"Unable to open primary script: /var/www/html/magento2/pub/magento2/index.php (No such file or directory)"
  • I guess your error is here: ` try_files $uri $uri/ /magento2/index.php$is_args$args;` I think by adding there /magento2/index.php... you are telling nginx to look for the file in that subdir (that is being nested in the pub dir and I understand shouldn't as you are changing the url, not the location of the index) Yet that dir / url structures in magento are a bit tricky and I don't have any to try (so perhaps I'm far from the solution) – ignivs Jan 2 at 21:21
  • 1
    by the way, there is a whole magento channel here in: magento.stackexchange.com There people should be more familiar with the internal url rewriting magento performs, and the way it is cached (take that into account as you may not be applying internal url changes correctly as well) – ignivs Jan 2 at 21:24
  • @ignivs thanks for the quick response. I have already tried without the /magento2 in front of the rewrite but then Magento is looking for index.php in: open() "/var/www/html/index.php" failed (2: No such file or directory). I did also consider posting on the magento stack exchange I think I will link this post there too. Magento was working from the base url prior to my attempts to change to a subfolder so I had thought it to be an NGINX configuration issue. – HostMyBus Jan 3 at 9:57

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.