phpmacosnginxvirtualboxdebian-stretch

Nginx error : "Primary script unknown ", how can I resolve that ? Thank you


Config :

Nginx and php fpm have www-data as user. Dirs :

Config nginx : /etc/nginx/conf.d/default.conf

server {
    listen   80;
    server_name _;
    charset utf-8;

    location / {
       root /var/www/all/;
       try_files $uri /index.html index.php;
       }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
     include /etc/nginx/fastcgi_params;
      fastcgi_index index.php;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/run/php/php7.2-fpm.sock;
     fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

When i do this command : sudo ls -l /var/www/all/ , i get :

  • drwxrwx--- 1 root vboxsf temp_converter
  • drwxrwx--- 1 root vboxsf myproject

I want to show projects folders under /media/sf_web using Firefox (or another web browser) but it doesn't work. When i try to connect on this ip , nginx show "File not found" and in error log i see "Primary script unknow".

e.j :


Solution

  • You haven't set a global root statement, so Nginx will look for PHP files in the default root. You need to move the root statement from inside the location / block into server block scope.

    The try_files statement is completely wrong.

    Try:

    root /var/www/all/;
    
    location / {
        try_files $uri $uri/ /index.php;
    }
    ...
    location ~ \.php$ {
        try_files $uri =404;
    
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
    }