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

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
    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/;
        }
    
    点赞 评论

相关推荐