Magento 2 nginx.conf.sample

This is a follow up to my original post Magento 2 nginx configuration, when Magento 2 was still in development. Back then I came across a few difficulties when trying to run Magento 2 on a Linux install running Nginx as the web server – namely a misconfiguration due to the lack of information available when trying to configure nginx directives to work with Magento 2. There was sparse developer documentation and a handful of independent and credible articles on the internet – I pretty much gave up due to the lack of “support”.

Magento 2 was released later that year (November 2015) and as a result developers and businesses started the migration process. This was great because it forced both developers and businesses to push and explore Magento 2 (and discover bugs and fix them!).

As a result, it feels like there is better support for Nginx, in the sense that Nginx is equally as supported when compared to Apache. This also means more knowledge & more expertise to tap into via the Magento community specifically when choosing Nginx over Apache.

The nginx.conf.sample file

Magento 2 includes an nginx.conf.sample file that can either be used as an independent nginx configuration or as an include within an existing nginx configuration file.

Setting up the nginx.conf.sample as an independent file is simple if you’re a seasoned server admin. Equally, setting up the nginx.conf.sample as an include is also simple. However I ran into a dilemma of wanting to run multiple test/production websites on the same server in both production and developer modes (and let me make tweaks to each config separately) but in a way that required little maintenance of said configuration files.

Creating multiple independent configuration files based on the sample has it drawbacks. Lets say for example you have 3 websites and decide to create 3 independent nginx files. You would copy the nginx.conf.sample 3 times and edit it accordingly. Can you see a pattern? Every time you wanted to adjust a setting across all configs you would have to edit each file. Which is why there are includes to begin with.

Moving forward

I had the following options for tackling this problem:

  1. Create 3x independent nginx configuration files based on nginx.conf.sample (this isn’t bad or wrong, just inefficient)
  2. Create 3x independent nginx files but include nginx.conf.sample (now we are becoming efficient)
  3. Create a single nginx configuration file with multiple server blocks for 3 websites and include nginx.conf.sample… (this is ideal)

I decided to go with option 3 for the obvious reason (oh no, there’s that magic number again).

This is what my “single” nginx configuration looked like:

## server block #1
server {
listen 80;
server_name mage.test;
set $MAGE_ROOT /var/www/magento2-test;
set $MAGE_MODE developer; # or production or developer
include /etc/nginx/conf.d/nginx.conf.sample;
...
}
## server block #2
server {
listen 80;
server_name mage.test2;
set $MAGE_ROOT /var/www/magento2-test2;
set $MAGE_MODE developer; # or production or developer
include /etc/nginx/conf.d/nginx.conf.sample;
...
}
## server block 3
server {
listen 80;
server_name mage.production;
set $MAGE_ROOT /var/www/magento2-production;
set $MAGE_MODE production; # or production or developer
include /etc/nginx/conf.d/nginx.conf.sample;
...
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.