Nginx 上历史模式的 Vue 路由器服务器配置不起作用
Vue router server configuration for history mode on nginx does not work
我阅读了来自 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.
所以,我尝试像下面这样配置我的 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'
});
但是,当用户直接访问我网站中的深层 link 时,我仍然得到 404。
已编辑:我也使用 Laravel 路由。以下是我的laravel路由
Route::get('user', function() {
return View::make('user.index');
});
我刚刚阅读 mattstauffer blog post 并最终在 Laravel 路线中找到了方法。喜欢下面
Route::get('user/{vue_capture?}', function() {
return View::make('user.index');
})->where('vue_capture', '[\/\w\.-]*');
当用户直接访问深层 link 站点时,它不会 return 404。
我阅读了来自 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.
所以,我尝试像下面这样配置我的 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'
});
但是,当用户直接访问我网站中的深层 link 时,我仍然得到 404。
已编辑:我也使用 Laravel 路由。以下是我的laravel路由
Route::get('user', function() {
return View::make('user.index');
});
我刚刚阅读 mattstauffer blog post 并最终在 Laravel 路线中找到了方法。喜欢下面
Route::get('user/{vue_capture?}', function() {
return View::make('user.index');
})->where('vue_capture', '[\/\w\.-]*');
当用户直接访问深层 link 站点时,它不会 return 404。