Nginx – Ubuntu + Nginx: directory index of “/var/www/app/the-app/” is forbidden

nginxUbuntu

I keep hitting this error when trying to load up my Laravel application on an Ubuntu+Nginx server.

The user should visit app.example.com/my-app and it should load the contents of /var/www/app/my-app/public/index.php

[error] 9028#0: *15001 directory index of "/var/www/app/my-app/" is
forbidden, client: xxx.xxx.xxx.xx, server: app.example.com, request:
"GET /my-app/ HTTP/1.1", host: "app.example.com"

My nginx config is as follows:

server
{
  server_name app.example.com www.app.example.com;
  root /var/www/app;
  index index.php index.html index.htm;

  location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
  }

   location ~ \.php$
   {
        #fastcgi_pass unix:/dev/shm/php-fpm-www.sock;
        fastcgi_pass 127.0.0.1:7777;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 8 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
   }

   location ~ /\.ht
   {
        deny all;
   }

   location /my-app/{
        alias /var/www/app/my-app/public/;
        try_files $uri $uri/ /public/index.php?$query_string;

        location ~ \.php$ {
           try_files $uri /index.php =404;
           fastcgi_split_path_info ^(.+\.php)(/.+)$;
           #fastcgi_index   index.php;
           #fastcgi_pass    unix:/var/run/php5-fpm.sock;
           fastcgi_pass 127.0.0.1:7777;
           fastcgi_index index.php;
           include         fastcgi_params;
           fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
  }

}

I've tried the following:

  • Set permissions chown -R www-data /var/www/app/my-app which didn't make any difference
  • Adjusting the alias and try_file directories which didn't change the error or assist

Best Answer

The error is due to the $uri/ part of the try_files clause which instructs nginx to serve the content of the directory /var/www/app/my-app/. Directory listing is forbidden by default, you can enable it by adding autoindex on; to the location.

It's seems like the file /var/www/app/my-app/public/index.php does not exist.