12

I'm trying to setup a vue-router on my nginx server. The issue I'm having is that my route doesn't work if I enter url directly to the browser myapp.com/mypath.

I've tried server configuration as described in the vue router docs as well as suggested similar configurations on stack overflow. My current nginx location configuration as follows:

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

location /subscribe {
    proxy_pass http://127.0.0.1/subscribe // express API app
}

All that does is redirects any path to my root component (path: /) and not /mypath. This does make sense and location seems to only redirect to the index file. How can I redirect direct link of myapp.com/mypath to /mypath route in my VueJS app?

Here is how my vue routes setup now:

...
const routes = [
    { path: '/', component: Landing },
    { path: '/mypath', component: MyPath }
];

const router = new VueRouter({ mode: 'history', routes });

new Vue({
    el: '#app',
    router,
    render: h => h(App)
});
2
  • Yeah, it's a typo here, but this isn't related to the issue I'm having. I've added this just in case, to show that I've got other location option there. I've corrected it now Commented Mar 2, 2018 at 16:29
  • Have you tried removing the $uri/ from the first location config ? I am using this (without even configuring any other location btw) and it seems to keep the /mypath appended. Commented Dec 21, 2020 at 8:01

8 Answers 8

10

I struggled for several hours solving the same problem. After all this config works for me:

events {
...
  http {
  ...
    server {
          listen       80;
          server_name  localhost;

          location / {
              root   /path/to/your/project/html;
              index  index.html index.htm;
              include  /etc/nginx/mime.types;
              try_files $uri $uri/ /index.html;
          }
    }
  }
}

I have almost the same router setup as you.

Sign up to request clarification or add additional context in comments.

2 Comments

this one works perfectly, especially on this line try_files $uri $uri/ /index.html; which is the most important
I don't get it. The try_files is exactly the same as in the original question. What is going on?
4

I've found 1 possible solution with the suggestion from a co-worker.

I'm now passing URI as a query parameter in nginx. So my config is now this:

location / {
    try_files $uri $uri/ /index.html?uri=$uri
}

Then in my router configuration in VueJS:

const routes = [
{
    path: '/',
    component: Landing,
    beforeEnter: (to, from, next) => {
        const { uri } = to.query;
        if (uri != null && uri != '/') {
            next(false);
            router.push(uri);
        } else {
            next();
        }
    }
}, ...

This seems to do the trick, although looks a bit dodgy.

Comments

4

Are you using a custom publicpath (mypath) in the vue.conf and the nginx?

If so, you need to change the configuration to match to that path.

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

Comments

3
  1. Got to: /etc/nginx/sites-enabled

  2. Open the config for your domain

  3. add the following code under 'root'

    location / {
      try_files $uri $uri/ /index.html;
    }
    
  4. save the file

  5. restart your nginx server with command: /etc/init.d/nginx restart

  6. Refresh browser

2 Comments

This does not solve the problem at all. It looks like a copy paste of how to configure nginx "getting started"...
this is a good answer because of the addition of index.html in the location line.
1

In my case try_files didn't work, probably because another nginx is over my app nginx, using proxy_pass. I had to use 404 error rewriting:

server {
    listen ${NGINX_PORT} ssl;
    server_name ${NGINX_HOST};
    ssl_certificate ${NGINX_SSL_CERTIFICATE};
    ssl_certificate_key ${NGINX_SSL_CERTIFICATE_KEY};
    error_page 404 =200 /index.html;
    location / {
        root ${NGINX_ROOT_LOCATION};
        index  index.html;
        include  /etc/nginx/mime.types;
    }
}

2 Comments

Which nginx was this in? Assuming you were still using proxy_pass I assume this is your app nginx config?
@Sandwich this is docker container with docker image nginx:stable-alpine
0

well I had same problem so I tried to redirect every 404 errors to root url and it perfectly works.

  1. Got to: /etc/nginx/sites-enabled

  2. Open default config file

  3. add this line before location for 404 redirect:

    error_page 404 /;

save the file and don't forget to restart nginx

Comments

0

My suggestion is make sure you use the next line, as suggested in vue-router documentation. But, in addition, if you have multiple files to set your nginx server,for instance a file for setting the SSL of your server,please make sure to include it also in those files.

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

Comments

0

In vue router 4 for vue3

first

try to find the mounted APP.vue, when you refresh the webpage, the App.vue will be loaded firstly. So make sure you don't mounted this.$router.push("/")

  mounted() {
     this.$router.push("/");
}

the correct thing

mounted() {
    this.loading();
},
methods: {
  loading() {
    if(document.URL=="http://localhost:9000/" || document.URL=="https://zoujiu.com.cn/"
          || document.URL=="http://zoujiu.com.cn/" || document.URL=="http://localhost:9000/#/"
          || document.URL=="http://zoujiu.com.cn/#/"|| document.URL=="https://zoujiu.com.cn/#/"
        ) {
          this.$router.push({ path: '/'});
        }
  }
}

deployment

I use this history with nginx ssl https

const router = createRouter({
  // history: createMemoryHistory(),
  history: createWebHashHistory(),
  // history: createWebHistory(),
  routes:constantRoutes,
})

development

I use this history

const router = createRouter({
  // history: createMemoryHistory(),
  // history: createWebHashHistory(),
  history: createWebHistory(),
  routes:constantRoutes,
})

this is myown website http://zoujiu.com.cn.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.