Laravel - GET http://127.0.0.1:8000/emer911/public/css/app.css [HTTP/1.0 404 Not Found 291ms]
use mix instead of asset:
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
Laravel docker-compose 404 not found Nginx
In your nginx you set root
with root /var/www/public;
.
In your docker-compose.yml
you mount your source to /var/www/html
.
Make sure they are the same.
Nginx fails to load css files
I found an workaround on the web. I added to /etc/nginx/conf.d/default.conf the following:
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
The problem now is that a request to my css file isn't redirected well, as if root is not correctly set.
In error.log I see
2012/04/11 14:01:23 [error] 7260#0: *2 open() "/etc/nginx//html/style.css"
So as a second workaround I added the root to each defined location.
Now it works, but seems a little redundant. Isn't root inherited from / location ?
Assets not referencing to public folder (Laravel)
I was having same problem. This is due to moving of .htaccess file from public to root of the project in order to serve localhost/project
rather than localhost/project/laravel
. And needed to use public as well in the asset:
<link href="{{ asset('public/css/app.css') }}" rel="stylesheet">
Or, modify the asset function from /Illuminate/Foundation/helpers.php
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool $secure
* @return string
*/
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
}
The preceding method is not good way. Anyway this would have been easier if there was config for setting asset path.
react.js application showing 404 not found in nginx server
When your react.js
app loads, the routes are handled on the frontend by the react-router
. Say for example you are at http://a.com
. Then on the page you navigate to http://a.com/b
. This route change is handled in the browser itself. Now when you refresh or open the url http://a.com/b
in the a new tab, the request goes to your nginx
where the particular route does not exist and hence you get 404.
To avoid this, you need to load the root file(usually index.html) for all non matching routes so that nginx
sends the file and the route is then handled by your react app on the browser. To do this you have to make the below change in your nginx.conf
or sites-enabled
appropiately
location / {
try_files $uri /index.html;
}
This tells nginx
to look for the specified $uri
, if it cannot find one then it send index.html
back to the browser. (See https://serverfault.com/questions/329592/how-does-try-files-work for more details)
Laravel routes not found after nginx install
This is the correct basic config for Laravel and Nginx:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EDIT:
Instead of:
fastcgi_pass unix:/var/run/php5-fpm.sock;
As of November 2018, as PHP 7.2 is out, it would be:
fastcgi_pass unix:/var/run/php7.2-fpm.sock;
Related Topics
How to Measure the Space That a Text Will Take in JavaScript
Permanently Change CSS Property with JavaScript/D3Js/Jquery
Jquery Scroll Show Hidden Content
How to Combine Fixed Height Header with Flexible Page Layout
How to Use Jquery to Detect If a File Input Has a File Selected
Image Crossfade with JavaScript and CSS3 Transitions
Jquery Word for Word Fade in Effect
How to Create a Marquee Using CSS or JavaScript
How to Get Element's Attribute Set in CSS Class
How to Include CSS Files from an MVC Partial Control
Prevent Requestanimationframe from Running All the Time
How to Dynamically Change the Style Tag Using JavaScript
Creating a Custom Attribute with Angularjs
Cannot Return CSS Property of HTML Element
The Best Way of Checking for -Moz-Border-Radius Support
How to Move a Carousel Item to the Middle When It's Clicked in Jquery