Nginx setup for laravel project, which has routes with ".php" endings

Viewed 368

I'm trying to setup my Laravel project using Nginx. My /etc/nginx/conf.d/default.conf is:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;
    location / {
        try_files $uri /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

I have a problem with routes with ".php" endings, e.g.

Route::get('modules.php', 'ModuleController@index');

Instead of going to index.php and looking there the route, the server tries to open file modules.php, which doesn't exist. I know, that problem in nginx settings, but I don't have experience with it, so I can't fix it myself.

2 Answers

Add a try_files statement to the second location block.

For example:

location / {
    try_files $uri /index.php?$args;
}
location ~ \.php$ {
    try_files $uri /index.php?$args;

    fastcgi_pass app:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Look through this page https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04

Here you can find suitable configuration and description. For example:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/laravel/public;
    index index.php index.html index.htm;

    server_name server_domain_or_IP;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
Related