Question

Laravel 5.4 new project nginx redirecting to 404 on every route except index

  • Posted December 11, 2017
  • Nginx

I’m setting up new Laravel 5.4 project on a Ubuntu 16.04.3 x64 nginx machine. The index page works fine but when i try using any other route it takes me to a 404 page. In my development environment, the routes are working fine. I’m guessing it has to do with my server setup.

Below is my information for the server:

server {
        listen 80;
        listen [::]:80;

        root /var/www/dev.drmanansala.com/public;

        index index.php index;

        server_name dev.drmanansala.com;

        error_page 404 /404page.php;

        location / {
                try_files $uri $uri/ /$uri.php?$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

    listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/dev.drmanansala.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/dev.drmanansala.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}
Subscribe
Share

This comment has been deleted


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hello,

According to the official Laravel documentation the correct default configuration should be:

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;
    }
}

As far as I can see you would need to adjust your PHP-FPM part a little bit to include the extra fastcgi rules.

Hope that this helps! Regards, Bobby