Мои настройки Nginx для mage2.pro (пример использования HTTPS, а также магазинов на Magento 2 и Magento 1.x на одном домене с Discourse)

server {
	listen 80;
	server_name mage2.pro;
	return 301 https://$server_name$request_uri;
}
server {
	listen 80;
	server_name www.mage2.pro;
	rewrite ^ https://mage2.pro$request_uri? permanent;
}
server {
	listen 443 ssl;
	server_name www.mage2.pro;
	rewrite ^ https://mage2.pro$request_uri? permanent;
}
server {
	listen 443 ssl;
	server_name mage2.pro;
    ssl_certificate certificates/mage2.pro/PositiveSSL/bundle.crt;
    ssl_certificate_key certificates/mage2.pro/PositiveSSL/.key;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	error_page 419 = @magento;
	proxy_set_header X-Forwarded-Proto $scheme;
	location ~ /\. {deny all;}
	location = /ssh/packages.json {rewrite ^ /packages.php?ssh=1 last;}
	location = /packages.json {rewrite ^ /packages.php last;}
	location = /packages.php {
		root /var/www/m2/composer/pub;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
		expires 0;
		add_header Cache-Control no-cache;
		fastcgi_pass 127.0.0.1:9000;
	}
	location /store {rewrite ^ https://mage2.pro/c/extensions permanent;}
	location /sandbox {
		root /var/www/m2;
		location = /sandbox {rewrite /sandbox /sandbox/index.php last;}
		location = /sandbox/ {rewrite /sandbox /sandbox/index.php last;}
		location = /sandbox/setup {rewrite /sandbox /sandbox/setup/index.php last;}
		location = /sandbox/setup/ {rewrite /sandbox /sandbox/setup/index.php last;}
		location /sandbox/setup {
			# 2015-07-14
			# Часть веб-адресов мастера установки являются относительными текущего пути:
			# <script type="text/javascript" src="pub/magento/setup/success.js"></script>
			# поэтому при установке надо указывать адрес со слэшем на конце:
			# https://discourse.pro/store/setup/
			location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
				access_log off;
				expires max;
				try_files $uri =404;
			}
			# Нельзя писать здесь =, потому что через index.php запрашиваются различные страницы,
			# например: https://discourse.pro/store/setup/index.php/navigation/menu
			location /sandbox/setup/index.php {return 419;}
		}
		location /sandbox/pub/ {
			location /sandbox/pub/media/ {
				location /sandbox/pub/media/customer/ {deny all;}
				location /sandbox/pub/media/downloadable/ {deny all;}
				location ~ /sandbox/pub/media/theme_customization/.*\.xml$ {deny all;}
				try_files $uri $uri/ /sandbox/pub/get.php?$args;
				location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
					add_header Cache-Control "public";
					expires +1y;
					try_files $uri $uri/ /sandbox/pub/get.php?$args;
				}
				location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
					add_header Cache-Control "no-store";
					expires off;
					try_files $uri $uri/ /sandbox/pub/get.php?$args;
				}
			}
			location /sandbox/pub/static/ {
				expires max;
				location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
					add_header Cache-Control "public";
					expires +1y;
					if (!-f $request_filename) {
						rewrite ^/sandbox/pub/static/(version\d*/)?(.*)$ /sandbox/pub/static.php?resource=$2 last;
					}
				}
				location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
					add_header Cache-Control "no-store";
					expires off;
					if (!-f $request_filename) {
						rewrite ^/sandbox/pub/static/(version\d*/)?(.*)$ /sandbox/pub/static.php?resource=$2 last;
					}
				}
				if (!-f $request_filename) {
					rewrite ^/sandbox/pub/static/(version\d*/)?(.*)$ /sandbox/pub/static.php?resource=$2 last;
				}
			}
			location ~ ^/sandbox/pub/errors/.*\.(xml|phtml)$ {deny all;}
			location /sandbox/pub/errors/ {try_files $uri =404;}
			location = /sandbox/pub/cron.php {deny all;}
		}
		location ~ (index|get|static|report|404|503|phpinfo)\.php$ {return 419;}
		if (-e $request_filename) {return 403;}
		rewrite /sandbox /sandbox/index.php last;
	}

	location /sandbox2 {
		index index.html index.php;
		root /var/www/m2;
		try_files $uri $uri/ @sandbox2_handler;
	}
	location @sandbox2_handler {
		rewrite / /sandbox2/index.php;
	}
	location ~ /sandbox2/.+\.php$ {
		root /var/www/m2;
		if (!-e $request_filename) {
			rewrite / /sandbox2/index.php last;
		}
		expires        off;
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include        fastcgi_params;
	}
	# media
	location ~* ^/sandbox2/.+\.(jpg|jpeg|gif|css|png|js|ico|swf)$ {
		proxy_set_header X-Forwarded-Proto $scheme;
		root /var/www/m2;
		access_log off;
		expires  max;
		break;
	}
    location ^~ /sandbox2/app/                { deny all; }
    location ^~ /sandbox2/includes/           { deny all; }
    location ^~ /sandbox2/lib/                { deny all; }
    location ^~ /sandbox2/media/downloadable/ { deny all; }
    location ^~ /sandbox2/pkginfo/            { deny all; }
    location ^~ /sandbox2/report/config.xml   { deny all; }
    location ^~ /sandbox2/var/                { deny all; }

	location / {
		root /var/www/res/discourse;
		access_log off;
		# 2015-11-28
		# Позволяет нам вызывать $.getJSON('https://mage2.pro/google-fonts.json');
		# http://enable-cors.org/server_nginx.html
		# https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
		add_header 'Access-Control-Allow-Origin' '*';
		expires max;
		try_files $uri @discourse;
	}
	location @discourse {
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Host $http_host;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_redirect off;
		proxy_pass http://mage2_pro;
	}
	location @magento {
		# Внутри @location нельзя использовать директиву «alias».
		root /var/www/m2;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		# 2015-07-18
		# Обратите внимание, что:
		# 1) Значением по умолчанию параметра MAGE_RUN_TYPE является «store»:
		# https://github.com/magento/magento2/blob/1.0.0-beta/app/code/Magento/Store/Model/StoreManager.php#L98
		# 2) Magento с одинаковым успехом может загружать магазин
		# как при MAGE_RUN_TYPE = store, так и MAGE_RUN_TYPE = website,
		# просто при MAGE_RUN_TYPE = website Magento считает значение MAGE_RUN_CODE идентификатором сайта
		# а не магазина, загружает сайт, а затем загружает магазин по умолчанию у этого сайта:
		# http://magento.stackexchange.com/a/34215/18793
		#
		# Если в настройках Magento домен магазина указан на уровне сайта,
		# то и в настройках веб-сервера разумно в качестве значения MAGE_RUN_TYPE использовать website.
		fastcgi_param MAGE_RUN_TYPE website;
		fastcgi_param MAGE_RUN_CODE mage2_pro;
		include fastcgi_params;
		fastcgi_pass 127.0.0.1:9000;
	}
}
1 лайк