![](/img/trans.png)
[英]How to fix error 404 with app.js and app.css in Laravel?
[英]Laravel 8 + nginx - app.css and app.js resources from public/ not loading - 404 not found
這似乎是一個重復的問題,但它是不同的。相信我,我為此研究了整個堆棧、laracast、reddit 和 github
我在帶有
nginx
的ubuntu VM
上有一個Laravel
應用程序。 我的 Laravel 8 應用程序的問題是它沒有從public/
加載app.css
和app.js
文件。 我已經運行了npm install & npm run dev/prod
但我仍然收到 404 錯誤 - 在 chrome 控制台中找不到。 所以我的app.css and.js
和 .js 在我的public/css - /js
文件夾中仍然存在並且運行良好。資源/資產的路徑也正確生成。仍然無法正常工作。 我嘗試了幾種server block
配置,但均未成功。如果您有同樣的問題並嘗試
npm install and run dev
,使用asset
助手 function 生成path
,<script src="{{ asset('/js/app.js') }}" defer></script>
和<link href="{{ asset('/css/app.css') }}" rel="stylesheet">
它仍然無法正常工作,請檢查下面的答案,*它可能會為您節省我花費的 1 周搜索。*強調文本
如果您的配置與我相同,那么解決方案非常簡單。
I installed my Laravel app in the /home/myUsername/myLaravelAppName
with a symlink for index.php in /var/www/myLaravelSiteName
- /var/www/myLaravelSiteName being the default nginx root for serving websites - as you know.
解決方案:也為每個 /public/css/ 和 /public/js/ 目錄創建一個符號鏈接到/var/www/myLaravelSiteName ,因為index.php
符號鏈接也不為/public/
中的 css 和 js 文件夾提供服務。顯然這對我來說不是那么明顯。
或者您可以將整個 /public/ 目錄符號鏈接到/var/wwwmyLaravelSiteName
。
如果這仍然不起作用,您可能還有其他問題,因此請繼續搜索,因為這些問題還有其他修復程序。 一個快速但“骯臟”的修復方法是使用引導程序中的在線CDN 鏈接並將它們添加到app.blade.php
中。 它會起作用,但不建議這樣做。
此外,您添加的任何新的自定義 css/js 文件都必須加載,因此首選正確的運行方式。 如果單獨提供而不是與整個 public/ 目錄一起提供,這些將必須有符號鏈接。
PS。 不要忘記將符號鏈接從站點可用到站點啟用。
我的工作服務器塊:
server {
listen 80;
listen [::]:80;
root /var/www/laravel;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
index index.php index.html index.htm ;
server_name laravel www.laravel;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri = 404;
}
location ~* \.(jpg|jpeg|png|gif|css|js|ico|html)$ {
access_log off;
expires max;
log_not_found off;
}
location ~/\.ht {
deny all;
}
sendfile off;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.