Waizujin's avatar

Routes other than "/" are not working running nginx

Alright so I'm sure it's an nginx config issue, most likely. But the issue is I just installed Laravel on a fresh server and I can access the "/" route, but not the "/home" route. (Sidenote: If I can't post this here, I'm sorry. Just never felt the Stack Overflow community was very welcoming...)

This is my /etc/nginx/sites-available file contents

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

    root /home;
    index index.php index.html;

    server_name [server ip address];

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

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

I'm using nginx + php5-fpm for my webserver. If you need more details than what is included here, please let me know and I can grab the config for them as well.

0 likes
9 replies
bashy's avatar
bashy
Best Answer
Level 65

root = root of your laravel public directory.

Change that to where ever your Laravel is installed (+ /public) and it will work.

Example

root /home/web/sites/laravel/public;

Don't forget to reload or restart Nginx.

sudo service nginx restart
// or
sudo nginx -s reload

P.s. Stack Overflow is like a boarding school

3 likes
Waizujin's avatar

Thanks @bashy I honestly didn't think that was the issue. What causes that to happen? I had left it open for now, because I was going to have a lot of projects on this one server. I can certainly just attach them to other IP's, but I'd have liked to leave it open for now, since it's all for development.

bashy's avatar

Not sure where you got your config from but I normally set that myself (or at least check it). Just think, root is where Nginx will serve the website from. So example.com/ would show files from the root directory.

If you want to host multiple, just add a new vhost (server block) and adjust the settings inside to reflect the new directory of your site. No need for additional IPs.

Also, welcome to follow my blog. I post Nginx stuff https://bashy.im/blog

Waizujin's avatar

@Bashy Well I have my projects all within home. So I navigated to the public directory myself. (/home/Personal/Laravel/public) and other projects would be inside /Personal and /Work.

bashy's avatar

Not the best way to load a Laravel project, you'll find you'll get a lot of issues with links and paths.

You can keep one main open URL and ones for Laravel installs. I have my own PHP script that adds the file and paths into a config then loads it into Nginx and restarts the service. I route all subdomains to the IP and they instantly work.

1 like
Waizujin's avatar

@bashy Alright, I understand that. I was just curious mostly. I do appreciate your help!

chilly2go's avatar

not sure if I should start a new discussion or continue here. I got the same problem. I am running nginx on Raspbian GNU/Linux 8.0 (jessie) (fresh install) opening the site (local ip) results in a normal page. clicking any link give me a 404. adding

/index.php?$query_string

to

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

results in downloads of the index.php file instead of php execution (which seems strange)

any ideas? better switch to apache? reinstall? want to view config files?

wolvorine's avatar

Try below

Replace the

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

with

try_files $uri $uri/ /index.php$is_args$args;

& restart the nginx

Sanjaykarki's avatar

I have made one application in laravel and used sql server in it.My sql server have 2 different databases with different username and password.I have used one of database in my application.....All tables are same in 2 different databases.I want to use that non use database in same appplication.......But there is a problem....

The error says "This page isn’t working

localhost is currently unable to handle this request. HTTP ERROR 500 "

I have just copied that runnable project and rename it...I have made change in .env files and database.php.....But it does not works.........Does any one have solution.......Plz help me out!!!!

Please or to participate in this conversation.