STEN
STEN

Reputation: 443

Nginx always redirect Laravel routes to default root index.php page

I'm a new Laravel learner. On my Mac (macOS 10.13), I have Nginx, PHP, and MySQL environment configured. At first, the Nginx localhost:8080/laravel/public displays the laravel welcome page without any issue. But when I try to add custom routes such as:

Route::get('test', function () {
    return 'Hello World!';
});

I got 404 page on localhost:8080/laravel/public/test.

After I Googled the solution for route 404 issue, I modified my nginx conf file by adding

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

as below:

server {
    ...

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

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Now is the story, the 404 issue has be fixed. But I still cannot access the correct route page.

When I open localhost:8080/laravel/public/test again, the browser takes me to the nginx docroot page (that is, localhost:8080/index.php).

I have tried php artisan serve in my laravel home folder, using that command localhost:8000/test can be accessed correctly with "Hello World!" text.

Updated:

I just tried some other strings after localhost:8080/, such as localhost:8080/abc or something, it seems any subpath will takes me to the localhost:8080/index.php page! after try_files $uri $uri/ /index.php?$query_string was added. If I remove this line of code, localhost:8080/abc will show 404 page (which should be correct because I don't have abc.php file in the root folder).

It seems like after adding

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

Nginx redirects all the url it not recognized to the default homepage?

Can someone tell me why my Nginx cannot work with Laravel route after try_files $uri $uri/ /index.php?$query_string is added?

If you need more detail, please let me know. Thanks very much!

Upvotes: 1

Views: 5541

Answers (1)

STEN
STEN

Reputation: 443

Well.. I solved this issue by modifying my Nginx site config file, changing

server {

    ...

    root         /var/www;

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

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

to

server {

    ...

    root         /var/www/laravel/public;

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

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Yes, just change the root path to laravel's public folder then restart nginx server.

Now I got Hello World! on http://localhost:8008/test

Upvotes: 1

Related Questions