Magento 2 Nginx Configuration – How to Configure Nginx for Magento 2

magento2nginx

enter image description hereMy actions: I install only: nginx 1.10.1, php7 and mysql 5.6 on ubuntu 14.04.
php install: sudo apt-get install -y php7.0-fpm php7.0-common php7.0-gd php7.0-mysql php7.0-mcrypt php7.0-curl php7.0-intl php7.0-xsl php7.0-mbstring php7.0-zip php7.0-bcmath php7.0-iconv php7.0-opcache php7.0-soap php7.0-json php7.0-xml
I copy sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/agestor.com.
In agestor.com paste:

upstream fastcgi_backend {  
    server unix:/var/run/php/php7.0-fpm.sock;
}

server {  
    listen 443 ssl;
    server_name agestor.com;
    set $MAGE_ROOT /var/www/agestor.com/html;
    set $MAGE_MODE default;
    include /var/www/agestor.com/html/nginx.conf.sample;
}

server {  
    listen 80;
    server_name agestor.com;
    set $MAGE_ROOT /var/www/agestor.com/html;
    set $MAGE_MODE default;
    include /var/www/agestor.com/html/nginx.conf.sample;
}  

And include him: sudo ln -s /etc/nginx/sites-available/agestor.com /etc/nginx/sites-enabled/
But not install magento 2.

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

error.log: 2016/06/21 14:21:35 [error] 13909#13909: *46 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 37.146.123.141, server: agestor.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "www.agestor.com"
I set up my VPS on this article: magento 2 for ubuntu 14.04

Best Answer

Magento2 contains NGINX config nginx.conf.sample that can be included in you server section. Let's try with minimal configuration;

You need to modify /etc/nginx/sites-available/my-domain.com with the following content:

upstream fastcgi_backend {
    # socket
    # server unix:/var/run/php5-fpm.sock;
   server   unix:/var/run/php/php7.0-fpm.sock;
    # use tcp connection
    #  server  127.0.0.1:9000;
   
}
server {
    listen 80;
    server_name www.my-domen.com;
    return 301 $scheme://www.my-domen.com$request_uri;
}

server {
    listen 80 reuseport;
    server_name my-domen.com;

    set $MAGE_ROOT /var/www/my-domen.com/html;

   set $MAGE_MODE developer;
#    set $MAGE_MODE default;
#    set $MAGE_MODE production;

    include /var/www/my-domen.com/html/nginx.conf.sample;
    fastcgi_read_timeout 3000;
}

and then add other settings do not relate to Magento