Upon refreshing the browser, a Vue + Docker + Nginx application is displaying a 404 error

After successfully dockerizing a Vue app using nginx, the application runs smoothly upon starting. However, an issue arises when attempting to refresh the page - a 404 error is displayed, as shown in the attached image. Despite trying various configurations to the nginx.conf file based on recommendations from [this Stack Overflow thread](https://stackoverflow.com/questions/47655869/how-to-use-vue-js-with-nginx), the error persists. Below are my current nginx.conf file and Dockerfile.

Error image: https://i.sstatic.net/DpkBQ.png

nginx.conf file:

server {
    root /usr/share/nginx/html;
    
    index index.html;

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

Dockerfile:

# Step 1: Build Vue Project

FROM node:14.15.1 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# Step 2: Create Nginx Server
FROM nginx:1.20 AS prod-stage
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Answer №1

I encountered a similar issue and managed to find a solution at this link. You can resolve it by including the following snippet in your Dockerfile and replacing [your_nginx_file.conf]

# Insert nginx configuration
COPY [your_nginx_file.conf] /temp/prod.conf
RUN envsubst /app < /temp/prod.conf > /etc/nginx/conf.d/default.conf

Answer №2

It is recommended to adjust your nginx.conf as running port 80 inside a container is not ideal.

If you need further information, you can refer to my detailed explanation here: Configuring nginx for Vue-router on Docker

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for effectively waiting in Selenium for a list to finish scrolling

For my project, I decided to write Selenium code using Javascript in order to automatically scroll down a list by simulating the Down key on the keyboard. Although there are simpler ways to achieve scrolling with Javascript commands, I specifically wanted ...

Choosing a Query with Puppeteer - Unleashing the Power of Selection in Puppeteer

How do I use Puppeteer to select the html anchor element that clicks and navigates to the Tutorial page? https://i.sstatic.net/6rkNn.png I've tried this but it's not working const puppeteer = require('puppeteer'); const url = process ...

React Three Fiber encountered an unexpected JSON token 'c' at position 3

I am encountering an issue while trying to load a .glb file using react-three-fiber. The error I'm receiving is as follows: Error Unexpected token c in JSON at position 3 I can't seem to figure out what mistake I am making - it seems that the co ...

Creating multiple filters for mongoose is a great way to enhance your querying capabilities

I run an online gaming store with a filtering system based on tags and genres. However, I am looking to expand this system to include filtering by platforms and systems as well. For instance, being able to filter games by the PC system and the Steam platfo ...

In what ways can two identical-looking arrays actually be distinct from each other?

Explore this JavaScript snippet: var st, ary, ary1, ary2; ary = [["a","b","c"], ["d","e","f"]]; // 2D array ary1 = ary[1]; st = "d,e,f"; ary2 = st.split(','); st = typeof(ary1) + " " + ary1.length + " " + ary1 + '\n'; // A ...

What is the best way to implement validation for a textfield to prevent submission if a negative value is entered?

I am working with a text field of type number and I have successfully set a minimum value of 0 to ensure that negative values are not accepted. However, I have encountered an issue where I am unable to delete the 0 once it is entered. Is there a way to fix ...

Transform the binary image data sent by the server into an <img> element without using base64 encoding

It's been a challenge trying to find a reliable method for adding custom request headers to img src, so I'm experimenting with manually downloading the image using ajax. Here is an example of the code I am working on: const load = async () => ...

Angular causing WKWebview to freeze

Situation Our Angular+Bootstrap app is functioning smoothly in Desktop on all operating systems, as well as Android and iPhone devices. There are no visible JavaScript errors or CSS warnings. Dilemma However, an issue arises intermittently while using t ...

Conditionally defining variables in JavaScript only if they are currently undefined

I have been working on extracting two keywords from a URL in the following format: localhost:3000/"charactername"/"realmname" My goal is to extract "charactername" and "realmname" and assign them to variables. Here is the code snippet I am using: var c ...

Creating a catchy hook for implementing a "Load More" button using JSON data retrieval

How can I implement the functionality for my load more button to fetch and display additional Json data from a fake api? Currently, everything is working as expected except for the load more feature. I have an empty function that needs to be filled in. T ...

export default select an option

Forgive me if my question comes off as naive, but I'm still learning the ropes. :-) I stumbled upon something perplexing in this GitHub file. I am aware that we can export or import default functions, but in this instance, the author has used: expo ...

Angular instructions

I am fairly new to working with Angular. Currently, I am using Angular 1.6 and I have been tasked with making some modifications to an existing application. After doing some research, I discovered that the previous developer had used in order to create a ...

TypeORM's OneToMany relationship creates a corresponding column in the database

I am working with three tables and entities: client store store_client The relationships between these entities are as follows: Client: @OneToMany((type) => StoreClient, (storeClient) => storeClient.client, { eager: false }) storeClient: StoreClie ...

arranging an array in JavaScript that contains numeric values stored as text

I'm attempting to organize my array. My goal is to sort the elements based on the value within the square brackets at index 2. This means that the element at position 15 should be at the top, followed by the one at position 8 and so forth. I've ...

Is it possible for the relative path of a gif image to become invalid when added to a div?

Question Context: In the view, I have a form that will show a loading 'spinner' gif when a user submits it. The Problem: If I place the spinner img element within its container div, the spinner is always displayed, which is not desired: https ...

Selenium Webdriver Tutorial: Uploading a File Using its Relative Path in JavaScript

Currently, I am utilizing selenium webdriver with node.js (javascript) in Chrome. For certain tests, I need to upload a file using its relative path (the file to upload is located in the same directory as the test file). var fileInput = driver.findElemen ...

Is there a way to pre-load the data prior to the component rendering in ReactJS?

The main goal of my project is to retrieve data from the Google Analytics API and display it as a list. Although I am able to successfully fetch the data from the API, I am encountering an issue when passing it to another component. While I can view the da ...

Using cross-env to pass command-line arguments to NPM scripts

I'm currently developing a basic app that I run using an npm script. The script has two main functions: setting the NODE_ENV environment variable and calling node index.js. My goal is to make the value of NODE_ENV customizable through a command line ...

Laravel-Mix and BrowserSync are all set up and running smoothly, but there seems to be a glitch

tag, I have some important details regarding my current setup: [email protected] Node v12.16.2 NPM v6.14.4 Operating System: Laravel Homestead Description: While running npm run watch, everything seems to work smoothly. Updates on views, cont ...

Error: Exceeded the hook limit during rendering - reactjs problem

I keep encountering an issue when trying to retrieve main data using useSwr, where I call another function with axios and end up with an Uncaught Error: Rendered more hooks than during the previous render. I've attempted to troubleshoot this countless ...