nginx serving AngularJS app returns 404 Not Found

1

I am experiencing problems on configuring nginx to serve static angularjs files.

This is my situation, I want the landing page of my application under

domain.com

and the AngularJS app under

domain.com/app

Now I am struggling with the nginx configuration. I heve created a nginx server configuration as follows:

server {
    listen   80; ## listen for ipv4; this line is default and implied

    index index.html index.htm;

    server_name domain.com www.domain.com;

    location / {
            root /usr/share/nginx/html/domain.com;
    }

    location /app {
        alias /usr/share/nginx/html/domain.com/app/public;
    }
}

But this is not working, I get this error on Firebug:

"NetworkError: 404 Not Found - http://domain.com/app/"

Any idea?


Ok I made some progress, if I change the following:

alias /usr/share/nginx/html/domain.com/app/public;

to the following:

alias /usr/share/nginx/html/domain.com/app/public/;

(if I use root instead of alias, I get 403 forbidden)

I can now access the application but still I have 404 on assets files, for example one of them is:

"NetworkError: 404 Not Found - http://example.com/app/assets/images/logo.png"

assets are under the directory:

/usr/share/nginx/html/example.com/app/public/app/assets/

but for some reason nginx is looking for assets here:

/usr/share/nginx/html/example.com/app/public/assets/

any idea?

ledzgio

Posted 2015-05-19T13:31:22.597

Reputation: 11

Is there an index.html file in the folder /usr/share/nginx/html/domain.com/app/public? – Arjan – 2015-05-19T13:34:54.077

Yes there is the index.html file and app folder with assets. The strange thing is that this application works fine if I put everything under the root, so I can access it by domain.com. – ledzgio – 2015-05-19T13:39:49.703

Why the difference between root and alias? Or, though I doubt that, maybe the problem is that location /app refers to a subdirectory of the file system folder used in location /? Does http://domain.com/app/public load the AngularJS index.html? (If so, then it's using the mapping for location / for that, I guess.) – Arjan – 2015-05-19T13:44:05.467

do you need a trailing slash after the /app in your location block (location /app/ {)? – heavyd – 2015-05-19T13:53:10.343

@Arjan http://domain.com/app/public does not load Angularjs, the error is the same (404). heavyd I changed to /app/ and now I get 403 Forbidden.

– ledzgio – 2015-05-19T13:58:44.663

I get this error on nginx logs: 2015/05/19 15:02:48 [error] 21155#0: *1 directory index of "/usr/share/nginx/html/domain.com/app/public" is forbidden, client: x.x.x.x, server: domain.com, request: "GET /app/ HTTP/1.1", host: "domain.com" – ledzgio – 2015-05-19T14:04:13.277

What about http://domain.com/app/index.html and http://domain.com/app/public/index.html then? And did you see my question about root vs alias? The error directory index of "/usr/share/nginx/html/domain.com/app/public" is forbidden would make me think there simply is no folder /usr/share/nginx/html/domain.com/app/public/ or it has no index.html to fall back to in case no specific file is requested. (I'm not an nginx expert though.) – Arjan – 2015-05-19T20:45:38.773

Check nginx error log, it should have entries about your request and 404 you got. – user996142 – 2015-05-20T11:46:54.177

You need to look at this: http://stackoverflow.com/a/10647080/132074

– David Betz – 2016-02-16T00:42:04.093

No answers