Magento 2 Nginx Configuration
Everything about Magento 2 on Firebear
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
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; } |
You can enable the developer mode if required. Just use the following variable:
1 2 |
# Magento 2 Developer mode set $MAGE_MODE developer; |
Nginx Configuration for Magento 2 (sample)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# 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; } |
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:
1 2 3 4 |
map $http_host $MAGE_RUN_CODE { site1.store.com site1; site2.store.com site2; } |
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:
1 |
location ~ (index|get|static|report|404|503)\.php$ { ... } |
And insert these two lines into it:
1 2 3 4 |
... fastcgi_param MAGE_RUN_TYPE website; fastcgi_param MAGE_RUN_CODE $MAGE_RUN_MODE; ... |
3. Now, you can reload Nginx configuration. Be logged in as a user with root privileges:
1 |
service nginx reload |