Search code examples
htmlnode.jsnginxrelative-path

Nginx - Cannot convince /app to behave like /app/ (relative path mayhem)


Example

If you navigate to https://aiogames.com/chart, none of the relative path sources (i.e., graph.js) resolves. To see the desired result visit https://aiogames.com/chart/ . This is due to the resources being hosted in a separate (node) application instead of NGINX when relative paths resolve to the base URL, the query NGINX instead of the application, and empty resources.

The NGINX setup is the default except for an addition to /etc/nginx/servers_enabled. In the server section for ssl under the "location /" block I've added the following:

#Section to enable node application chart on port 3000 
    location /chart {
            proxy_pass http://localhost:3000/;
    }

Problem

I need the node application to be agnostic to the NGINX setup (No hard links, and should act the same if I had instead redirected the base URL to the application). Is there any way to configure NGINX to treat /chart as a directory (the same as /chart/)? Or how should I set it up, so that /chart forwards to /chart/?

Additional Background

I'm relatively new to NGINX. Although I instantly saw the bug and understood it resulted from my configuration and relative path names, the docs I used to configure NGINX don't give a clean (let alone elegant) solution. Many answers I did find suggested using absolute links or copying the data into NGINX, which cannot possibly be a good or reasonable practice.

Testing happens locally as if the application was on its own base URL, so the bug is only noticeable on the actual deployment.

I appreciate any direction.

Thank You


Solution

  • I think you overcomplicate the issue.

    There should be one canonical URL, let it be the one with a slash.

    Set up NGINX to redirect from non-slash to the canonical URL:

    location = /chart {
        return 301 /chart/;
    }