记录一些nginx前端配置
1.常用配置
#预期: 输入 http://a.com/xxx/ 访问到前端启动文件 服务器上的/home/biz/index.html文件
location /xxx {
root /home/biz/;
#alias /home/biz/;
index index.html;
try_files $uri $uri/ /index.html;
}
说明:这里注意root和alias的区别
- root是往匹配的到的地址前面加,或者说把匹配到的原封不动的拼接在root后面,所以实际访问/home/biz/xxx/index.html
- alias则是替换掉匹配到的地址,实际访问地址 /home/biz/index.html
2.代理配置
#预期浏览器显示 http://a.com/xxx/ 实际访问的是 http://b.com/xxx/
location /xxx {
proxy_pass http://b.com/xxx/;
}
3.后缀名相关
#由 http://a.com/XXXXX/index.html?asdpqwokme.pdf 转发到(实际访问) http://b.com/XXXXX/asdpqwokme
location /XXXX/ {
if($request_uri ~ "\/XXXX\/(.*).pdf"){
set $arg1 $1;
rewrite .* http://b.com/XXXX/$arg1? break;
}
}
说明:
1.request_uri代表访问的完整路径,包含问号后面的参数,如果直接在location做匹配 比如 location ~ (.*).pdf nginx默认是获取不到问号后面的参数或者字符的,导致匹配不上, 所以在匹配上根目录之后,再走if判断
2.rewrite 语法 :`rewrite 正则 目标 标志位
4.微应用设置
# 子应用配置(单独的项目目录)
location /MicApp1/ {
# 绝对路径,因为要使MicApp1成为MicApp1的根目录,所以用alias
alias /user/xxx/MicApp1/dist/;
index index.html index.htm;
}
本文持续更新