以下文章来自CodeLee前端开发的匹配规则location=表示完全匹配。只有当请求的url路径正好等于后面的字符串时,才会命中。^~表示如果符号后面的字符是最佳匹配,则采用该规则,不再进行后续查找。~表示规则是使用正则表达式定义的,并且区分大小写。~*表示规则是使用不区分大小写的正则表达式定义的。需要注意的是nginx的匹配优先级是按照上面的顺序来的,需要注意的是一旦某个匹配命中直接退出,就不会再进行匹配了_前端训练中剩下的普通匹配根据匹配长度最长的优先级进行匹配,也就是说谁匹配的多就使用谁。服务器{server_namewebsite.com;location/document{return701;}location~^/docume.${return702;}location~*^/document${return703;}}curl-Iwebsite.com:8080/document702匹配702是因为正则优先级高,一旦匹配到正则就直接退出,所以不会匹配703server{server_namewebsite.com;location~^/docume.${return701;}location^~/doc{return702;}location~*^/document${return703;}}curlhttp://website.com/documentHTTP/1.1702匹配702因为^~精确匹配比正则模式优先级高,支持退出服务器匹配后{server_namewebsite.com;location/doc{return702;}location/docu{return701;}}701前缀匹配按照最长匹配,不考虑订单历史模式、跨域、缓存、反向代理html设置历史模式location/{indexindex.htmlindex.htm;proxy_set_headerHost$host;这里最重要的是历史模式try_files$uri$uri//index.html;index.html文件不能设置强缓存设置协商缓存可以add_headerCache-Control'no-cache,must-revalidate,proxy-revalidate,max-age=0';}接口反向代理位置^~/api/{cross-域处理设置header域名add_headerAccess-Control-Allow-Origin*;跨域处理设置header方法add_headerAccess-Control-Allow-Methods'GET,POST,DELETE,OPTIONS,HE广告';重写路径重写^/api/(.*)$/$1中断;反向代理proxy_passhttp://static_env;proxy_set_headerHost$http_host;}location~*.(?:css(.map)?|js(.map)?|gif|svg|jfif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)${静态资源设置7天强缓存过期7d;access_logoff;}使用目录来区分多个历史单文件因为不可能每个项目都开一个域名,所以只能通过添加路径来指向划分多个网站,比如:www.taobao.com/tmall/login访问天猫的登录页面www.taobao.com/alipay/login访问支付宝的登录页面服务器{listen80;server_nametaobao.com;indexindex.htmlindex.htm;通过正则来匹配捕获[tmall|alipay]中间的路径位置~^/(1+)/(.*)${try_files$uri$uri//$1/dist/index.html=404;}}负载均衡是根据upstream来进行负载均衡的,中间会涉及到一些相关的策略,比如ip_hash,weightupstreambackserver{hash算法,自动定位服务器保证唯一ip在同一台机器上解决session登录状态ip_hash的问题;server127.0.0.1:9090down;(down表示订单前服务器暂时不参与负载)server127.0.0.1:8080weight=2;(权重默认为1,权重越大,负载的权重越大)server127.0.0.1:6060;server127.0.0.1:7070backup;(当所有其他非备机宕机或忙时,请求备机)}灰度部署如何根据headers进行灰度,下面的例子是使用cookies设置_web前端训练。如何获取header值在nginx中,可以通过$http_xxx获取变量。上游稳定{serverxxxmax_fails=1fail_timeout=60;serverxxxmax_fails=1fail_timeout=60;}upstreamcanara{serverxxxmax_fails=1fail_timeout=60;}server{listen80;server_namexxx;setdefaultset$group"stable";根据cookie头设置接入服务proxy_passhttp://$group;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;indexindex.htmlindex.htm;}}优雅降级常用于ssr节点的服务挂了返回500错误码然后降级到csr的cos桶或者nginx中优雅降级。error_page参数主要用于降级到备份地址。上游ssr{serverxxxmax_fails=1fail_timeout=60;serverxxxmax_fails=1fail_timeout=60;}upstreamcsr{serverxxxmax_fails=1fail_timeout=60;serverxxxmax_fails=1fail_timeout=60;}location^~/ssr/{proxy_passhttp://ssr;开启自定义错误捕获如果这里没有设置为on,则会转到nginx处理上的默认错误页面proxy_intercept_errors;capture500serieserrors如果500错误,会降级为如下csr渲染error_page500501502503504=@csr_locationerror_page500501502503504=200@csr_location注意等号前面没有200,意思是最后返回的状态码是基于@csr_location。加上200就是不管@csr_location返回什么,都会返回一个200的状态码}location@csr_location{此时地址还有/ssr/去掉rewrite^/ssr/(.*)$/$1break;proxy_passhttp://csr;rewrite_logon;}webp根据浏览器自动降级为png。这套方案不像常见的nginx将png转webp的方案,而是先通过图床系统(节点服务)上传两张图片:一张是png原图,一张是png压缩后的图片到webp(使用的是imagemin-webp)然后使用nginx检测header是否支持webp返回webp图片。如果不支持,返回原图。还实现了错误拦截。如果cos桶丢了webp图片,浏览器支持webp,也会降级为png。http{include/etc/nginx/mime.types;default_type应用程序/八位字节流;设置日志格式log_formatmain'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"''"$proxy_host""$upstream_addr"';access_log/var/log/nginx/access.logmain;sendfileon;keepalive_timeout65;opengzipgzipon;gzip_varyon;gzip_proxiedany;gzip_comp_level6;gzip_typestext/plaintext/csstext/xmlapplication/jsonapplication/javascriptapplication/rss+xmlapplication/atom+xmlimage/svg+xml;这里的负载均衡可以是多个cosbucket地址可以是upstreamstatic_env{serverxxx;serverxxx;}map设置变量映射。第一个变量是指要映射的键值Accpet。第二个值是变量别名map$http_accept$webp_suffix{默认为空字符串default"";Accep包含设置为.webp值的webp字段"~*webp"".webp";}server{listen8888;absolute_redirectoff;#取消绝对路径的重定向网站首页路径。该路径仅供参考,具体请以实际目录为准。root/usr/share/nginx/html;location/{indexindex.htmlindex.htm;proxy_set_headerHost$host;try_files$uri$uri//index.html;add_headerCache-Control'no-cache,max-age=0';}favicon.icolocation=/favicon.ico{log_not_foundoff;access_logoff;}robots.txtlocation=/robots.txt{log_not_foundoff;access_logoff;}location~*.(png|jpe?g)${通过WebPsupportheadertobackend如果header支持webpif($webp_suffix~*webp){先试试看有没有webp格式图片rewrite^/(.*).(png|jpe?g)$/$1.webpbreak;如果找不到,则捕获404错误并返回原始错误。注意这里的=号表示最后返回@static_img的状态。error_page404=@static_img;}proxy_intercept_errorson;add_headerVaryAccept;proxy_passhttp://static_env;proxy_set_headerHost$http_host;expires7d;access_logoff;}location@static_img{set$complete$schema$server_addr$request_uri;rewrite^/.+$$request_uribreak;proxy_passhttp://static_env;proxy_set_headerHost$http_host;expires7d;}assets,medialocation~*.(?:css(.map)?|js(.map)?|gif|svg|jfif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)${proxy_passhttp://static_env;proxy_set_headerHost$http_host;expires7d;access_logoff;}error_page500502503504/50x.html;location=/50x.html{root/usr/share/nginx/html;}}}/?
