url \ whatever.php nginx 404错误而不是laravel 404错误

问题描述 投票:1回答:3

我使用Ubuntu在Nginx上安装了laravel。到目前为止,除了一个问题外,一切都很好。当用户使用自己的404错误页面插入任何类似domain.com/user/whatever.php nginx响应的URL而不是显示laravel 404页面时。

我的nginx配置中缺少什么?

我的nginx配置文件:

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;
    }
}
laravel nginx
3个回答
2
投票

据我了解,如果文件以“.php”结尾,nginx会尝试将其发送到php引擎。然后,如果文件不存在,php引擎会在nginx级别抛出404。您应该再次捕获并将其重定向到php引擎:

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

} 

0
投票

使用Laravel文档中的漂亮URL nginx配置:

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

资料来源:https://laravel.com/docs/5.4/installation#pretty-urls

您还需要确保在resources/views/errors/404.blade.php下设置了404错误页面,如文档中所述:

Laravel可以轻松显示各种HTTP状态代码的自定义错误页面。例如,如果要自定义404 HTTP状态代码的错误页面,请创建resources / views / errors / 404.blade.php。此文件将在您的应用程序生成的所有404错误中提供。

进一步阅读:https://laravel.com/docs/5.4/errors#http-exceptions


0
投票

替换这个

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

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

在那之后

sudo service nginx restart
© www.soinside.com 2019 - 2024. All rights reserved.