What Is Nginx?

Nginx is an open-source web server that offers high performance and serves highly scalable architecture that is very different from Apache. It combines the features of HTTP load balancer, content cache, reverse proxy, and mail proxy.

It is an event-driven approach, which enables the processing of multiple requests at the same time. It requires PHP-fpm to process PHP requests.

There is a very easy approach when we are using an apache server for a single or multi-store in Magento 2. But what if when we are using Nginx server for Magento 2. The following are the steps that show you how to configure Nginx for Multistore, Single Store, and Subdomain.

Before Configuration, Some Basic And Required Steps That Are Necessary.

1. First, make sure you have properly installed the Nginx server and PHP-fpm with Magento 2 because Nginx only works with PHP-fpm. If you want to install Nginx, then follow these steps;

  • Run the command

apt-get -y install Nginx

  • Install and configure php-fpm and php – cli

apt-get -y install php7.0-fpm php7.0-cli

  • Open the php.ini file in an editor and run the following commands

vim /etc/php/7.0/fpm/php.ini
vim /etc/php/7.0/cli/php.ini

  • Make necessary modification in the file and save it in the editor

memory_limit = 2G;
max_execution_time = 1800
zlib.output_compression = on

  • Restart the service of PHP-fpm and Nginx by running the following commands.

systemctl restart php7.0-fpm
service restart nginx

  • Verify if Nginx is properly installed or not by running the following command. It will provide you with a list of responses to ensure that your Nginx has properly installed and service is active or not.

service nginx status
If you need any help or guidelines for the Nginx you can get by running this command as below;
systemctl -h nginx

2. Accept multiple domains in one virtual host file, or use one virtual host file for each website or store. The virtual host configuration files are located in;

/etc/nginx/sites-available.

3. Use and modify nginx.conf. – a sample file for the configuration, if necessary that is provided by Magento 2 by default.

4. Make sure Magento 2 has been installed to its path properly. For example., /var/www/html/magento2

5. Create more than one website or store in Magento 2 other than the default, as per the requirement.

After all this basic configuration now we are ready to configure Nginx for both Multi-websites and Single-Websites in Magento 2. The Multi-Websites concept in Magento 2 can be achieved either by using its subdomain or by creating a subfolder to its root directory.

Benefits of using Nginx as a web server for Magento 2 multi websites

Several benefits are:

  • High Performance: Nginx is known for its lightweight and efficient architecture, enabling it to handle high traffic loads and deliver fast response times.
  • Scalability: Nginx’s event-driven, non-blocking model allows it to handle many concurrent connections, making it suitable for scaling Magento 2 across multiple websites.
  • Reverse Proxy and Load Balancing: Nginx can act as a reverse proxy and distribute incoming traffic across multiple backend servers, ensuring optimal resource utilisation and improved reliability.
  • Caching: Nginx provides built-in caching capabilities, allowing for faster content delivery and reduced server load.
  • SSL/TLS Termination: Nginx simplifies SSL/TLS termination, enabling secure communication between clients and servers.
  • URL Rewriting: Nginx offers powerful URL rewriting capabilities, facilitating clean and SEO-friendly URLs for each Magento 2 website.

Multi Websites Using The Concept Of Sub Domain

Steps To Configure Nginx For Multi Websites In Magento 2

Step 1: Create or set up multi websites in Magento 2

1. To create a website

  • Click Stores -> Setting -> All Stores
  • On the Stores page, click Create Website and add following details
    • Name – Enter a name to identify the website.
    • Code – Enter a unique code. For example., if you have a Canada store, you can enter Canada.
    • Sort Order – Enter a numerical sort order; this is an optional field.
  • Click save Web Site and you can add as many as you wish, based on the requirement.

2. To create a store

  • Click Stores -> Setting -> All Stores
  • On the Stores page, click Create Store
    • Website – Click the name of the website with which to associate this store
    • Name – Enter name of the store
    • Code – Enter a unique code for the store
    • Root Category – Click and select the name of Root Category for this store
  • Click save Store and you can add as much as based on websites that you have created

