How to configure Nginx for Magento 2?

Nginx was firstly developed by Igor Sysoev in 2002 and announced in 2004. In 2011, Nginx, Inc was founded and started providing commercial supports in 2012. Nginx is an open source reverse proxy server for different protocol HTTP, HTTPS, SMTP, POP3 and IMAP as well as a load balancer, HTTP cache and web server. Nginx highly pays attention to high concurrency, high performance and low usage of memory. Nginx is also known with good stability, multiple features, simple configuration and low usage of memory.

Magento 2 users can easily install Magento 2 on Nginx by performing in Server block the configuration settings.

Below, we will introduce you an example of configuring Nginx for Magento 2. You can consider this way as Apache virtual hosts.

Let’s start!

In example.com website, you create /etc/nginx/sites-available/example.com file then apply following commands:

upstream fastcgi_backend {

   server   unix://var/run/php5-fpm.sock;

}

server {

   listen 80;

   server_name example.com; 

   set $MAGE_ROOT /path/to/magento/root/dir/;

   set $MAGE_MODE default; 

   include /path/to/magento/root/dir/nginx.conf.sample;

}

If you are required to enable the developer mode, use variable below:

# Magento 2 Developer mode

set $MAGE_MODE developer;

Sample of Nginx configuration for Magento 2:

# Magento Vars

# set $MAGE_ROOT /path/to/magento/root;

# set $MAGE_MODE default; # or production or developer

#

# Example configuration:

# upstream fastcgi_backend {

#   # use tcp connection

#   # server 127.0.0.1:9000;

#   # or socket

#   server   unix://var/run/php5-fpm.sock;

# }

# server {

#   listen 80;

#   server_name mage.dev;

#   set $MAGE_ROOT /var/www/magento2;

#   set $MAGE_MODE developer;

#   include /vagrant/magento2/nginx.conf.sample;

# }
 

root $MAGE_ROOT/pub;

index index.php;

autoindex off;

charset off; 

add_header 'X-Content-Type-Options' 'nosniff';

add_header 'X-XSS-Protection' '1; mode=block'; 

location /setup {

   root $MAGE_ROOT;

   location ~ ^/setup/index.php {

       fastcgi_pass   fastcgi_backend;

       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";

   }

} 

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?$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/ {

   if ($MAGE_MODE = "production") {

       expires max;

   }

   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/(version\d*/)?(.*)$ /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 ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;

       }

   }

   if (!-f $request_filename) {

       rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;

   }

   add_header X-Frame-Options "SAMEORIGIN";

} 

location /media/ {

   try_files $uri $uri/ /get.php?$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?$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?$args;

   }

   add_header X-Frame-Options "SAMEORIGIN";

} 

location /media/customer/ {

   deny all;

} 

location /media/downloadable/ {

   deny all;

} 

location /media/import/ {

   deny all;

} 

location ~ cron\.php {

   deny all;

} 

location ~ (index|get|static|report|404|503)\.php$ {

   try_files $uri =404;

   fastcgi_pass   fastcgi_backend; 

   fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";

   fastcgi_param PHP_VALUE "memory_limit=256M \n max_execution_time=600";

   fastcgi_read_timeout 600s;

   fastcgi_connect_timeout 600s;

   fastcgi_param MAGE_MODE $MAGE_MODE; 

   fastcgi_index index.php;

   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

   include       fastcgi_params;

}

Set up Magento 2 multiple websites or stores with Nginx

Step 1: Set up websites, stores and store views in the Magento Admin

Step 2: Create Nginx virtual hosts

  • Verify your site

If you don’t have DNS set up for URLs of your stores, you have to add a static route to the host in your hosts file by:

  • Locating the hosts file of your operating system
  • Adding the static route in the format:
    <ip address> french.mysite.mg
    <ip address> german.mysite.mg
  • Going to one in URLs below in your browser:
    http://mysite.mg/admin
    http://french.mysite.mg/frenchstoreview
    http://german.mysite.mg/germanstoreview

Step 3: Pass the values of the Magento variables $MAGE_RUN_TYPE and $MAGE_RUN_CODE to Nginx using the Magento-provided nginx.conf.sample.

  • $MAGE_RUN_TYPEcan be either store or website
    • Use websiteto load your website in your storefront
  • Use storeto load any store view in your storefront
  • $MAGE_RUN_CODEis the unique website or store view code that corresponds to $MAGE_RUN_TYPE

That’s tutorial related to configuring Nginx for Magento 2 we show you.

Thanks for reading!