0

What am I doing wrong here?

This works:

location / {
    alias /var/www/static/;
    try_files $uri index.html =404;
}

But this doesn't:

location /hello {
    alias /var/www/static/;
    try_files $uri index.html =404;
}

Here is the error I'm getting:

[error] 14428#0: *1 open() "/usr/share/nginx/html/bundle.js" failed (2: No such file or directory)

I know that Nginx is looking for bundle.js from the wrong directory. It should be located in the /var/www/static/ folder.

I can fix that by adding

root /var/www/static/

in the beginning of my config but then if I add another location the same problem re-emerges.

location /world {
    alias /var/www/another/;
    try_files $uri index.html =404;
}

Error:

[error] 14827#0: *1 open() "/var/www/static/bundle2.js" failed (2: No such file or directory)

bundle2.js should be in the /var/www/another/ folder but because I defined root as /var/www/static/ Nginx is looking for bundle2.js from the wrong folder.

Here is the whole config:

server {
    listen 80;

    root /var/www/static/;

    location /hello {
        alias /var/www/static/;
        try_files $uri index.html =404;
    }

    location /world {
        alias /var/www/another/;
        try_files $uri index.html =404;
    }
}
0

How is the file requested inside your html file? If you request it with a slash in the beginning (<script src="/bundles.js"></script>) it is root-relative, leading nginx to look for the file in the website's root directory, which with your first config was the default directory. With the second config nginx looked it up under "/var/www/static"
If your .js is called by <script src="bundles.js"></script> (relative path) it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.