Vue路由器在Nginx上的历史模式服务器配置无法工作

5
我从vue router documentation阅读到以下说明:

注意:使用历史模式时,服务器需要正确配置,以便用户直接访问您网站上的深层链接时不会出现404错误。

因此,我尝试按照以下方式配置我的nginx。
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;
    }
}

以下是我的Vue配置

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

但是,当用户直接访问我网站中的深链接时,仍然会出现404错误。
编辑:我也使用Laravel路由。以下是我的Laravel路由。
Route::get('user', function() {
    return View::make('user.index');
});

所以,你修好了吗? - antoniputra
@antoniputra 是的,使用了我自己的答案。 - Set Kyar Wa Lar
1个回答

5

我刚刚阅读了Matt Stauffer的博客文章,最终找到了在Laravel路由中实现该功能的方法。如下所示:

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

它不会在用户直接访问网站的深链接时返回404错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接