Problems caused by the deployment of the nuxt project to nginx

according to the official vue instructions, the nginx configuration has been modified to

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

also, if the layout/error.vue, is set in the nuxt project, the result is that the 404 address will display the home page, and the non-404 address will be transferred to the corresponding page.
vue official instructions

 404  index.html  Vue  404 

const router = new VueRouter({
  mode: "history",
  routes: [
    { path: "*", component: NotFoundComponent }
  ]
})

I saw the auto-rendered routing of the nuxt project

export function createRouter () {
  return new Router({
    mode: "history",
    base: "/",
    linkActiveClass: "nuxt-link-active",
    linkExactActiveClass: "nuxt-link-exact-active",
    scrollBehavior,
    routes: [
        {
            path: "/product/test",
            component: _cda7ca6c,
            name: "product-test"
        },
        {
            path: "/product/:id?",
            component: _fb3a329c,
            name: "product-id"
        },
        {
            path: "/",
            component: _776d265b,
            name: "index"
        }
    ],
    
    
    fallback: false
  })
}
In the end,

does not have a wildcard setting, so it is the cause of the problem. After testing, it is normal to manually modify router and then publish it to nginx, but it is too troublesome. So I"d like to ask you how to solve this problem. Thank you.

Aug.04,2021

Don't use nginx, use nginx, and don't write this
try_files $uri $uri/ / index.html;
but instead use reverse proxy

< hr >
path: "/product/:id?",

you have this kind of route in your nuxt. If you generate static files and use other services, you have to re-implement this matching route by yourself, which is approximately equivalent to
pure static websites use nuxt generate
to build services using nuxt start

.

opposes another answer. There is no problem with combining Nuxt with Nginx. If you use spa mode, you still have to write the sentence try_files . If static generation is used, it doesn't matter if you have the sentence try_files (the effect is slightly different).

Nuxt has its own error handling mechanism: ide/views-sharplayouts" rel=" nofollow noreferrer "> https://nuxtjs.org/guide/view. (see paragraph Error Page)


answer it yourself. If you have any questions, you are welcome to point out and correct

.

1. Solve the problem that dynamic routing will be ignored when nuxt generates static files

dynamic routing is ignored when Nuxt.js executes the generate command.
you can configure the appropriate parameters in nuxt.config.js to solve this problem. The configuration is as follows

 generate: {
    routes: function () {
      return axios.get('https://my-api/users')
      .then((res) => {
        return res.data.map((product) => {
          return '/product/' + product.id
        })
      })      
    }
  }

II. Solve the problem of deploying static files generated by nuxt to nginx

1, configure nginx

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

2, configure nuxt

because nuxt dynamically rendered routes do not render "*" , as a result, 404 addresses will display the home page, and non-404 addresses will be transferred to the corresponding page, so you also need to manually configure rendering routes in nuxt.config.js .

  router: {
    extendRoutes (routes) {
      routes.push({
        name: 'custom',
        path: '*',
        component: resolve(__dirname, 'pages/404.vue')
      })
    }
  }
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e65d44-45b50.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e65d44-45b50.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?