在配置文件中加上下面demo中加粗字体部分(try_files $uri $uri/ @router;)即可;
uri即用户访问的地址,比如:http://www.baidu.com/search.html,那么uri就是/search.html
uri/代表访问的是一个目录,比如:http://www.baidu.com/search/photo/,那么uri/就是 /search/photo/
try_files 首先会判断是文件还是目录,会尝试到目录下读取访问的文件,如果第一个变量存在,就返回;不存在继续跳转到下一个参数上。
demo:
server {
listen 80;
server_name localhost;
location / {
root /opt/nb/www;
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router{
rewrite ^(.+)$ /index.html last;
}
location /nb/ {
proxy_pass http://127.0.0.1:8881/nb/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}