Score:0

Nginx multiple directories with static references

do flag

I have an Nginx server on which I want to host two seperate react apps, let's call them Aapp and Bapp.

The folder structure is like this

  • Aapp (directory)
    • index.html
    • script.js
    • style.css
  • Bapp (directory)
    • index.html
    • script.js
    • style.css
  • ..other files and directories

I then have my config file setup like this:

server {
  root home/user/Aapp;
  index index.html;
      
  location ^~ /Bapp {
    allow all;
    alias /home/user/Bapp;
  }
  ....
}

Visiting the url my-domain/Bapp is successfully showing index.html from the folder 'Bapp' but it is including the script.js and style.csss from 'Aapp' instead of the ones from 'Bapp'.

Is there a way to tell the server if the file request is coming from the Bapp folder, it should use the 'Bapp' folder files? or do I need to update it in my application build script? (not ideal as different environments may differ)

Score:0
us flag

You need to handle setting the base URL for the application in application setting: https://stackoverflow.com/questions/48134785/how-to-set-a-base-url-for-react-router-at-the-app-level

Handling this issue cannot be done in a good way with nginx.

Score:0
us flag

This problem might be more script-based than server based. In your react apps, do you reference the files as src="/script.js" or just src="./script.js"?

What I imagine the issue is here is that your scripts are referencing the absolute home which is /home/user/Aapp instead of referencing the relative location.

Michael avatar
do flag
The reference is like src="/script.js"
Scott Craig avatar
us flag
Yeah that’s the problem. That is referencing the other app’s script. Reference it as “./script.js”.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.