Laravel + NGINX CSS/JS 404 not found - laravel

Laravel 5.5 fresh
nginx version: nginx/1.10.3 (Ubuntu)
When I go to: http://<my-ip>/crm/ all is working great, I get the Laravel welcome page, all js and css are loading correctly.
When I go to http://<my-ip>/crm/register - I get 404 for css and js.
This is my conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
rewrite ^/((?!en)[a-z]*)/home$ /index.php?lang=$1&$args last;
rewrite ^/((?!en)[a-z]*)/sitemap.xml$ /sitemap.xml last;
rewrite /pigeon/(.*)$ /pigeon/index.php?/ last;
rewrite /crm/(.*)$ /crm/index.php?/ last;
if (!-f $request_filename){
set $rule_52 1$rule_52;
}
if ($rule_52 = "1"){
rewrite ^/(.*)(?<!php)$ /$1.php last;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /pigeon {
alias /var/www/html/pigeon/public;
try_files $uri $uri/ #pigeon;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location /crm {
alias /var/www/html/crm/public;
try_files $uri $uri/ /index.php?$query_string;
location /crm.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
How do I fix this conf file to catch css and js files correctly?
Thanks

I had a similar issue. It seems allright what you have there in the server block.Although you might be better off if you add a proper server name.
Check this question and see if it helps. It has to do with the symlink to var/www/html for your crm, if you used one.
Laravel 8 + nginx - app.css and app.js resources from public/ not loading - 404 not found
And if not you could still use the online CDN's from bootstrap.Also in the question.

Related

hosting vue.js and codeignitor with nginx

I have 2 folders dist and api_middleware in '/var/www/public_html/website.com/'
dist folder contains my frontend production code. This folder is symblink-ed from /home/user/frontend/dist
api_middleware folder contains codeignitor code, that we use as middleware for frontend to communicate with our erp. This folder is symblink-ed from /home/user/api_middleware.
I want to host both code with nginx. And this is the code that I came up with.
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/website.com.crt;
ssl_certificate_key /etc/ssl/website.com.key;
server_name website.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
index index.php index.html index.htm index.nginx-debian.html;
location / {
root /var/www/public_html/website.com/dist;
try_files $uri $uri/ /index.html;
}
location /api_middleware {
root /var/www/public_html/website.com;
try_files $uri $uri/ /api_middleware/index.php?/$request_uri;
client_max_body_size 100M;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_read_timeout 300;
}
}
navigating to website.com works. But the api calls with website.com\api_middleware\<PARAMS> is returning a 404
What am I doing wrong?
PS. Using ubuntu 18.4.
My Updated working code:
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/website.com.crt;
ssl_certificate_key /etc/ssl/website.com.key;
server_name website.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
root /var/www/public_html/website.com/dist;
index index.php index.html index.htm index.nginx-debian.html;
location /api_middleware {
alias /var/www/public_html/website.com/api_middleware/;
try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri;
client_max_body_size 100M;
}
location ~ /api_middleware/.+\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_read_timeout 300;
}
}
The issue was that using multiple root was messing with nginx to access the directory location my second codebase.
I used alias to change the location to search for my 2nd codebase, while using root to set location of my 1st codebase.
Take a look at the line try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri;. I have added 'api_middleware' twice here, even though the location to index.php file is /var/www/public_html/website.com/api_middleware/index.php . This is needed because of a very old bug in nginx.
finally, to process the php files, I changed the URI where to look for the php files to ~ /api_middleware/.+\.php$.
Reference:
NGINX try_files + alias directives
https://serverfault.com/questions/1035733/nginx-difference-between-root-and-alias
https://serverfault.com/questions/684523/nginx-multiple-roots/684605

Laravel + nginX + Windows 10 + api routing issue (404 Error)

