cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 / NGINX: Missing resource files after installation in subdirectory

Magento 2 / NGINX: Missing resource files after installation in subdirectory

Hello Magento community!

 

I'm currently evaluating in my former company if we should change our shop framework to Magento 2.1 CE. However I'm facing some troubles after the installation.

 

My setup:

I've configured an AWS EC 2 instance with Ubuntu 16.04 PHP 7 and NGINX 1.10 and installed Magento 2 over the CLI following this tutorial: http://devdocs.magento.com/guides/v2.1/install-gde/bk-install-guide.html

All permissions should be set correctly.

 

The webshop should be accessible via a subdirectory (e.g. www.company.com/magento/). So my NGINX web root is /var/company/shops.

In the shops directory there is only one subdirectory called magento which contains the Magento installation.

 

The Problem

This is what I see when opening the site (www.company.com/magento/)

2017-02-13_10-39-05.jpg

 

The Chrome Developer Tools show me the following errors:

2017-02-13_10-41-34.jpg

I assume that this problem is caused  by an incorrect NGINX config. However according to many posts on the web I should use the sample NGINX config file provided by Magento and change the $MAGE_ROOT.

 

sites-enabled/magento

# Example configuration:
 upstream fastcgi_backend {
    server unix:/var/run/php/php7.0-fpm.sock;
 }

 server {
    listen 6080;
    server_name staging.company.com;
    set $MAGE_ROOT /var/company/shops/magento;
    set $MAGE_MODE developer;

## Optional override of deployment mode. We recommend you use the
## command 'bin/magento deploy:mode:set' to switch modes instead.
##
## set $MAGE_MODE default; # or production or developer
##
## If you set MAGE_MODE in server config, you must pass the variable into the
## PHP entry point blocks, which are indicated below. You can pass
## it in using:
##
## fastcgi_param  MAGE_MODE $MAGE_MODE;
##
## In production mode, you should uncomment the 'expires' directive in the /static/ location block

root $MAGE_ROOT/pub;

index index.php;
autoindex on;
charset UTF-8;
error_page 404 403 = /errors/404.php;
#add_header "X-UA-Compatible" "IE=Edge";

# PHP entry point for setup application
location ~* ^/setup($|/) {
    root $MAGE_ROOT;
    location ~ ^/setup/index.php {
        fastcgi_pass   fastcgi_backend;

        fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
        fastcgi_param  PHP_VALUE "memory_limit=768M \n max_execution_time=600";
        fastcgi_read_timeout 600s;
        fastcgi_connect_timeout 600s;

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

    location ~ ^/setup/(?!pub/). {
        deny all;
    }

    location ~ ^/setup/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}

# PHP entry point for update application
location ~* ^/update($|/) {
    root $MAGE_ROOT;

    location ~ ^/update/index.php {
        fastcgi_split_path_info ^(/update/index.php)(/.+)$;
        fastcgi_pass   fastcgi_backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        include        fastcgi_params;
    }

    # Deny everything but index.php
    location ~ ^/update/(?!pub/). {
        deny all;
    }

    location ~ ^/update/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}

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

location /pub/ {
    location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
        deny all;
    }
    alias $MAGE_ROOT/pub/;
    add_header X-Frame-Options "SAMEORIGIN";
}

location /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 ~ ^/static/version {
        rewrite ^/static/(version\d*/)?(.*)$ /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 ^/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 ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/ {
    try_files $uri $uri/ /get.php$is_args$args;

    location ~ ^/media/theme_customization/.*\.xml {
        deny all;
    }

    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;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/customer/ {
    deny all;
}

location /media/downloadable/ {
    deny all;
}

location /media/import/ {
    deny all;
}

# PHP entry point for main application
location ~ (index|get|static|report|404|503)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 1024 4k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=768M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    # start modify to enable tls
    fastcgi_param  MAGE_MODE $MAGE_MODE;

    fastcgi_param  SCRIPT_NAME     $fastcgi_script_name;
    fastcgi_param  SERVER_NAME     $host;
    fastcgi_param  SERVER_PORT     $http_x_forwarded_port;
    fastcgi_param  REMOTE_ADDR     $http_x_real_ip;

    if ($http_cloudfront_forwarded_proto ~ https) {
      set $fastcgi_https on;
    }
    if ($http_x_forwarded_proto ~ https) {
      set $fastcgi_https on;
    }

    fastcgi_param  HTTPS           $fastcgi_https if_not_empty;
    # end modify to enable tls

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

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/xml+rss
    image/svg+xml;
gzip_vary on;

# Banned locations (only reached if the earlier PHP entry point regexes don't match)
location ~* (\.php$|\.htaccess$|\.git) {
    deny all;
}
}

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {
	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	include /etc/nginx/blacklist.conf;
	include /etc/nginx/blockips.conf;
	default_type application/octet-stream;

	##
	# Logging Settings
        ##
        log_format tractive_log_format '$remote_addr - $http_x_forwarded_for [$time_local] | "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time';

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 6;
	# gzip_buffers 16 8k;
	gzip_min_length 256;
	gzip_http_version 1.1;
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon;

	##
	# Security Headers
	##
#	add_header X-Xss-Protection "1; mode=block" always;
	add_header X-Content-Type-Options "nosniff" always;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/sites-enabled/*;
}

I would be glad if somebody of you could assist me.

4 REPLIES 4

Re: Magento 2 / NGINX: Missing resource files after installation in subdirectory

I had experienced the same issues with Magento 2.0, 2.1 and 2.1.3 installations. But after days of trial and error, I finally fixed all the issues of these types. The one thing I can confirm which will give you a clear direction. This issue is due to server configuration which includes, NGINX, MySQL and PHP configurations. Please follow guidelines provided by Magento for environment setup.

 

At the end I manged to successfully install all these version without any code changes in Magento files. Also Magento recommended file permissions been set. For NGINX I used sample configuration file.

 

Hope this helps to figure out the problem at your end.

Re: Magento 2 / NGINX: Missing resource files after installation in subdirectory

Hello,

I had the same problem for weeks. Today I found my solution. I enabled the RewriteEngine in Apache. The command is "a2enmod rewrite" or "sudo a2enmod rewrite". Then a "service apache2 restart" to restart the server.

Hope it will be helpfull and sorry for my bad english

Hajo

Re: Magento 2 / NGINX: Missing resource files after installation in subdirectory

Good afternoon. Tell me please, how did you solve this problem?

Re: Magento 2 / NGINX: Missing resource files after installation in subdirectory

Hi vadimttv1,

 

Please check your NGINX configuration and make sure you are using as defined in "nginx.conf.sample"

 

Hope this helps!

Best Regards,

Asad