当前位置: 首页 > 科技观察

说说Nginx前端是干什么的

时间:2023-03-22 16:48:24 科技观察

这篇文章是为了收集自己在工作中经常用到的nginx相关知识点。本文不是基础知识的讲解,更多的是一些解决方案中的简单实现。location=的匹配规则表示完全匹配。只有当请求的url路径正好等于后面的字符串时,才会命中。^~表示如果符号后面的字符是最佳匹配,则采用该规则,不再进行后续查找。~表示规则是使用正则表达式定义的,并且区分大小写。~*表示规则是使用不区分大小写的正则表达式定义的。需要注意的是,nginx的匹配优先级是按照上面的顺序来的,需要注意的是,一旦某一个匹配命中,直接退出,就不会再进行匹配,其余的普通匹配都会按照最长匹配长度优先。匹配就是谁匹配的多就用谁。服务器{server_namewebsite.com;位置/文件{返回701;}location~*^/docume.*${return702;}location~*^/document${return703;}}curl-Iwebsite.com:8080/document702#匹配702因为正则优先级高,一旦匹配到正则就直接退出,所以不会匹配703server{server_namewebsite.com;位置~*^/docume.*${return701;}位置^~/doc{返回702;}location~*^/document${return703;}}curlhttp://website.com/documentHTTP/1.1702#匹配702因为^~精确匹配优先级高于正则匹配后支持退出服务器{server_namewebsite.com;位置/文档{返回702;}位置/docu{返回701;}}#701前缀匹配是根据最长匹配,与订单历史模式、跨域、缓存、反向代理无关#htmlsethistorymodelocation/{indexindex.htmlindex.htm;proxy_set_header主机$host;#历史模式在这里是最重要的try_files$uri$uri//index.html;#index.html文件不允许设置强缓存和设置协商缓存到add_headerCache-Control'no-cache,must-revalidate,proxy-revalidate,max-age=0';}#接口反向代理位置^~/api/{#跨域处理设置header域名add_headerAccess-Control-Allow-Origin*;#跨域处理设置header方法add_headerAccess-Control-Allow-Methods'GET,POST,DELETE,OPTIONS,HEAD';#重写路径rewrite^/api/(.*)$/$1break;#反向代理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)${#静态资源设置七日强缓存过期7d;access_logoff;}使用目录区分多个历史订单因为不可能每个项目都开一个域名,所以该文件只是通过添加路径指向划分多个网站,例如:www.taobao.com/tmall/login访问天猫的登录页面www.taobao.com/alipay/login访问支付宝登录页面服务器{listen80;server_nametaobao.com;indexindex.htmlindex.htm;#使用正则模式匹配捕获[tmall|alipay]位置中间的路径~^/([^\/]+)/(.*)${try_files$uri$uri//$1/dist/index.html=404;}}负载均衡是基于upstream进行负载均衡,涉及到ip_hash、weightupstreambackserver{#hash算法等相关策略,自动定位服务器保证IP唯一去同机解决session登录状态ip_hash的问题;服务器127.0.0.1:9090宕机;(down表示单单前的服务器暂时不参与负载)server127.0.0.1:8080weight=2;(weight默认为1.weight越大,负载的权重越大)server127.0.0.1:6060;服务器127.0.0.1:7070备份;(当所有其他非备份机器都停机或忙碌时,请求备份机器)}标题是灰度的。下面的例子是使用cookies来设置header值的获取方式。在nginx中,$http_xxx可用于获取变量upstreamstable{serverxxxmax_fails=1fail_timeout=60;服务器xxxmax_fails=1fail_timeout=60;}upstreamcanara{serverxxxmax_fails=1fail_timeout=60;}server{listen80;服务器名称xxx;#设置默认值set$group"stable";#根据cookie头设置访问服务if($http_cookie~*"tts_version_id=canara"){set$groupcanara;}if($http_cookie~*"tts_version_id=stable"){设置$group稳定;}location/{proxy_passhttp://$group;proxy_set_header主机$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;indexindex.htmlindex.htm;}}优雅降级常用于ssr节点服务挂了返回500错误码然后降级到csr的cosbucket或者nginx优雅降级主要使用error_page参数降级并指向备份地址上游ssr{serverxxxmax_fails=1fail_timeout=60;服务器xxxmax_fails=1fail_timeout=60;}upstreamcsr{serverxxxmax_fails=1fail_timeout=60;服务器xxxmax_fails=1fail_timeout=60;}location^~/ssr/{proxy_passhttp://ssr;#启用自定义错误捕获这里如果不设置on,会转到nginx默认处理的错误页面proxy_intercept_errorson;#捕获500系列错误如果500错误退化为下面的csr渲染error_page500501502503504=@csr_location#error_page500501502503504=200@csr_location#注意等号前面没有200,表示最终返回的状态码是基于@csr_location。加200表示无论@csr_location返回什么都返回200状态码}location@csr_location{#此时地址还有/ssr/去掉rewrite^/ssr/(.*)$/$1break;proxy_passhttp://csr;rewrite_logon;}webp根据浏览这个方案不像一般的nginx将png转webp的方案,而是先通过图床系统(节点服务)上传两张图片:一张是原图png,一张是原图png是压缩了png的webp图片(使用imagemin-webp)然后使用nginx检测header是否支持webp返回webp图片。如果不支持,返回原图。还有一个错误拦截,如果cos桶丢了webp图片,浏览器支持webp,必须降级为pnghttp{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;发送文件;keepalive_timeout65;#启用gzipgzipon;gzip_vary开启;gzip_proxied任何;gzip_comp_level6;gzip_typestext/plaintext/csstext/xmlapplication/jsonapplication/javascriptapplication/rss+xmlapplication/atom+xmlimage/svg+xml;#这里的负载均衡可以是多个cosbucket地址可以是upstreamstatic_env{serverxxx;服务器xxx;}#map设置变量映射第一个变量是指要映射的keyValueAccpet第二个值是变量别名map$http_accept$webp_suffix{#默认为空字符串default"";#如果Accep包含设置为.webp值的webp字段则正则匹配"~*webp"".webp";}服务器{听8888;绝对重定向关闭;#取消绝对路径重定向#网站首页路径该路径仅供参考,具体请以实际目录为准。根/usr/share/nginx/html;location/{indexindex.htmlindex.htm;proxy_set_header主机$host;try_files$uri$uri//index.html;add_headerCache-Control'no-cache,max-age=0';}#favicon.icolocation=/favicon.ico{log_not_foundoff;access_log关闭;}#robots.txtlocation=/robots.txt{log_not_foundoff;access_log关闭;}#location~*\.(png|jpe?g)${#将WebP支持的header传递给后端#如果header支持webpif($webp_suffix~*webp){#首先尝试查找是否有webp格式的图片重写^/(.*)\.(png|jpe?g)$/$1.webp中断;#如果没有找到,这里捕获404错误,返回原来的错误注意这里的=号表示最后返回@static_img的状态error_page404=@static_img;}proxy_intercept_errors开启;add_header变化接受;proxy_passhttp://static_env;proxy_set_header主机$http_host;7天到期;access_log关闭;}location@static_img{#set$complete$schema$s服务器地址$request_uri;重写^/.+$$request_uri中断;proxy_passhttp://static_env;proxy_set_header主机$http_host;7天到期;}#资产,媒体位置~*\.(?: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_header主机$http_host;7天到期;access_log关闭;}error_page500502503504/50x.html;location=/50x.html{root/usr/share/nginx/html;}}}