Magento 2 Nginx Configuration

- Magento 2
 

magento2-nginx-configuration

Nginx is a reverse proxy server for different protocols, such as HTTPSHTTP,  POP3SMTP, and IMAP, as well as an HTTP cache, a load balancer,  and an origin web server. Being an open source project, Nginx has been started with a strong focus on high performance, high concurrency,  and low usage of memory. Nginx runs on Mac OS XLinux, BSD variants, Solaris, HP-UX, AIX, and other *nix flavors. In addition, you can find a proof of concept port for Windows. The project is licensed under the 2-clause BSD-like license, and below we will shade light on Magento 2 Nginx Configuration.

Everything about Magento 2 on Firebear

Magento 2 Demo

To deploy Nginx to interact with a dynamic HTTP content you should use different alternatives to the CGI protocol (FastCGI, SCGI), Web Server Gateway Interface or Phusion Passenger module. It can also work as a software load balancer.

The development of Nginx has been started in 2002, and Nginx, Inc was formed 9 years later – in July 2011. The company offerы commercial support in February since 2012, and a paid NGINX Plus subscription since August 2013.

To handle requests, Nginx relies on an event-driven asynchronous approach, instead of a process-oriented approach of the Apache HTTP Server model with the Event MPM as the asynchronous processing model. Thanks to modular event-driven architecture, the performance of Nginx is more predictable under higher loads.

Fortunately, Magento 2  can be easily installed on Nginx. You just need to implement the following configuration settings in the “Server block”. Consider this method as an equivalent to Apache virtual hosts. For a website “example.com” create file /etc/nginx/sites-available/example.com add the lines listed below:

You can enable the developer mode if required. Just use the following variable:

 Nginx Configuration for Magento 2 (sample)

How to set up multiple sites with Nginx

First of all, set up your Nginx configuration as recommended in the <Magento root>/nginx.conf.sample. The file is provided with the Magento software. You should get a small configuration file (main.conf); the configuration file created on the basis of the Magento-provided sample (nginx.magento.conf).

Now, we will explain, how to configure two websites – site1.store.com and site2.store.com. Note that multiple hosts require just one entry point.

It is possible to utilize the same method for running multiple stores with the help of the value store instead of website for the $MAGE_RUN_TYPE variable.

1. First of all, you should write a map directive in order to set the aforementioned  $MAGE_RUN_CODE variable:

It sets the $MAGE_RUN_CODE variable on the basis of the host. So if your host (you make a request to it) is site1.store.com,then $MAGE_RUN_CODE should be set to site1.

2. Then, it is necessary to send MAGE_RUN_CODE and MAGE_RUN_TYPE variables to the appropriate php-fpm server. Find the following block:

And insert these two lines into it:

3. Now, you can reload Nginx configuration. Be logged in as a user with root privileges:

Source