-1

I try to enable PHP for only one subdirectory (the laravel directory) but I didn't manage to get this working. NGINX is always saying 404 File not found or php says "no input file specifed". What am I doing wrong?

This is my location config:

        location /laravel {
        root   html/laravel/public;
        index   index.php index.html index.html;
        try_files   $uri $uri/ /index.php?$query_string;

        location ~ \.php$ {
            root   html/laravel/public;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  document_root$fastcgi_script_name;
            #include        fastcgi_params;
            include fastcgi.conf;
         }
     }

UDPATE 1: It seems that nginx does not properly evaluate my location expressions:

2018/09/12 16:30:44 [error] 26476#24408: *1 CreateFile() "C:/Server/nginx/html/index.php" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /laravel/ HTTP/1.1", host: "localhost"

This is the wrong path and at least the root of the / location:

   location / {
        root   C:/Server/nginx/html;
        index  index.html index.htm index.php;
    }

I tried to move the block but nothing changes.

UPDATE 2: It seems that nginx is very buggy. The documentation states:

Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.

As my error log shows, The try_files directive does not respect the root path because it trys to open the file relative to another location block.

10
  • You are missing the $ in $document_root. Also, you are passing uncontrolled requests to PHP. Commented Sep 12, 2018 at 13:14
  • This doesn't change anything. If I open localhost/larvael a 404 error occurs and if i directly open index.php the interpreter says no input file specified. Commented Sep 12, 2018 at 14:18
  • The URI at the end of the try_files statement should be /laravel/index.php. You currently have /index.php which will be processed by the location / block, which explains the error log entry in your EDIT. Commented Sep 12, 2018 at 14:51
  • I've changed that, now the 404 error disappears but php keeps complaining that there is no input file. Commented Sep 12, 2018 at 14:53
  • 1
    Also, where are your files? In /some/path/to/html/laravel/public/laravel/index.php? Should you be using alias instead of root? See my answer here; Commented Sep 12, 2018 at 14:55

2 Answers 2

3

As @Richard pointed out in the linked Stackoverflow Thread, this seems to be a bug of nginx. For me this solution works with nginx:

        location  /laravel {
        alias html/laravel/public;
        index   index.php index.html index.html;
        try_files  $uri $uri/  @nested;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include fastcgi.conf;
            fastcgi_index   index.php;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
            #include        fastcgi_params;
         }
     }

     location @nested {
        rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
     }

Source: https://serversforhackers.com/c/nginx-php-in-subdirectory

Sign up to request clarification or add additional context in comments.

Comments

1

may be some changes and updates you need to apply:

If you want to put your laravel project in a subfolder on a server with ngnix-ubuntu 16-php.7.2, so here is update ngnix config :

1) your nested(subfolder) isn't inside your main folder

/var/www/main:
/var/www/nested:

then your config :

location /nested {

        alias /var/www/nested/public;

        try_files $uri $uri/ @nested;

               location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                                }
   }

location @nested {
        rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}

2) your laravel-test folder (subfolder) inside your main :

/var/www/main:
/var/www/main/nested:

then your config :

location /laravel-test {

    alias /var/www/main/laravel-test/public;

    try_files $uri $uri/ @laravelTest;

           location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $request_filename;
                    fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                            }


  }

location @laravelTest {
        rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;
}

1 Comment

This looks like a replication of my solution with multiple laravel instances. What's new about this configuration in contrast to mine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.