H.R. Shadhin
H.R. Shadhin

Reputation: 166

Nginx alias with laravel4

My nginx configuration for laravel

 server {
        listen          80;
        server_name     app.dev;
        rewrite_log     on;
        root            /var/www/l4/angular;
        index         index.html;

        location /{
        # URLs to attempt, including pretty ones.
            try_files   $uri $uri/ /index.php?$query_string;

        }
        location /lara/ {
          index index.php;
          alias /var/www/l4/public/;
        }
        # Remove trailing slash to please routing system.
        if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
        }
        location ~ ^/lara/(.*\.php)$ {
              alias /var/www/l4/public/$1;
                      fastcgi_pass unix:/var/run/php5-fpm.sock;
                      fastcgi_index index.php;
                      fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                      include fastcgi_params;
        }
    }

Laravel route:

Route::get('/', function()
{
    return View::make('index');
});

Route::get('x', function()
{
    return "alpha";
});

my problem is,"http://app.dev/lara/index.php" is working but "http://app.dev/lara" and lara/x is not working.

Upvotes: 1

Views: 729

Answers (1)

Ahmad Sherif
Ahmad Sherif

Reputation: 6223

In a nutshell, make the following edits. An explanation of why is below.

Replace

try_files   $uri $uri/ /index.php?$query_string;

with

try_files   $uri $uri/ /lara/index.php?$query_string;

Replace the last location directive with this

location ~ /lara/(.*)$ {
                  fastcgi_pass unix:/var/run/php5-fpm.sock;
                  fastcgi_index index.php;

                  include fastcgi_params;
                  fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";
                  fastcgi_param REQUEST_URI /$1;
}

Restart nginx.

Now the why. I spotted a couple of mistakes with your nginx config. First, /index.php?$query_string in the try_files directive should be /lara/index.php?$query_string, otherwise nginx will try a request like http://app.dev/lara as /var/www/l4/angular/index.php?, which leads no where (unless you have an index.php there, and even is it will be served as text, not through fpm).

The second has to do with the location ~ ^/lara/(.*\.php)$ directive. I think restricting it to URIs that end with .php is wrong, because it won't work for http://app.dev/lara/x, which will make nginx only search for /var/www/l4/public/x, returning 404 of course. Changing the regex to ^/lara/(.*)$ should do the job of catching /lara/x. Now the fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; directive is erroneous because for http://app.dev/lara/x, SCRIPT_FILENAME is /var/www/l4/public/x/lara/x, and removing the $1 in the alias directive won't make it any better. Instead, make the fastcgi_param like this fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";, remove the alias directive, it's useless now, then move include fastcgi_params; above the fastcgi_param so it won't override SCRIPT_FILENAME value.

Done? Not yet :). Trying /lara/x will show a Laravel routing error, because it tries to find the route lara/x instead of x, this is because you're including fastcgi_params. Just add fastcgi_param REQUEST_URI /$1; after SCRIPT_FILENAME param directive. Now it should be working fine. Don't forget to restart nginx :).

Upvotes: 5

Related Questions