Nginx on Ubuntu Server throws 404 not found

10,249

Let me answer my question.

All the configs are fine - it turned out that the autoloader is not working. See in the error log nginx didn't redirect http requests to the right directories. It should be working fine, but I guess it's polluted somehow during rebooting.

Solution:
1. in the remote server: composer install
2. if not working, simply restore the entire server and boot again. It's a booting problem anyway.

Share:
10,249

Related videos on Youtube

Stanley Luo
Author by

Stanley Luo

Full stack web Developer React + React Native + Node + AWS

Updated on June 04, 2022

Comments

  • Stanley Luo
    Stanley Luo almost 2 years

    After rebooting the laravel-based website on a Ubuntu server using nginx, and configured everything properly (hopefully, at least it worked before), I can only access to the index page but not any other page - nginx keeps throwing 404 not found.

    Thought it could be permission issue but I've already tried

    sudo chown -R :www-data /var/www/foo-bar sudo chmod -R 775 /var/www/foo-bar/storage sudo chmod -R 775 /var/www/foo-bar/resources

    But seems not helping. And here is my nginx config file:

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        root /var/www/foo-bar/public;
        index index.php index.html index.htm;
    
        server_name 120.25.203.113;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            try_files $uri /index.php =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;
        }
    }
    

    Any idea? (Try http://120.25.203.113/ and click on the center button to have a look if interested!)

    Update: the error log shows

    [error] 24119#0: *77 open() "/var/www/ozunimate/public/student/register" failed (2: No such file or directory), client: 14.202.230.9, server: 120.25.203.113, request: "GET /student/register HTTP/1.1", host: "120.25.203.113", referrer: "http://120.25.203.113/"
    

    "student/register" is not under /public and should be redirected (it used to be redirected normally before rebooting). Seems redirect not working anymore.

    • Joseph
      Joseph about 8 years
      Can you update your question with a copy of your /etc/nginx/sites-enabled/default file (assuming you haven't created a different vhost file, in which case post that instead).
    • Stanley Luo
      Stanley Luo about 8 years
      Sure, edited. @Joseph
    • Joseph
      Joseph about 8 years
      Your config looks the same as mine and mine works fine on Laravel 5. Are you using Laravel 5 or 4? Anyway, I've upvoted your question so hopefully someone else will notice it.
    • Stanley Luo
      Stanley Luo about 8 years
      I'm using Laravel 5. It worked before, after rebooting somehow it throws 404 error.. Thanks man for upvote!
    • semm0
      semm0 about 8 years
      you wrote the command 'sudo chown -R :www-data /var/www/foo-bar' but in your config the root is 'root /var/www/foo-bar/public;'. Are permission correct for the public folder? because 404 not found looks like nginx can't find the desired path. Can you enable error logging and check what's written to it?
    • Stanley Luo
      Stanley Luo about 8 years
      @semm0 Nah that should be OK - in Laravel projects the index page is under /public, and with -R the sub directories are also affected. The error log showed it's a problem that the views file weren't redirect to resources/views - and I am trying to figure it out