【发布时间】:2020-05-09 11:14:40
【问题描述】:
所以,在过去的几个小时里,我一直遇到麻烦,以至于我用尽了我尝试过的所有解决方案。我的 JS 和 CSS 文件返回 404 请求(Laravel 端)。
- NGINX 配置
server {
root /var/www/myapp/public;
server_name subdomain.myapp.com;
index index.htm index.php;
location / {
try_files $uri $uri/ /index.php?query_string;
}
# Added afterwards and still did not fix my issue.
location /public/css {
alias /var/www/html/myapp/public/css/;
}
location /public/js {
alias /var/www/html/myapp/public/js/;
}
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$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/subdomain.myapp.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/subdomain.myapp.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
}
server {
if ($host = subdomain.myapp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name subdomain.myapp.com;
listen 80;
return 404; # managed by Certbot
}
我还做了以下事情:
npm run prod 编译我的所有文件。我发现奇怪的一件事是,为什么我的 /css/app.css 的块名称为 /js/app
Asset Size Chunks Chunk Names
/css/app.css 141 KiB 0 [emitted] /js/app
/js/app.js 798 KiB 0 [emitted] [big] /js/app
/js/app.js.LICENSE.txt 2.52 KiB [emitted]
除此之外,我还清除了 Laravel 的缓存
php artisan config:cache
还有来自 layouts 文件夹的 app.blade.php:
<head>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
<!-- Styles -->
<link rel="stylesheet" href="{{ asset('css/app.css') }}" />
</head>
【问题讨论】:
-
您的基本网址是否正常工作?当你输入你的基本网址时,你看到什么了吗?
-
基本 url 正指向正确的位置。只给我 404。
-
硬编码路径是否像 src="/js/app.js" 或 src="//mysite.com/js/app.js" 一样工作。
-
我找到了解决问题的方法。它在 nginx 文件中。