这篇文章是收集我在工作中经常会用到的nginx相关知识点,本文并不是基础知识的讲解更多的是一些方案中的简单实现。location的匹配规则表示精确匹配。只有请求的url路径与后面的字符串完全相等时,才会命中。表示如果该符号后面的字符是最佳匹配的,采用该规则,不再进行后续的查找。表示该规则是使用正则定义的,区分大小写。表示该规则是使用正则定义的,不区分大小写。 注意的是,nginx的匹配优先顺序按照上面的顺序进行优先匹配,而且注意的是一旦某一个匹配命中直接退出,不再进行往下的匹配 剩下的普通匹配会按照最长匹配长度优先级来匹配,就是说谁匹配得越多就用谁。server{servernamewebsite。locationdocument{return701;}locationdocume。{return702;}locationdocument{return703;}}curlIwebsite。com:8080document702匹配702因为正则的优先级更高,而且正则是一旦匹配到就直接退出所以不会再匹配703server{servernamewebsite。locationdocume。{return701;}locationdoc{return702;}locationdocument{return703;}}curlhttp:website。comdocumentHTTP1。1702匹配702因为精确匹配的优先级比正则高也是匹配到之后支持退出server{servernamewebsite。locationdoc{return702;}locationdocu{return701;}}701前缀匹配匹配是按照最长匹配,跟顺序无关history模式、跨域、缓存、反向代理html设置history模式location{indexindex。htmlindex。proxysetheaderHhistory模式最重要就是这里tryfilesuriuriindex。index。html文件不可以设置强缓存设置协商缓存即可addheaderCacheControlnocache,mustrevalidate,proxyrevalidate,maxage0;}接口反向代理locationapi{跨域处理设置头部域名addheaderAccessControlAllowO跨域处理设置头部方法addheaderAccessControlAllowMethodsGET,POST,DELETE,OPTIONS,HEAD;改写路径rewriteapi(。)1反向代理proxypasshttp:proxysetheaderH}location。(?:css(。map)?js(。map)?gifsvgjfificocurheicwebptiff?mp3m4aaacoggmidi?wavmp4movwebmmpe?gaviogvflvwmv){静态资源设置七天强缓存expires7d;}以目录去区分多个history单文件 因为不可能每一个项目开启一个域名,仅仅指向通过增加路径来划分多个网站,比如:www。taobao。comtmalllogin访问天猫的登录页面 www。taobao。comalipaylogin访问支付宝的登录页面server{listen80;servernametaobao。indexindex。htmlindex。通过正则来匹配捕获〔tmallalipay〕中间的这个路径location(〔〕)(。){tryfilesuriuri1distindex。html404;}}负载均衡 基于upstream做负载均衡,中间会涉及一些相关的策略比如iphash、weightupstreambackserver{哈希算法,自动定位到该服务器保证唯一ip定位到同一部机器用于解决session登录态的问题server127。0。0。1:9090(down表示单前的server暂时不参与负载)server127。0。0。1:8080weight2;(weight默认为1。weight越大,负载的权重就越大)server127。0。0。1:6060;server127。0。0。1:7070(其它所有的非backup机器down或者忙的时候,请求backup机器)}灰度部署 如何根据headers头部来进行灰度,下面的例子是用cookie来设置 如何获取头部值在nginx中可以通过httpxxx来获取变量upstreamstable{serverxxxmaxfails1failtimeout60;serverxxxmaxfails1failtimeout60;}upstreamcanara{serverxxxmaxfails1failtimeout60;}server{listen80;设置默认根据cookie头部设置接入的服务if(httpcookiettsversionidcanara){}if(httpcookiettsversionidstable){}location{proxypasshttp:proxysetheaderHproxysetheaderXRealIPproxysetheaderXForwardedFindexindex。htmlindex。}}优雅降级 常用于ssr的node服务挂了返回500错误码然后降级到csr的cos桶或者nginx中 优雅降级主要用errorpage参数来进行降级指向备用地址。upstreamssr{serverxxxmaxfails1failtimeout60;serverxxxmaxfails1failtimeout60;}upstreamcsr{serverxxxmaxfails1failtimeout60;serverxxxmaxfails1failtimeout60;}locationssr{proxypasshttp:开启自定义错误捕获如果这里不设置为on的话会走向nginx处理的默认错误页面捕获500系列错误如果500错误的话降级为下面的csr渲染errorpage500501502503504csrlocationerrorpage500501502503504200csrlocation注意这上面的区别等号前面没有200表示最终返回的状态码已csrlocation为准加了200的话表示不管csrlocation返回啥都返回200状态码}locationcsrlocation{这时候地址还是带着ssr的要去除rewritessr(。)1proxypasshttp:} webp根据浏览器自动降级为png 这套方案不像常见的由nginx把png转为webp的方案,而是先经由图床系统(node服务)上传两份图片:一份是原图png 一份是png压缩为webp的图片(使用的是imageminwebp) 然后通过nginx检测头部是否支持webp来返回webp图片,不支持的话就返回原图即可。这其中还做了错误拦截,如果cos桶丢失webp图片及时浏览器支持webp也要降级为pnghttp{includeetcnginxmime。设置日志格式logformatmainremoteaddrremoteuser〔timelocal〕accesslogvarlognginxaccess。keepalivetimeout65;开启gzipcomplevel6;负载均衡这里可以是多个cos桶地址即可upstreamstaticenv{}map设置变量映射第一个变量指的是要通过映射的key值Accpet第二个值的是变量别名maphttpacceptwebpsuffix{默认为空字符串正则匹配如果Accep含有webp字段设置为。webp值webp。}server{listen8888;取消绝对路径的重定向网站主页路径。此路径仅供参考,具体请您按照实际目录操作。location{indexindex。htmlindex。proxysetheaderHtryfilesuriuriindex。addheaderCacheControlnocache,maxage0;}favicon。icolocationfavicon。ico{}robots。txtlocationrobots。txt{}location。(pngjpe?g){PassWebPsupportheadertobackend如果header头部中支持webpif(webpsuffixwebp){先尝试找是否有webp格式图片rewrite(。)。(pngjpe?g)1。找不到的话这里捕获404错误返回原始错误注意这里的号代表最终返回的是staticimg的状态吗errorpage404}addheaderVaryAproxypasshttp:proxysetheaderHexpires7d;}locationstaticimg{rewrite。proxypasshttp:proxysetheaderHexpires7d;}assets,medialocation。(?:css(。map)?js(。map)?gifsvgjfificocurheicwebptiff?mp3m4aaacoggmidi?wavmp4movwebmmpe?gaviogvflvwmv){proxypasshttp:proxysetheaderHexpires7d;}errorpage50050250350450x。location50x。html{}}}