Establish a Vue application to run on Nginx at port 80 in Raspbian, paired with a Flask backend operating on port 8080

My setup involves Nginx running a Flask-based backend on port 8080 with the following configuration:

server {
    listen 8080 default_server;
    listen [::]:8080;

    root /var/www/html;

    server_name _;

    location /static {
        alias /var/www/html/static/;
    }

    location / {
        try_files $uri @wsgi;
    }

    location @wsgi {
        proxy_pass http://unix:/tmp/gunicorn.sock;
        include proxy_params;
    }

    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off;
        log_not_found off;
        expires max;
    }
}

I have also set up a systemd service that uses gunicorn to run the Flask app using:

gunicorn --bind=unix:/tmp/gunicorn.sock --workers=4 start_backend:web_app

Currently, Python Flask backend is working on port 8080. I now want to add Vue app on the default port 80 as well.

Update

server {
    listen 80 default_server;
    listen [::]:80;
    root /var/www/html/dist;
    server_name _;

    location /static {
        alias  /var/www/html/dist/static/;
    }

    location / {
        root /var/www/html/dist;
        try_files $uri $uri/ /index.html;
    }

    location /api {
        root /var/www/html;
        try_files $uri @wsgi;
    }

    location @wsgi {
        proxy_pass http://unix:/tmp/gunicorn.sock;
        include proxy_params;
    }

Answer №1

It seems like the solution here is to create an additional server block dedicated to serving the frontend of your application.

server {
    listen 80 default_server;
    listen [::]:80;

    location / {
         root /path/to/dist;
         try_files $uri $uri/ /index.html;
    }
}

This code snippet was inspired by a helpful tutorial. Be sure to replace /path/to/dist with the actual directory path where your Vue.js front-end code is located.

In the tutorial's section on setting up Nginx, they demonstrate how to serve both a Flask backend and a Vue.js frontend from the same server block but different URLs:

server {  
    listen 80;
    server_name 123.45.67.89;

    location /api {
        include uwsgi_params;
        uwsgi_pass unix:/home/survey/flask-vuejs-survey/backend/surveyapi.sock;
    }

  location / {
    root /home/survey/flask-vuejs-survey/frontend/survey-spa/dist;
    try_files $uri $uri/ /index.html;
  }

For public-facing applications, it's recommended to use the second configuration to ensure that all content is served through port 80 instead of potentially blocked ports such as 8080.

You may need to update your Vue.js app to point to the API endpoint at /api while leaving / free to host the frontend components.

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

Retrieve the unique identifier of a single post from a JSON file within a NuxtJS project

Is there a way to retrieve the unique post id data from a JSON file in NuxtJS? created() { this.fetchProductData() }, methods: { fetchProductData() { const vueInstance = this this.$axios .get(`/json/products.json`) ...

Attach actions to specialized component

Working with Vue3 I'm currently in the process of developing a custom component and I want to bind events to the input element within it. How can I achieve this now that $listeners are no longer available? I am unable to use v-bind="$attrs" ...

Learn how to extract JSON information from a URL and integrate it into either a table or dropdown in Vue.js

Looking to retrieve a JSON data array from a URL and utilize it to populate either a table or dropdown with Vue.js and Axios. Here is the link where I intend to fetch the data. Any advice on how to accomplish this? https://jsonplaceholder.typicode.com/us ...

Vue's parent-child components interact through asynchronous lifecycles

Even though I'm directed to 'myFavouritePage', the message 'Hi, I'm child' is displayed after the redirection. Is there a parent function that creates the child components upon completion? Parent: beforeCreate () { var or ...

Vue.nextTick incorrect detection

scenario: const Constructor = Vue.extend(MyComponent); function createComponent() { vm = new Constructor({ props, }).$mount(); return vm; } Query: In my testing process, I notice vm.$nextTick().then(() => { expect(result).to.eq ...

Conceal the number of entries displayed in Ag-Grid while in Pivot Mode

I am currently utilizing ag-grid enterprise version 21.2.x in my VueJs Project, which includes a pivot table as displayed in the picture below. My objective is to conceal the record count in the pivot table. Despite attempting a solution recommended for v ...

Enhancing User Authentication: Vue 3 with TypeScript Login

Recently, I came across a new technology called Supabase and noticed that most resources mention registration on JavaScript instead of TypeScript. As I started working on a project using Vue 3 + TypeScript, I encountered some errors that I need help resolv ...

Restrictions of Vue Data Objects

I'm currently pondering the optimal amount of data to store in a Vue data object. Imagine I have over 4,000 objects structured like this: personWithAppointment = { id: 1, appointment_id: 1, first_name: 'Jim', last_name: 'Jim&ap ...

Error: Installation of component unsuccessful in nuxt.js

After creating a new file called testcomp.vue, I added the following code in pages/index.vue: import testcomp from 'components/testcomp' In the export default{} section, I included the component as follows: components:{ 'testcomp&apo ...

When Vue 3 is paired with Vite, it may result in a blank page being rendered if the

Issue with Rendering Counter in Vite Project After setting up a new project using Vite on an Arch-based operating system, I encountered a problem when attempting to create the simple counter from the Vue documentation. The element does not render as expec ...

Passing a variable as a property to a nested child component in Vue.js

I am curious about how to efficiently pass variables to nested components. Within my setup, I have a total of 3 components: Main Secondary Tertiary All of these components share a common variable (referred to as sharedVar). If I want to avoid using Vue ...

Wordpress: nginx encountered an error

I set up my wordpress website on DigitalOcean, following a tutorial from this site. The next day, my wordpress site went down and I encountered the following error message: "An unexpected error has occurred. Apologies, but the page you are trying to acc ...

The NGINX reverse proxy fails to forward requests to an Express application

I am currently in the process of setting up a dedicated API backend for a website that operates on /mypath, but I am encountering issues with NGINX not properly proxying requests. Below is the nginx configuration located within the sites-enabled directory ...

I am attempting to show a message using v-model, however, it seems to be malfunctioning

I am currently learning how to use Vue.js with Laravel. However, I am encountering an issue when trying to display a message in an input tag using v-model. <div class="card" id="myAppId"> <p>@{{ message }}</p> <input type="text" v-mod ...

Leveraging the power of Vue Draggable with a dynamic computed Array

I have been utilizing the vuedraggable component available at this link: https://github.com/SortableJS/Vue.Draggable The setup I am using is as follows: <draggable v-model="filteredItems"> <div class="item" v-for="item ...

Vue Nativescript failing to display an image

Why are my images not showing up? The label is displaying the URL properly but the image is not showing. Why is it not displaying? Do I need to add something? It was working before. When I added a normal image with the source plain text like this, it show ...

Displaying a certain div when clicked within a loop using Vue.js and Laravel

I am currently facing an issue with displaying a hidden div upon click. The problem arises when using a loop to dynamically generate all the divs. Whenever I click the button, it shows all the divs instead of just one specific div on each click. I attempte ...

What are some methods to manage the timing of sending socket connection requests from the client to the backend server?

Currently using vue-socket.io, my frontend initiates a Socket.io connection request to the backend when the app is first being built or whenever the page is refreshed. This poses an issue for me because the page is initially constructed on the landing page ...

Issue encountered during rendering: "TypeError: Attempting to access property '_t' of an undefined object" while running a Unit Test using Jest

I spent hours troubleshooting a unit test for my Vue.js component, but no matter how much I searched the internet, I kept encountering this error: console.error node_modules/vue/dist/vue.runtime.common.dev.js:1884 TypeError: Cannot read property ' ...

Ways to eliminate all content preceding a specified value

Currently utilizing Bootstrap Vue and aiming to incorporate a formatter callback for injecting HTML into a table column. In the provided example from Bootstrap's documentation, the link is formatted as an anchor link: Ex. <b-table striped respon ...