NGINX server configuration for static apps

161 views Asked by At

I have a website which is an app containing other apps. In the main app, I'll load the other apps with iframes.

The main app is served at the domain root: http://example.com The other apps are served under the /apps path:

Also the apps are developed in Polymer so there is client side navigation. I make sure the /apps path is not used on the client side to hit the NGINX server correctly but it also means that for url such as http://example.com/view1 it should redirect to the index.html of the main app. And for url like http://example.com/apps/app-b/view1 it should redirect to the index.html of app-b.

I'm trying to configure NGINX to serve those static apps.

server {
        listen 80;
        server_name example.com;

        root /var/www/example.com/main-app;
        index index.html;

        try_files $uri $uri/index.html /index.html;

        location ~ /apps/([a-z-]+) {
                alias /var/www/example.com/apps/$1;
        }
}

With the above config I have the main app working with the correct redirection to the index.html for the /view1 path for example.

But I have a 403 forbidden error for the sub apps.

directory index of "/var/www/example.com/apps/app-b" is forbidden, client: 127.0.0.1, server: example.com, request: "GET /apps/app-b/

I've tried other configurations with no success (infinite redirection leading to /index.html/index.html/index.html...).

I'm sure about the filesystem permissions all directories are 755 and files are 644.

I don't know why it is trying to do a directory index. Any help is appreciated.

1

There are 1 answers

1
Richard Smith On BEST ANSWER

When using alias with a regular expression location, you need to build the entire path to the file. Currently, you are only capturing the second path element.

Having said that, you do not actually need to use an alias here, as the root directive can be use instead.

location /apps/ {
    root /var/www/example.com;
}

See this document for details.