1. 需求
同一个端口,根据路径映射到不同的前端项目
2.解决方法
server{
listen 8000;
location /project {
root /opt/project/web;
index index.html;
}
location /project_b {
alias /opt/project_b/web/; #需要以/结尾
index index.html;
}
}
listen 8000表示对外提供的端口
主要是通过配置多个location来进行路径匹配
注意:root与alias的区别
root与alias都是用来定义项目的路径
- 使用root:
访问路径为:http://127.0.0.1:8000/project/index.html
映射后的路径为:/opt/project/web/project/index.html
- 使用alias:
访问路径为:http://127.0.0.1:8000/project_b/index.html
映射后的路径为:/opt/project_b/web/index.html
注:路径以"/"结尾
扩展
location两种匹配规则:
1.匹配URL类型,有四种参数location [ =、~、~*、^~ ] uri {
...
}
=:全路径匹配(默认)
~:正则匹配
~*:正则匹配不区分大小写
- ^~:先按字符匹配,未匹配到再按正则匹配
2.命名location,用@标识location @name { … }