0

I have successfully installed a laravel application on a nginx server running on arch linux. However when I try to view the app in web browser all routes return 404 except the default '/'. I know this because I have make:auth but I can't reach the register and login page.

I followed instructions I found via Google and created two folders, sites-available and sites enabled in /etc/nginx/. I then created a config file for laravel app in /etc/nginx/sites-available called niko in sites available and symlinked to it like so ln --symbolic /etc/nginx/sites-available/niko /etc/nginx/sites-enabled/niko.

Here are the contents of my configuration file /etc/nginx/sites-available/niko.

server {
    listen       80;
    server_name  niko;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/niko/public;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            root /usr/share/nginx/html/niko/public;
            include fastcgi.conf;
    }
    }

In my /etc/nginx/nginx.conf I have this line just above the closing } of the http {} block:
include /etc/nginx/sites-enabled/*; When I check in the nginx error log(/var/log/nginx/error.log) I find this entries:

2019/06/14 10:59:22 [error] 5429#5429: *3 directory index of "/usr/share/nginx/html/niko/" is forbidden, client: 192.168.1.101, server>
2019/06/14 10:59:36 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 10:59:36 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 10:59:38 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 10:59:38 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 11:26:53 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 11:26:53 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 11:27:01 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), c>
2019/06/14 11:27:01 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), c>
2019/06/14 11:27:04 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), c>
2019/06/14 11:27:04 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), 

I have been pulling out my hair for hours because I don't know where to check. I have tried several things but nothing seems to work. Your support is appreciated.

UPDATE: My new /etc/nginx/sites-available/niko is here

server {
    listen 80;
    server_name niko;
    root /usr/share/nginx/html/niko/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
  }
}

UPDATE 1: namei -l /usr/share/nginx/html/niko/ gives:

f: /usr/share/nginx/html/niko/
drwxr-xr-x root root /
drwxr-xr-x root root usr
drwxr-xr-x root root share
drwxr-xr-x root root nginx
drwxr-sr-x kali root html
drwxr-sr-x kali root niko
Kha Kali
  • 169
  • 2
  • 13

1 Answers1

0

Laravel requires that you change your nginx.conf file a tiny bit for it to work properly.

Check out this example conf file that's available in the Laravel Documentation.

server {
    listen 80;
    server_name example.com;
    root /example.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
Linus Juhlin
  • 1,175
  • 10
  • 31
  • When I use this I now get 403 forbidden. – Kha Kali Jun 14 '19 at 09:17
  • Are you getting any useful information from the nginx logs? – Linus Juhlin Jun 14 '19 at 09:18
  • Did you just copy this example conf completely or did you tailor it to your own needs? – Linus Juhlin Jun 14 '19 at 09:19
  • The latest log is `2019/06/14 12:18:27 [error] 5509#5509: *3 directory index of "/usr/share/nginx/html/niko/" is forbidden,` – Kha Kali Jun 14 '19 at 09:20
  • I edited the root directive to point to my app public folder. then restarted nginx. – Kha Kali Jun 14 '19 at 09:21
  • Check out if this answer will help you on your way: https://stackoverflow.com/a/51879482/1308765 – Linus Juhlin Jun 14 '19 at 09:22
  • I have tried recommendations in the link and now I'm still stuck with the 404. It seems to me nginx takes the laravel routes without file extensions as folders. Because from the error log it complains of no such directory. `2019/06/14 12:27:52 [error] 5546#5546: *1 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory),` Do you have an idea how to let laravel translate the routes or how to fix this? Thanks mate. – Kha Kali Jun 14 '19 at 09:32
  • Could you update your question with your new nginx conf? – Linus Juhlin Jun 14 '19 at 09:35
  • Could you do `namei -l /usr/share/nginx/html/niko/` as well, so we got the permission out of the way? – Linus Juhlin Jun 14 '19 at 09:44
  • `f: /usr/share/nginx/html/niko/` `drwxr-xr-x root root /` `drwxr-xr-x root root usr` `drwxr-xr-x root root share` `drwxr-xr-x root root nginx` `drwxr-sr-x kali root html` `drwxr-sr-x kali root niko` – Kha Kali Jun 14 '19 at 09:46
  • I don't think your nginx server has root. You should have a look at changing those permissions. – Linus Juhlin Jun 14 '19 at 09:52
  • Thanks @Linus. which folder permission should I change? And what should I change them to? Thanks for your time. – Kha Kali Jun 14 '19 at 09:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194938/discussion-between-khakali-webdev-and-linus-juhlin). – Kha Kali Jun 14 '19 at 10:14