Nginx configration for magento 2

Nginx configration for magento 2
Facebooktwitterredditpinterestlinkedinmail

Nginx configration for magento 2

Here i have explained how we can do Nginx configration for magento 2 and properly map virtual host to magento root directory and how enable virtual host.

ubuntu$ sudo vi /etc/nginx/sites-available/default
server {
    listen 80;
    server_name _;
    rewrite / $scheme://$host$request_uri permanent;
}
 
server {
    listen 80 default_server;
    listen [::]:80 default_server;
	
    server_name _;
    root /var/www/html;
    
    location /setup {
        try_files $uri $uri/ @setuphandler;
    }

    # Rewrite Setup's Internal Requests
    location @setuphandler {
        rewrite /setup /setup/index.php;
    }

    location / {
        index index.php index.html;
        try_files $uri $uri/ @handler;
    }

    # Rewrite Internal Requests
    location @handler {
       rewrite / /index.php;
    }

    location /pub/static {
         try_files $uri $uri/ @static;
    }

    location @static {
          rewrite ^/pub/static/(.*)$ /pub/static.php?resource=$1? last;
    }

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

        	if (!-f $request_filename) {
            		rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
        	}
    	}
    	location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        	add_header Cache-Control "no-store";
        	expires    off;

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

    location /media/ {
	try_files $uri $uri/ /get.php?$args;
	location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
		add_header Cache-Control "public";
		expires +1y;
		try_files $uri $uri/ /get.php?$args;
	}
	location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
		add_header Cache-Control "no-store";
		expires    off;
		try_files $uri $uri/ /get.php?$args;
	}
    }
    

    location /media/customer/ {deny all;}
    location /media/downloadable/ {deny all;}
    location ~ /media/theme_customization/.*\.xml$ { deny all; }
    location /errors/ {try_files $uri =404;}
    location ~ ^/errors/.*\.(xml|phtml)$ {deny all;}
    location ~ cron\.php {deny all;}

    location ~ \.php$ {
		if (!-e $request_filename) { rewrite / /index.php last; }
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
Facebooktwitterredditpinterestlinkedinmail