CSS和JS文件的404-子目录中带有Laravel的Nginx

时间:2019-03-22 02:11:33

标签: php laravel nginx webserver ubuntu-16.04

我的CSS和JS文件显示为404 /未找到文件。显示的是“ Laravel 404”页面,而不是显示的“ Nginx 404”页面,这使我认为这可能是Laravel问题,但我不确定。我的网站其余部分和子目录中的Laravel应用程序都正常运行。

我让Nginx从默认根目录/

提供一个常规PHP网站(PHP-FPM)。

我也有Nginx从/ todos /

提供Laravel应用

但是/ todos /(Laravel应用程序)下的图像都显示为404。文件系统位置分别为/ todos / public / css /和/ todos / public / js/。

我猜这是一个Nginx问题,但是我不确定。这可能是Laravel问题。我需要在/routes/web.php中为Laravel中的CSS和JS文件设置一个路由吗?

这是一个非常漂亮的Bitnami Ubuntu安装。

这是我的Nginx配置文件:

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";

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";

/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 个答案:

答案 0 :(得分:0)

好吧,我最终添加了以下位置块,并且有效,结果发现alias是诀窍:

    location /todos/css/ {
            alias /opt/bitnami/nginx/html/todos/public/css/;
    }
    location /todos/js/ {
            alias /opt/bitnami/nginx/html/todos/public/js/;
    }