3. To create a store view

  • Click Stores -> Setting -> All Stores
  • On the Stores page, click Create Store View
    • Store – Click the name of the store with which to associate this store view
    • Name – Enter a name for the store view for identifying
    • Code – Enter a unique code for the store view
    • Status – Select Enabled
  • Click the save Store view and you can add as much as based on websites and stores that you have created

Step 2: Create and configure Nginx Virtual hosts file by using the concept of Sub Domain.

Creating and configuring Nginx virtual host will define how we can load our websites on the storefront. You can do this either by defining a single virtual host file for multiple websites or multiple virtual host files per website by customizing code. You have to pass the necessary parameters to configure the virtual host file accordingly.

1. To create one virtual host file for each website

  • Open a text editor and add the following contents to a new file named

/etc/nginx/sites-available/magento:

map $http_host $MAGE_RUN_CODE {

default ”;

canada.site.com canada;

mexico.site.com mexico;

}

server {

listen 80;

server_name site.com canada.site.com mexico.site.com;

set $MAGE_ROOT /var/www/html/magento2;

set $MAGE_MODE developer;

set $MAGE_RUN_TYPE website; or store

include /var/www/html/magento2/nginx.conf;

}

  • Save your changes to the files and exit the text editor
  • Verify the server configuration: nginx -t
  • If successful, the following message displays

nginx: configuration file /etc/nginx/nginx.conf test is successful If errors display, check the syntax of your virtual host configuration files.

2. To create or configure multiple virtual host file per websites that will be customized according to its website

  • Open a text editor and add the following contents to a new file named

/etc/nginx/sites-available/canada.site.com:

map $http_host $MAGE_RUN_CODE {

canada.site.com canada;

}

server {

listen 80;

server_name canada.site.com;

set $MAGE_ROOT /var/www/html/magento2;

set $MAGE_MODE developer;

set $MAGE_RUN_TYPE website;

#or set $MAGE_RUN_TYPE store;

include /var/www/html/magento2/nginx.conf;

}

Create another file named mexico.site.com in the same directory with the following contents:

map $http_host $MAGE_RUN_CODE {

mexcio.site.com mexico;

}

server {

listen 80;

server_name mexico.site.com;

set $MAGE_ROOT /var/www/html/magento2;

set $MAGE_MODE developer;

set $MAGE_RUN_TYPE website; #or set $MAGE_RUN_TYPE store;

include /var/www/html/magento2/nginx.conf;

}

  • Save your changes to the files and exit the text editor
  • Verify the server configuration: nginx -t
  • If successful, the following message displays

nginx: configuration file /etc/nginx/nginx.conf test is successful If errors display, check the syntax of your virtual host configuration files.

Step 3: Restart service of Nginx by the following command and verify the site if it is working properly or not

service nginx restart

You can also do this in another alternate way if you have not sites-enabled in your Nginx configuration by creating a virtual host file in a different way for subfolders of the websites

Multi Websites Using The Concept Of Sub Folder

After creating, websites, stores and store views from Magento Admin. We have to create two subfolders for the two websites with its default websites as follows;

Step 1: Create or set up multi websites in Magento 2 by using the concept of Sub Folder

For example.,Canada

  • Create a folder named Canada within the Magento root Directory and add .htaccess and index.php from its Magento root directory.
  • Replace code in index.php

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

With this,

$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = ‘canada’;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = ‘website’;

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

For example., Mexico

  • Create a folder named Mexico within the Magento root Directory and add .htaccess and index.php from its Magento root directory.
  • Replace code in index.php

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

With this,

$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = ‘mexico’;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = ‘website’;

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

Step 2: Open CLI and enter the command. This will create your file as per your domain for each multiple websites

sudo vim /etc/nginx/conf.d/site.com.conf

Step 3: Open the file in editor by above command and apply the necessary changes as follows

server{

listen 80;

server_name site.com;

set $MAGE_ROOT /var/www/html/magento2;

set $MAGE_MODE developer;

include /var/www/html/magento2/nginx.conf;

}

And if you have multiple websites in it, then add this in file as follows

