[英]Laravel doesn't read css and js files (nginx)
我正在尝试在 Docker for Windows 上使用 laravel 7 和 nginx 制作一个应用程序。
我成功地在公共文件夹中显示 laravel 的首页,但它从未读取公共文件夹中的 css 和 js 文件并显示 404 错误。 404错误页面有时是nginx的,有时是laravel的。 (我将在下面向您展示的代码显示了laravel之一)
我怀疑这个问题与我的 nginx 配置有关,并尝试了很多方法但没有任何效果。
这是我的 Laravel 的目录结构。
blog
-public
--index.php
--css
---index.css
--js
---jquery-3.4.1.min.js
这是我的 nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$uri - $is_args - args :::'
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
这是我的 default.conf。 注释掉的代码是我尝试过的。
server {
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/blog/public;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
#location ~ \.(css|js)$ {
# root /var/www/html/blog/public;
# try_files $uri $uri/ =404;
#}
#location /css/ {
# alias /var/www/html/blog/public/css/;
#}
#location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
# root /var/www/html/laravel/public;
# expires max;
# add_header Cache-Control public;
# access_log off;
#}
#location ~ \.css {
# add_header Content-Type text/css;
#}
#location ~ \.js {
# add_header Content-Type application/x-javascript;
#}
}
我已经被这个问题困住了两天,所以任何建议都是有帮助的。
编辑:
这就是 access.log 所说的:
172.24.0.1 - - [31/Mar/2020:10:36:09 +0000] "GET /index.php HTTP/1.1" 200 443 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
172.24.0.1 - - [31/Mar/2020:10:36:10 +0000] "GET /js/jquery-3.4.1.min.js HTTP/1.1" 404 1564 "http://localhost/index.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
172.24.0.1 - - [31/Mar/2020:10:36:10 +0000] "GET /css/index.css HTTP/1.1" 404 1564 "http://localhost/index.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
error.log 没有说明。
20200401
我将 default.conf 的根目录更改为 /var/www/html 并更改目录结构以找出此问题是否是 laravel 的问题。 以下文件用作原始 php 应用程序。 index.php 尝试读取 index.css。
html
-index.php
-css
--index.css
它读取 index.css 但不改变样式。 我在 index.php 上的标签上写了与 index.css 相同的 css,以确保语法是否正确,并且它正确地改变了样式。 此外,它在 Chrome 上显示以下错误:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost/index.css".
index.css 在我的编辑器上是这样的:
h1{
text-align: center;
}
但是在浏览器上,index.css 和 index.php 是一样的:
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Sapphire test</title>
<link rel="stylesheet" href="css/test.css">
</head>
<!--<style>-->
<!-- h1{-->
<!-- text-align: center;-->
<!-- }-->
<!--</style>-->
<body>
<header>
<h1>Sapphire test</h1>
</header>
<main>
<p>Hello World</p>
</main>
<script>
</script>
<footer>
</footer>
</body>
</html>
这太令人困惑了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.