5

I read the following note from vue router documentation

Note: when using the history mode, the server needs to be properly configured so that a user directly visiting a deep link on your site doesn't get a 404.

So, I try to configure my nginx like the following

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/laravel/public/;
    index index.php index.html index.htm;

    server_name working.dev;

    location /user {
        rewrite ^(.+)$ /index.php last;

    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

And the following is my Vue configuration

var router = new VueRouter({
    hashbang: false,
    history: true,
    linkActiveClass: "active",
    root:  '/user'
});

But, I still got 404 when user directly visiting a deep link in my site.

Edited: I use Laravel routing too. The Following is my laravel routing.

Route::get('user', function() {
    return View::make('user.index');
});
4

1 回答 1

5

我刚刚阅读了 mattstauffer 的博客文章,终于在 Laravel 路线中找到了方法。像下面这样

Route::get('user/{vue_capture?}', function() {
    return View::make('user.index');
})->where('vue_capture', '[\/\w\.-]*');

当用户直接访问站点的深层链接时,它不会返回 404。

于 2016-04-05T03:21:40.863 回答