dousuo2812 2019-03-21 18: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 09: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/;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 为什么树莓派5b显示禁止连接
  • ¥20 流量太费!寻找便宜的app音视频SDK或平替方案。
  • ¥15 kubeasz部署遇到问题
  • ¥15 GUIDE to App Designer Migration Tool for MATLAB
  • ¥50 第三代非支配排序遗传算法(NSGA-Ⅲ)和多目标粒子群优化算法(MOPSO)的实现
  • ¥20 plant simulation与python com接口实时数据交互
  • ¥15 有关汽车的MC9S12XS128单片机实验
  • ¥15 求c语言动态链表相关课程有偿,或能将这块知识点讲明白
  • ¥15 FLKT界面刷新异常
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部