Score:0

Setting Context Path in nginx

cn flag

I am trying to serve my webpack bundled app and files from a context path. Nginx Config file is as below:

nginx.conf

server {

  listen 80;
  root   /usr/share/nginx/html;
  index  index.html index.htm;

  location / {
    try_files $uri $uri/ /mfeexample/index.html;
  }

  location /mfeexample/ {   
    try_files $uri $uri/ /mfeexample/index.html;
  }
}

Dockerfile

# Build Environment
FROM node:14.2.0-alpine as build

# Working Directory
WORKDIR /app

#Copying node package files to working directory
COPY package.* .

#Installing app dependency
RUN npm install --silent

#Copy all the code
COPY . .

#RUN Production build
RUN npm run build

# production environment
FROM nginx:stable-alpine

# Deploy the built application to Nginx server
# /usr/share/nginx/html is the Nginx static directory used to serve the appliation
COPY --from=build /app/build /usr/share/nginx/html/mfeexample

# Remove the default Nginx configuration in Nginx Container
RUN rm /etc/nginx/conf.d/default.conf

# Apply the new configuration file
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf

# Expose the Application on PORT 80
EXPOSE 80

# Start Nginx server
CMD ["nginx", "-g", "daemon off;"]

Now looking at the network tab in browser, I found all the files are being served at http://localhost:3000/mfeexample/, that is exactly what I wanted. However, when I try nested route it breaks, nested route I mean http://localhost:3000/mfeexample/customer/, looking at the network tab again, nginx is looking for files under http://localhost:3000/mfeexample/customer/index.html, but I want nginx to always find the files from http://localhost:3000/mfeexample.

PS: Please keep in mind I am new to nginx and I have been doing trial and error method to get upto this point.

Thanks in advance.

us flag
Could you try to clarify your question with clear examples: "When I request URL A, I want to get file B from filesystem". "When I request URL C, I want to get file D from filesystem". It is a bit difficult to see what you need from your question. Please also clarify what you mean by context path.
Sampreeth Amith Kumar avatar
cn flag
Context path I mean the URL path at which the application needs to be served. In my case, I want the application to be served at /mfeexample and not at the root level. All the files should also be served from /mfeexample.
us flag
Please add exact examples like I requested above. Without clear examples this question cannot be answered.
Sampreeth Amith Kumar avatar
cn flag
To give an example, when I hit the URL http://localhost:3000/ react application should not be rendered but when I hit the URL http://localhost:3000/mfeexample/ the application should render it's content and also serve all the static files from http://localhost/mfeexample. Now if I try nested route i.e http://localhost:3000/mfeexample/customer/ the same react application should serve all the static files from http://localhost:3000/mfeexample/ but should render the content in http://localhost:3000/mfeexample/customer/ I hope this helps.
Sampreeth Amith Kumar avatar
cn flag
The application should return main.js from the React application which should live at http://localhost:3000/mfeexample. The user should be able to visit either http://localhost:3000/mfeexample or http://localhost:3000/mfeexample/customer, however the current issue is that when going to http://localhost:3000/mfeexample/customer the server attempts to serve main.js from http://localhost:3000/mfeexample/customer instead of the base path at http://localhost:3000/mfeexample
us flag
Sorry, but your example still does not show what filesystem path should each URL serve. Please edit the question and add the URL - path mappings there.
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.