Can you share the url so we can see the error? Also show the nginx config.
Moving to a new server and js doesn't load
So here it is.
We have a working laravel 7.28.4 laravel on php 7.2 and have to move it from a centos cpanel server to an ubuntu server.
So here is what we did.
- Enabled php7.2 on the new installation
- Installed Laravel globally for the specific user:
composer global require laravel/installer - Made an installation of laravel 7 on the user:
php7.2 /usr/local/bin/composer create-project --prefer-dist laravel/laravel:^7 www.domain.com - Tested it. It was working just fine. running
php7.2 artisan --versionreturned version 7.30.6 - tested through web browser. Laraverl test site came up without issue. Environment seemed ready to receive the website
- compressed all files and exported database from old server
- copied both files and database from old server to the new one.
- extracted and imported.
- Found using
grepand replaced usingsedall occurences of the old server's home folder. - Changed all .env settings to reflect the home folder, site url and databases used on the new serer.
After all that we tested to see if the website was comming up It was but with errors Mainly js errors. In reality only js errors seemed that the js controller was not being triggered in the routes and all javascript files returned error 404. They literally produced the 404 error whenever you even tried to directly access them.
After searching and searching we realised that it was not the controller's fault. the js files produce a 404 error when paired with nginx. It seems that once nginx picks up .js as the extension of an asset, it takes it and produces a 404 error before passing it through the routes/web.php. If we use different extensions (eg .javascript instead of .js) the files are being served.
It seems to me that when nginx "detects" a request for a .js file, it does not pass it through laravel and checks if the file physicaly exists in the file tree. When it's not found there (our js files are created using a js controller) it returns a 404 error before giving the chance to laravel to serve the file.
can you think of a way around it?
@Lumethys Hello.
2 things:
First: I fixed it.
I got the suggestion from the old programmer to add -> header('Content-Type', 'application/javascript'); at the return of every view on the js controller I posted above. That did not work of course. Laravel documentation gives another example: https://laravel.com/docs/8.x/responses#view-responses
So I changed the js controller and made it as so:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class JavascriptController extends Controller {
public function sxm_javascript() {
//return view('_javascript.sxm_javascript');
return response()
-> view('_javascript.sxm_javascript')
-> header('Content-Type', 'application/javascript');
}
public function sxm_home() {
//return view('_javascript.sxm_home');
return response()
-> view('_javascript.sxm_home')
-> header('Content-Type', 'application/javascript');
}
public function sxm_submit_one() {
//return view('_javascript.sxm_submit_one');
return response()
-> view('_javascript.sxm_submit_one')
-> header('Content-Type', 'application/javascript');
}
public function sxm_submit_two() {
//return view('_javascript.sxm_submit_two');
return response()
-> view('_javascript.sxm_submit_two')
-> header('Content-Type', 'application/javascript');
}
public function sxm_listing() {
//return view('_javascript.sxm_listing');
return response()
-> view('_javascript.sxm_listing')
-> header('Content-Type', 'application/javascript');
}
public function sxm_search_results() {
//return view('_javascript.sxm_search_results');
return response()
-> view('_javascript.sxm_search_results')
-> header('Content-Type', 'application/javascript');
}
public function sxm_myspiti_searches() {
//return view('_javascript.sxm_myspiti_searches');
return response()
-> view('_javascript.sxm_myspiti_searches')
-> header('Content-Type', 'application/javascript');
}
public function sxm_myspiti_favorites() {
//return view('_javascript.sxm_myspiti_favorites');
return response()
-> view('_javascript.sxm_myspiti_favorites')
-> header('Content-Type', 'application/javascript');
}
public function sxm_myspiti_notes() {
//return view('_javascript.sxm_myspiti_notes');
return response()
-> view('_javascript.sxm_myspiti_notes')
-> header('Content-Type', 'application/javascript');
}
public function sxm_myspiti_hidden() {
//return view('_javascript.sxm_myspiti_hidden');
return response()
-> view('_javascript.sxm_myspiti_hidden')
-> header('Content-Type', 'application/javascript');
}
public function sxm_myspiti_profile() {
//return view('_javascript.sxm_myspiti_profile');
return response()
-> view('_javascript.sxm_myspiti_profile')
-> header('Content-Type', 'application/javascript');
}
public function sxm_myspiti_alerts() {
//return view('_javascript.sxm_myspiti_alerts');
return response()
-> view('_javascript.sxm_myspiti_alerts')
-> header('Content-Type', 'application/javascript');
}
public function sxm_myspiti_listings() {
//return view('_javascript.sxm_myspiti_listings');
return response()
-> view('_javascript.sxm_myspiti_listings')
-> header('Content-Type', 'application/javascript');
}
public function sxm_auth() {
//return view('_javascript.sxm_auth');
return response()
-> view('_javascript.sxm_auth')
-> header('Content-Type', 'application/javascript');
}
public function sxm_contact() {
//return view('_javascript.sxm_contact');
return response()
-> view('_javascript.sxm_contact')
-> header('Content-Type', 'application/javascript');
}
}
You can see I've commented out how the code was and I converted it in a way that it spits out also the header of the content type. I suppose I can also add the Access-Control-Allow-Origin there that nginx wants for js files. If I require that I might add it.
Second: Thank you for your responses and any insight. However, changing and improving a site that was programmed by another one is very complex. Mainly because I do paid work. So, I'm getting paid for the migration at this point and I can only make suggestions about the security of the installation. I will use our conversation here so that I do not appear biased. I will present my concerns and back them with your comments, but it's up to the owner of the page to decide if he is willing to pay for the developing time.
In conclusion: thank you for your help and time. Your contribution was invaluable and I have to say, seing how laravel works, how well it's documented, how scalable it is and how helpful the community is, makes me want to learn more and use it on future projects as fast as possible.
Please or to participate in this conversation.
