3

I'm trying to setup 2 ember-cli apps under one domain on nginx.

http://example.com/
http://example.com/bar

App 1 is stored under /var/www/foo, and App 2 is stored under /var/www/bar.

First prize is having the main root point to /var/www/bar, and having it set up so that the / points to foo and / points to bar. This is mostly for consistency in the way things are setup. However I'm happy to alter the setup just to get it to work.

I have the following nginx snippet:

server {
    listen 80;
    server_name www.example.com;
    root /var/www/bar;

    location /bar {
        root /;
        try_files $uri $uri/ /index.html?/$request_uri;
    }

    location / {
        root /var/www/foo;
        try_files $uri /index.html?/$request_uri;
    }
}

I have tried it the other way around, but also couldn't get it to work. It seems that location / overrides the location /bar/. If I comment out the location / directive, then /bar works, but obviously not /. Vice versa also works. How do I get both of these to work together?

Note: ember-cli isn't really a contributing factor at this point. Just getting index.html files to work will do the trick.

2

The last parameter in the try_files directive is either a fallback code, location or URI. For the last two cases nginx make an internal redirect so the expected behaviour is to go to location /. You also make a mistake in root directive of location bar, / will match the root of your filesystem !

You need to change the fallback URI to /bar/index.html?/$request_uri. If understand well, you want to serve bar's index.html from /var/www/bar/index.html. Two solutions :

[1]

server {
    listen 80;
    server_name www.example.com;
    root /var/www;

    location /bar {
        try_files $uri $uri/ /bar/index.html?/$request_uri;
    }

    location / {
        root /var/www/foo;
        try_files $uri /index.html?/$request_uri;
    }
}

[2]

server {
    listen 80;
    server_name www.example.com;
    root /var/www/bar;

    location /bar/ {
        alias /var/www/bar/;
        try_files $uri $uri/ /bar/index.html?/$request_uri;
    }

    location / {
        root /var/www/foo;
        try_files $uri /index.html?/$request_uri;
    }
}

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.