location /canada/ {

set $MAGE_ROOT /var/www/vhosts/site.com/httpdocs/pub;

set $MAGE_MODE developer;

try_files $uri $uri/ /canada/index.php?$args;

}

location /mexico/ {

set $MAGE_ROOT /var/www/vhosts/site.com/httpdocs/pub;

set $MAGE_MODE developer;

try_files $uri $uri/ /mexico/index.php?$args;

}

Configure Each Website Domain Virtual File On Nginx

Step 1: Open CLI and enter the command. This will create your file as per your domain for each multiple websites

In our example., we have two websites so we will create two each Nginx virtual host file

sudo vim /etc/nginx/conf.d/canada.site.com.conf and sudo vim /etc/nginx/conf.d/mexico.site.com.conf

Step 2: Open the file in the editor by above command and apply the necessary changes as follows

Canada

server{

listen 80;

server_name canada.site.com;

set $MAGE_ROOT /var/www/html/magento2/canada/;

set $MAGE_MODE developer;

include /var/www/html/magento2/nginx.conf;

}

Mexico

server{

listen 80;

server_name mexico.site.com;

set $MAGE_ROOT /var/www/html/magento2/mexico/;

set $MAGE_MODE developer;

include /var/www/html/magento2/nginx.conf;

}

Troubleshooting common issues and debugging Nginx integration in Magento 2 multi websites

Here are some key pointers for effective troubleshooting:

  • Check Nginx error logs for any error messages or warnings related to the multi-website configuration.
  • Verify the Nginx configuration files for syntax errors or misconfigurations, such as incorrect server blocks or missing directives.
  • Ensure the correct permissions are set for files and directories accessed by Nginx.
  • Investigate any issues with SSL/TLS certificates, including expiry or misconfiguration.
  • Debug and monitor network connections and request/response flows using tools like Wireshark or ngrep.
  • Analyze Magento 2 logs to identify any errors or issues specific to the multi-website setup.
  • Test individual website configurations separately to isolate and identify any conflicts or specific issues.

Testing and Verifying Nginx Integration in Multi-Website Environment for Magento 2

Read Important Points:

  • Verify that the Nginx server blocks are properly configured for each website in the multi-website setup. This ensures correct routing and separation of website traffic.
  • Test the SSL/TLS configuration to ensure secure communication between clients and the Nginx server, including proper certificate installation and encryption settings.
  • Validate that the Nginx server correctly routes requests to the appropriate website based on the server name specified in the configuration.
  • Verify that Nginx effectively delivers static files and media content, such as images and videos, for each website in the multi-website environment,
  • If load balancing is implemented, test the Nginx configuration to ensure even distribution of traffic across backend servers for each website.
  • Test Redirects and Rewrite Rules- Verify that redirects and rewrite rules are correctly implemented in the Nginx configuration. This ensures proper handling of URLs and website redirection.

Additional Note

  • Some additional tasks or changes might be required, like port changing to configure Nginx settings as per the requirement. You may have to contact the hosting provider for the additional changes.
  • Do not change the nginx.config.sample file unnecessary as it is Magento default core file. If you want to change, then copy the content of that file, create nginx.config file, paste the code in it and then make necessary changes for memory or execution time.
  • When Magento 2 using Nginx server, then .htaccess on the root directory is not going to be useful like it is used in Apache server. If you want to change the Mage_mode for the Magento 2 directory, then we have to change in two places, one is in Magento core file /app/etc/env.php and another in Nginx virtual host file, the one we created set $MAGE_MODE developer;
  • Make sure after each change in the Nginx configuration file, restart the Nginx services. It may happen that if you won’t restart Nginx services then your changes will not reflect on the storefront.

Final Takeaway

We hope you have understood the configuration for the Nginx in Magento 2. If you’re a Magento Developer or a Magento Development Agency, then you know that it is not entirely easy to perform this process, we have provided as much as information that helps to integrate easily with proper settings. Both Apache and Nginx have many different qualities. While Apache configuration is easier than Nginx, the latter is way better than Apache in terms of performance and lightweight results.

If you have any questions feel free to contact ZealousWeb and we will provide you with all answers to your questions.