404 для файлов CSS и JS - Nginx с Laravel в подкаталоге - PullRequest
0 голосов
/ 22 марта 2019

Мои файлы CSS и JS отображаются как 404 / Файл не найден.Это страница "Laravel 404", а не страница "Nginx 404", которая показывается, что заставляет меня думать, что это может быть проблемой Laravel, но я не уверен.Остальная часть моего сайта и приложение Laravel в подкаталоге работают нормально.

У меня есть Nginx, обслуживающий обычный веб-сайт PHP (PHP-FPM) из корневого каталога по умолчанию в /

У меня также есть Nginx, обслуживающий приложение Laravel из / todos /

Но все изображения из / todos / (приложение Laravel) отображаются как 404. Расположение файловой системы / todos / public / css /и / todos / public / js / соответственно.

Я предполагаю, что это проблема Nginx, но я не уверен.Это может быть проблемой Laravel.Нужно ли устанавливать маршрут в /routes/web.php для файлов css и js в Laravel?

Это довольно ванильная установка 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 голосов
/ 26 марта 2019

Ну, я закончил тем, что добавил следующие блоки местоположения, и это сработало, оказалось, что alias был уловкой:

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