Reputation: 362
We have a laravel project deployed on nginx. Some of css and js files cannot be accessed and returns 404 error. We have checked the routing and permissions several times. What could be the problem?
Update: This is the nginx config:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
gzip_static off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass xxx.xxx.xx.x:9000;
fastcgi_cache_background_update on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
There are some files that seem nginx has access to their directory, so the problem shall not become from nginx config as I suppose. There should be something wrong with laravel which cannot load the assets correctly. This is the code to access the file that returns 404:
<link href="{{asset('/css/bootstrap.css')}}" rel="stylesheet">
Upvotes: 0
Views: 3768
Reputation: 3071
Shoot in the dark you havent even posted configurations or error log but make sure you are not just taking care of the "PHP" traffic.
server {
listen 80;
server_name example.com;
root /srv/example.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Thats the important part. Its telling NGINX: Try to find the URI (/img/sample.png) inside the configured webroot (root). If you can not find it, send the request to the /index.php file.
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Upvotes: 2