I am trying to make a very simple API request with above settings. It is working perfectly if I serve it with artisan serve command. But If I make the request with nginx, it gives 404 error. Thats why I guess the issue is with the nginX. my nginX conf is as follows.
server {
listen 80 ;
listen [::]:80 ;
root D:/sites/demo/public;
index index.php index.html index.htm ;
server_name laravel.local;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri /index.php = 404;
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

404 errors with controller files using CodeIgniter 3 and NGINX

I have a server running NGINX 1.10.0 and installed CodeIgniter 3 in a subdirectory /ci/. The welcome page renders when I go to the subdirectory but any other controller gives a 404 error.
Here is the code in /etc/nginx/sites-available/default configuration file:
server {
server_name mydomain.org;
root /home/username/mydomain/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
expires 15d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/username/mydomain/ci/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;;
}
}
I created a Hello World Controller file (Hello.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){
$this->load->view('helloworld');
}
}
In my config.php file, I have the following:
$config['base_url'] = '';
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'REQUEST_URI';
I have tried the approaches here:
NGINX CodeIgniter Recipe: https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
CodeIgniter NGINX Rewrite Rules:
http://www.farinspace.com/codeigniter-nginx-rewrite-rules/
Codeigniter nginx 404 error:
Codeigniter nginx 404 error
Most of everything that I've read is based on PHP5 or an older version of CodeIgniter.
Use this config in Nginx (Source):
server {
server_name domain.tld;
root /your/web/root/path
index index.html index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
Also you need to make a change in your php-fpm config. Edit this file:
/etc/php/7.0/fpm/pool.d/www.conf
Find this line:
listen = /run/php/php7.0-fpm.sock
Change to this:
listen = 127.0.0.1:9000
This changes should solve your issue.
Here is the final working versions of the default, www.conf and config files. Another issue I found out I was having is that the controller and file name have to be the same capitalization. The controller class Hello needs to have the filename be Hello. Some of my controllers were not the same capitalization, this however was not the sole cause of the 404 as I tried that before fixing the default file.
/etc/nginx/sites-available/default:
server {
server_name example.org;
root /home/username/example/;
index index.php index.html index.htm;
location / {
# Check if a file or directory index file exists, else route it to index.php
try_files $uri $uri/ /index.php;
# Handle PHP files
location ~* \.php$ {
root /home/username/example/ci;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri $uri/ ci/index.php;
}
}
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
}
/etc/php/7.0/fpm/pool.d/www.conf:
listen = 127.0.0.1:9000
Finally, in my config file:
$config['base_url'] = 'http://example.org/ci/';
This configuration changes any CI links from
http://example.org/ci/Hello
to
http://example.org/Hello

Nginx / Laravel config to redirect from non index.php route

I've just deployed a newly built website running Laravel 5 and nginx and struggling to setup my nginx config to 301 redirect some old URLs - specifically those with .php extensions.
I'm looking to 301 redirect the below...
domain.com/foobar.php?id=123 -> domain.com/foobar/123
The default config assumes everything runs from index.php, so Laravel does not trigger on foobar.php.
I have tried adding the following rewrite rule, but it doesn't trigger (I assume because nginx isn't listening out for foobar.php requests?
rewrite ^/foobar.php(.*)$ /foobar/$1/ permanent;
Relevant snippet of my current nginx config below...
server {
listen 443 ssl;
server_name domain.com;
root /home/domain.com/public;
index index.html index.htm index.php;
charset utf-8;
if ($request_method = GET ) {
rewrite ^([^.]*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
This should work:
rewrite ^/foobar.php /foobar/$arg_id/? permanent;
You can get any argument like this $arg_{your_argument}. Just replace {your_argument} with the actual argument name.

Nested Codeigniter apps not working in NGINX

We've setup 2 CodeIgniter apps which has its own complete code bases on its own directories as in this example. Plus we also have wordpress blog on the same public_html directory.
public_html/HOME_APP
public_html/admin_tool/ADMIN_TOOL_CODES
public_html/blog/WORDPRESS_CODES
HOME_APP codes (CodeIgniter) and our WORDPRESS works fine. But the admin_tool (CodeIgniter) doesn't work. We can access only the http://example.com/admin_tool/index.php but not any inside controller pages. When accessing its show 404 error page. And it looks like the routes are handled via APP1
The nginx rules are as follows. Appreciate if anyone can help us to fix the issue with admin_tool
server{
listen 80;
root /home/ubuntu/websites/example.com/public_html;
index index.html index.htm index.php;
server_name example.com;
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
index index.php;
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =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;
}
location ^~/admin_tool/ {
root /home/ubuntu/websites/example.com/public_html/admin_tool;
index index.php;
try_files $uri $uri/ /index.php$args;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
The following should do the trick for all of your installations:
# We define the index directory at the outermost level and therefore
# only once for all servers. Also note that we use the PHP file first
# because all main directories are handled by PHP scripts and this will
# give us best performance.
index index.php index.html index.htm;
server {
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
# 80 is default!
#listen 80;
root /home/ubuntu/websites/example.com/public_html;
server_name example.com;
location / {
# Don't allow access to the logs directory.
location ~* ^/logs {
return 404;
}
# Don't allow access to PHP files.
location ~* \.php$ {
return 404;
}
# Handle static files.
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
# Directly return if the requested URI is a real file.
try_files $uri $uri/ index.php =404;
}
# Codeigniter and WordPress will always handle everything with their
# index.php script, therefore we only need to catch that case.
location = /index.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
I had the same problem while I am using codeigniter in subfolder(dashboard).
All non-wordpress requests are not being entertained but remain on wordpress main page.
I fixed my issue by following lines in my configuration file /etc/nginx/sites-available/{my-site-name}
location /dashboard {
try_files $uri $uri/ /dashboard/index.php;
}
location /index.php {
fastcgi_pass unix:/usr/sbin/php5-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Resources