1

I have installed laravel with Nginx using Ubuntu. Everything is working fine so far except for one problem. When a user insert any url like domain.com/user/whatever.php nginx response with 404 error page of its own instead of showing the laravel 404 page.

what am I missing in my nginx config?

my nginx config file:

server {
    listen 80;
    server_name ip domainFotcom wwwDotdomainDotcom;
    return 301 https://domainDotcom$request_uri;
}

server {
    listen 443 ssl http2;
    server_name ip wwwDotdomainDotcom;
    return 301 $scheme://domainDotcom$request_uri;
}


# Default server configuration
#
server {
    #listen 80 default_server;
    #server_name ip domainDotcom wwwDotdomainDotcom;

    #listen [::]:80 default_server;

    # SSL configuration
    #
      listen 443 ssl default_server;
listen [::]:443 ssl default_server;


root /var/www/domain/public;

    # Add indexDotphp to the list if you are using PHP
    index indexDotphp indexDothtml indexDothtm indexDotnginx-debianDOthtml;

server_name domainDotcom;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
             try_files $uri $uri/ /indexDotphp?$query_string;

    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
     location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }

    location ~ /.well-known {
            allow all;
    }
}

3 Answers 3

4

As I understand it, if a file ends with ".php" nginx tries to send it to php engine. Then, if the file doesn't exist, php engine throw 404 at nginx-level. You should catch and redirect it to php engine again:

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

    root /var/www/path/public;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name domain.com www.domain.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri @missing;

        # regex to split $uri to $fastcgi_script_name and $fastcgi_path
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        # Bypass the fact that try_files resets $fastcgi_path_info
        # see: http://trac.nginx.org/nginx/ticket/321
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;

        include fastcgi.conf;
    }

    location @missing {
        rewrite ^ /error/404 break;

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

} 
Sign up to request clarification or add additional context in comments.

Comments

-1

Use the pretty URLs nginx config from the Laravel docs:

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

Source: https://laravel.com/docs/5.4/installation#pretty-urls

You also need to ensure you have a 404 error page set up under resources/views/errors/404.blade.php, as mentioned in the docs:

Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php. This file will be served on all 404 errors generated by your application.

Read further here: https://laravel.com/docs/5.4/errors#http-exceptions

4 Comments

as you can see in the config code of my file I have the location / { try_files $uri $uri/ /index.php?$query_string; } is set. I also have a 404 page in my laravel app.
What is the full file path of the 404 page?
it's in the views/errors/404.blade.php the file works fine for all routes except when a route ends with whatevername.php
So basically whenever you append .php to the end of a URL and hit it in the browser it appears it's not even reaching laravel, otherwise it would throw the http exception and return the 404. So, your issue is with nginx. Try stripping down your nginx config to the absolute minimum required (i.e. what is in my answer) and then incrementally adding the other parts that you need that are included in your question.
-1

replace this

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

with

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

after that run

sudo service nginx restart

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.