2017-07-24 57 views
0

我使用Ubuntu安裝了Nginx的laravel。除了一個問題,一切都很好。當用戶使用404錯誤頁面插入像domain.com/user/whatever.php nginx響應之類的任何url時,而不是顯示laravel 404頁面。url whatever.php nginx 404錯誤代替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; 
    } 
} 

回答

0

使用來自Laravel文檔漂亮的網址,nginx的配置:

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

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

你還需要確保你有一個404錯誤頁面設置在resources/views/errors/404.blade.php下,如文檔中所述:

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 ; } 設置。我的laravel應用程序中也有一個404頁面。 –

+0

404頁面的完整文件路徑是什麼? – haakym

+0

它在views/errors/404.blade.php文件中對於所有路由都能正常工作,除非路由以whatevername.php結尾 –

0

據我瞭解,如果一個文件以「.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; 
    } 

}