Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use vue.js with Nginx?

Tags:

I want to build a single page application with Vue.js using Nginx as my webserver and a my own Dropwiward REST API. Moreover I use Axios to call my REST request.

My nginx config looks like

server {     listen       80;     server_name  localhost;      location / {         root     path/to/vue.js/Project;         index    index.html index.htm;         include  /etc/nginx/mime.types;     }     location /api/ {         rewrite ^/api^/ /$1 break;         proxy_pass http://localhost:8080/;     } } 

Currently I can just call my localhost/api/path/to/rescource to get the the information from the backend.

I build the Front end with HTML and javascript(vue.js) which has worked so far. However when I want to build a single page application most tutorials mention node.js. How can I use Nginx instead?

like image 491
A.Dumas Avatar asked Dec 05 '17 14:12

A.Dumas


People also ask

How do I run a vue project on localhost?

Open in browser To view the project, open a tab and type http://localhost:3000 into the URL bar. That's the address that the Node server is listening to. You should now see this page, which is the basic structure of our project.


1 Answers

Add the following code to your Nginx Config:

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

the following snippet has been taken from vue-router docs, which is here.

Also, you need to enable history mode on VueRouter:

const router = new VueRouter({   mode: 'history',   routes: [...] }) 
like image 181
Daksh Miglani Avatar answered Sep 28 '22 20:09

Daksh Miglani