한 페이지 응용 history 모드 nginx 설정
hash 모드 에서 경 로 를
/#/xxx/xxx
모드 로 바 꾸 고 페이지 를 새로 고치 지 않 으 며 자원 경로 가 변 하지 않 아 내용 의 전환 을 요청 합 니 다.history 모드 는 직접 제거
/#/
되 었 습 니 다. 서버 가 설정 하지 않 으 면 404 입 니 다.서버 가 history 모드 를 실현 하 는 원리: 특정한 응용 자원 을 요청 하면 이 응용 프로그램의 입구 로 돌아 갑 니 다 (index. html). 경 로 는 모두 입구 에 맡 깁 니 다.
이루어지다
nginx 는 이 기능 을 실현 할 수 있 는 방법 이 많 습 니 다. error 를 통 해 소개 합 니 다.페이지 구현 방법
vue 를 예 로 들 면:
통과 errorpage
// xxx.com base
const router = new VueRouter({
base: process.env.BASE_URL,
mode: 'history'
/* ... */
})
// xxx.com/xxx base
const router = new VueRouter({
base: '/demo',
mode: 'history'
/* ... */
})
./
. history 모드 에서 경 로 는 / \ # / 로 표시 되 지 않 기 때문에 상대 경 로 를 사용 하면 경로 가 잘못 되 어 일부 자원 404 를 초래 할 수 있 습 니 다.// xxx.com /
module.exports = {
publicPath: '/',
/* ... */
}
// xxx.com/xxx xxx/
module.exports = {
publicPath: 'demo/',
/* ... */
}
# root
# location ( ), location ( )
root /www/server/nginx/html;
# xxx.com
location / {
index index.html;
error_page 404 /index.html;
}
# xxx.com/xxx
location /demo {
index index.html;
error_page 404 /demo/index.html;
}
error 이용page 의 특성 상 404 페이지 만 있 으 면 해당 입구 파일 을 되 돌려 줍 니 다.
try 통과 하기files
try 도 사용 가능파일 구현
# xxx.com
location / {
try_files $uri $uri/ /index.html;
}
rewrite 로 도 가능... nginx NB