Magento 2 nginx configuration

Note: This article is very old and doesn’t reflect the recommended nginx configuration for Magento 2. I did create a different article here Magento 2 nginx.conf.sample but for a quick and easy implementation check this stack exchange reply How to configure nginx for Magento 2?

Hi all,

With the updated dev release of Magento 2 (version 0.42.0-beta11) I decided to dig my nails in to the codebase & learn how it differs from Magento 1. I had a few issues setting up my NGINX config since the documentation for Magento 2 supports Apache. After trawling the bugs/issues on github I was finally able to get the installation wizard running. Below will help you get started if you’re used to the WNMP or LNMP stack.

Note: The installation wizard did not let me complete setup with MariaDB 5.6 so I installed Percona 5.6 instead. Magento 2 only officially supports Oracle MySQL 5.6+

server {
	listen 80;
	server_name DOMAIN.COM;
	rewrite / $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www
server {
	listen 80;
	#listen 443 default ssl;

	#ssl_certificate /var/www/sslCertificates/DOMAIN.COM.crt;
	#ssl_certificate_key /var/www/sslCertificates/DOMAIN.COM.key;

	server_name www.DOMAIN.COM *.DOMAIN.COM;
	root /var/www/magento2; 

	location / {
		index index.html index.php; ## Allow a static html file to be shown first
		try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
		#expires 30d; ## Assume all files are cachable

	location  /. { ## Disable .htaccess and other hidden files
		return 404;
	location @handler { ## Magento uses a common front handler
		rewrite / /index.php;
	location ~ \.php/ { ## Forward paths like /js/index.php/x.js to relevant handler
		rewrite ^(.*.php)/ $1 last;
	location ~ \.php$ { ## Execute PHP scripts
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		#fastcgi_param  HTTPS $fastcgi_https;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;	
		includefastcgi_params; ## See /etc/nginx/fastcgi_params

	 # Rewrite Internal Requests
	 location @handler {
		 rewrite / /index.php;

	 # Rewrite magento2 static files
	 location /pub/static {
		 rewrite ^/pub/static/(.*)$ /pub/static.php?resource=$1? last;

Leave a Reply

Your email address will not be published.

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