Question

Nginx subdirectory css and js not wokring Laravel

Am trying to setup multiple laravel projects on nginx server. now in /var/www/htm/ i have two projects demo and demo1

nginx default 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 index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    location /demo {

        alias /var/www/html/demo/public;

        try_files $uri $uri/ @nested;

               location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass   unix:/run/php/php7.4-fpm.sock;

                        }
   }

location @nested {
        rewrite /demo/(.*)$ /demo/index.php?/$1 last;
}

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }
}

Now here when am trying to access myip/demo then there is no css. i.e <link rel=“stylesheet” href=“/css/demo.css”> this means nginx trying to find css and js files in root directory but files are available in demo directory. how can I solve this? thank you

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

The problem seems to be that your demo assets are stored at /demo/css/demo.css however you are loading them directly with /css/demo.css. What you could do @asset blade helper.

To do so, in your .env file you need to make sure that you have your ASSET_URL`` set correctly to your_ip/demo:

ASSET_URL=http://your_ip.com/demo

And then when you load your assets in your blade view, do not specify them directly with <link rel=“stylesheet” href=“/css/demo.css”> but instead use the @asset() helper function:

<link rel=“stylesheet” href=“@asset('css/demo.css')”>

The generated asset URL will be: http://your_ip.com/demo/css/demo.css.

For more information, I would recommend taking a look at the official documentation here:

https://laravel.com/docs/8.x/helpers#method-asset

Hope that this helps. Best, Bobby