dousuo2812 2019-03-22 02:11
浏览 437
已采纳

404用于CSS和JS文件 - Nginx与Laravel在子目录中

My CSS and JS files are showing up as 404 / File Not Found. It is the "Laravel 404" page, not the "Nginx 404" page that is being shown, which makes me think it could be a Laravel issue, but I'm not sure. The rest of my site and the Laravel app in the sub-directory are working fine.

I have Nginx serving a regular PHP web site (PHP-FPM) from the default root at /

I also have Nginx serving a Laravel app from /todos/

But the images from under /todos/ (the Laravel app) are all showing up as 404. The file system location is /todos/public/css/ and /todos/public/js/ accordingly.

I'm guessing this is an Nginx issue, but I'm not sure. It might be a Laravel issue. Do I need to set a Route in /routes/web.php for css and js files in Laravel?

This is a pretty vanilla Bitnami Ubuntu install.

Here are my Nginx config files:

Contents of nginx.conf:

user  daemon daemon;
worker_processes  auto;

error_log  "/opt/bitnami/nginx/logs/error.log";

pid        "/opt/bitnami/nginx/logs/nginx.pid";

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    client_body_temp_path  "/opt/bitnami/nginx/tmp/client_body" 1 2;
    proxy_temp_path "/opt/bitnami/nginx/tmp/proxy" 1 2;
    fastcgi_temp_path "/opt/bitnami/nginx/tmp/fastcgi" 1 2;
    scgi_temp_path "/opt/bitnami/nginx/tmp/scgi" 1 2;
    uwsgi_temp_path "/opt/bitnami/nginx/tmp/uwsgi" 1 2;

    access_log  "/opt/bitnami/nginx/logs/access.log";

    sendfile        on;

    keepalive_timeout  65;
    gzip on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_vary on;
    gzip_types text/plain
           text/xml
           text/css
           text/javascript
           application/json
           application/javascript
           application/x-javascript
           application/ecmascript
           application/xml
           application/rss+xml
           application/atom+xml
           application/rdf+xml
           application/xml+rss
           application/xhtml+xml
           application/x-font-ttf
           application/x-font-opentype
           application/vnd.ms-fontobject
           image/svg+xml
           image/x-icon
           application/atom_xml;

gzip_buffers 16 8k;

add_header X-Frame-Options SAMEORIGIN;

ssl_prefer_server_ciphers  on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS;

include "/opt/bitnami/nginx/conf/bitnami/bitnami.conf";

Contents of bitnami.conf:

# HTTP server
server {
    listen       80;
    listen   [::]:80 default_server ipv6only=on;

    server_name  localhost;
    return 301 https://$host$request_uri;

    location / {
        root   /opt/bitnami/nginx/html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
    }

    ## Begin - Security
    # deny all direct access for these folders
    location ~* /(\.git|cache|bin|logs|backup|tests)/.*$ { return 403; }
    # deny running scripts inside core system folders
    location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
    # deny running scripts inside user folder
    location ~* /user/.*\.(txt|md|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
    # deny access to specific files in the root folder
    location ~ /(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt|\.htaccess) { return 403; }
    ## End - Security

    include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";
    include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}

# HTTPS server
server {
   listen       443 ssl http2;
   listen [::]:443 default ipv6only=on;

   server_name  localhost;

   ssl_certificate      server.crt;
   ssl_certificate_key  server.key;

   ssl_session_cache    shared:SSL:1m;
   ssl_session_timeout  5m;

   ssl_ciphers  HIGH:!aNULL:!MD5;
   ssl_prefer_server_ciphers  on;
   add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

   location / {
       root   /opt/bitnami/nginx/html;
       index  index.php index.html index.htm;
       try_files $uri $uri/ /index.php?$query_string;
   }

    location /todos {
        try_files $uri $uri/ /todos/index.php?$query_string;
        index  index.php index.html index.htm;
        root /opt/bitnami/nginx/html/todos/public/;
        location ~ \.php$ {
            fastcgi_index index.php;
            fastcgi_read_timeout 300;
            fastcgi_pass   unix:/opt/bitnami/php/var/run/www.sock;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_param SCRIPT_FILENAME /opt/bitnami/nginx/html/todos/public/index.php;
            fastcgi_param QUERY_STRING $query_string;
            include        fastcgi_params;
        }
    }

    ## Begin - Security
    # deny all direct access for these folders
    location ~* /(\.git|cache|bin|logs|backup|tests)/.*$ { return 403; }
    # deny running scripts inside core system folders
    location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
    # deny running scripts inside user folder
    location ~* /user/.*\.(txt|md|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
    # deny access to specific files in the root folder
    location ~ /(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt|\.htaccess) { return 403; }
    ## End - Security

   include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";
   include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}
include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-vhosts.conf";

Contents of /opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf:

location ~ \.php$ {
    root           html;
    fastcgi_read_timeout 300;
    fastcgi_pass   unix:/opt/bitnami/php/var/run/www.sock;
    fastcgi_index  index.php;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    include        fastcgi_params;
}
  • 写回答

1条回答 默认 最新

  • dsf1222 2019-03-26 17:58
    关注

    Well, I ended up adding the following location blocks and it worked, turns out alias was the trick:

        location /todos/css/ {
                alias /opt/bitnami/nginx/html/todos/public/css/;
        }
        location /todos/js/ {
                alias /opt/bitnami/nginx/html/todos/public/js/;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集