location的匹配规则

  1. = 表示精确匹配。只有请求的url路径与后面的字符串完全相等时,才会命中。
  2. ^~ 表示如果该符号后面的字符是最佳匹配,采用该规则,不再进行后续的查找。
  3. ~ 表示该规则是使用正则定义的,区分大小写。
  4. ~* 表示该规则是使用正则定义的,不区分大小写。

注意的是,nginx的匹配优先顺序按照上面的顺序进行优先匹配,而且注意的是一旦某一个匹配命中直接退出,不再进行往下的匹配
剩下的普通匹配会按照最长匹配长度优先级来匹配,就是谁匹配的越多就用谁。

  1. server {
  2. server_name website.com;
  3. location /document {
  4. return 701;
  5. }
  6. location ~* ^/docume.*$ {
  7. return 702;
  8. }
  9. location ~* ^/document$ {
  10. return 703;
  11. }
  12. }
  13. curl -I website.com:8080/document 702
  14. # 匹配702 因为正则的优先级更高,而且正则是一旦匹配到就直接退出 所以不会再匹配703
  1. server {
  2. server_name website.com;
  3. location /doc {
  4. return 702;
  5. }
  6. location /docu {
  7. return 701;
  8. }
  9. }
  10. # 701 前缀匹配匹配是按照最长匹配,跟顺序无关

html设置history模式

  1. # html设置history模式
  2. location / {
  3. index index.html index.htm;
  4. proxy_set_header Host $host;
  5. # history模式最重要就是这里
  6. try_files $uri $uri/ /index.html;
  7. # index.html文件不可以设置强缓存 设置协商缓存即可
  8. add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';
  9. }
  10. # 接口反向代理
  11. location ^~ /api/ {
  12. # 跨域处理 设置头部域名
  13. add_header Access-Control-Allow-Origin *;
  14. # 跨域处理 设置头部方法
  15. add_header Access-Control-Allow-Methods 'GET,POST,DELETE,OPTIONS,HEAD';
  16. # 改写路径
  17. rewrite ^/api/(.*)$ /$1 break;
  18. # 反向代理
  19. proxy_pass http://static_env;
  20. proxy_set_header Host $http_host;
  21. }
  22. 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)$ {
  23. # 静态资源设置七天强缓存
  24. expires 7d;
  25. access_log off;
  26. }

以目录去区分多个history单文件

因为不可能每一个项目开启一个域名,仅仅指向通过增加路径来划分多个网站,比如:

  1. www.taobao.com/tmall/login访问天猫的登录页面
  2. www.taobao.com/alipay/login访问支付宝的登录页面 ```nginx server { listen 80; server_name taobao.com; index index.html index.htm;

    通过正则来匹配捕获 [tmall|alipay]中间的这个路径

    location ~ ^/([^\/]+)/(.*)$ {
    1. try_files $uri $uri/ /$1/dist/index.html =404;
    } }
  1. <a name="twE8c"></a>
  2. ## 负载均衡
  3. 基于upstream做负载均衡,中间会涉及一些相关的策略比如ip_hash、weight
  4. ```nginx
  5. upstream backserver{
  6. # 哈希算法,自动定位到该服务器 保证唯一ip定位到同一部机器 用于解决session登录态的问题
  7. ip_hash;
  8. server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载)
  9. server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大)
  10. server 127.0.0.1:6060;
  11. server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)
  12. }

灰度部署

如何根据headers头部来进行灰度,下面的例子是用cookie来设置
如何获取头部值在nginx中可以通过$http_xxx来获取变量

  1. upstream stable {
  2. server xxx max_fails=1 fail_timeout=60;
  3. server xxx max_fails=1 fail_timeout=60;
  4. }
  5. upstream canara {
  6. server xxx max_fails=1 fail_timeout=60;
  7. }
  8. server {
  9. listen 80;
  10. server_name xxx;
  11. # 设置默认
  12. set $group "stable";
  13. # 根据cookie头部设置接入的服务
  14. if ($http_cookie ~* "tts_version_id=canara"){
  15. set $group canara;
  16. }
  17. if ($http_cookie ~* "tts_version_id=stable"){
  18. set $group stable;
  19. }
  20. location / {
  21. proxy_pass http://$group;
  22. proxy_set_header Host $host;
  23. proxy_set_header X-Real-IP $remote_addr;
  24. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25. index index.html index.htm;
  26. }
  27. }

优雅降级

常用于ssr的node服务挂了返回500错误码然后降级到csr的cos桶或者nginx中
优雅降级主要用error_page参数来进行降级指向备用地址。

  1. upstream ssr {
  2. server xxx max_fails=1 fail_timeout=60;
  3. server xxx max_fails=1 fail_timeout=60;
  4. }
  5. upstream csr {
  6. server xxx max_fails=1 fail_timeout=60;
  7. server xxx max_fails=1 fail_timeout=60;
  8. }
  9. location ^~ /ssr/ {
  10. proxy_pass http://ssr;
  11. # 开启自定义错误捕获 如果这里不设置为on的话 会走向nginx处理的默认错误页面
  12. proxy_intercept_errors on;
  13. # 捕获500系列错误 如果500错误的话降级为下面的csr渲染
  14. error_page 500 501 502 503 504 = @csr_location
  15. # error_page 500 501 502 503 504 = 200 @csr_location
  16. # 注意这上面的区别 等号前面没有200 表示 最终返回的状态码已 @csr_location为准 加了200的话表示不管@csr_location返回啥都返回200状态码
  17. }
  18. location @csr_location {
  19. # 这时候地址还是带着/ssr/的要去除
  20. rewrite ^/ssr/(.*)$ /$1 break;
  21. proxy_pass http://csr;
  22. rewrite_log on;
  23. }

webp根据浏览器自动降级为png

这套方案不像常见的由nginx把png转为webp的方案,而是先经由图床系统(node服务)上传两份图片:

  1. 一份是原图png
  2. 一份是png压缩为webp的图片(使用的是imagemin-webp)

然后通过nginx检测头部是否支持webp来返回webp图片,不支持的话就返回原图即可。这其中还做了错误拦截,如果cos桶丢失webp图片及时浏览器支持webp也要降级为png

  1. http {
  2. include /etc/nginx/mime.types;
  3. default_type application/octet-stream;
  4. # 设置日志格式
  5. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  6. '$status $body_bytes_sent "$http_referer" '
  7. '"$http_user_agent" "$http_x_forwarded_for"'
  8. '"$proxy_host" "$upstream_addr"';
  9. access_log /var/log/nginx/access.log main;
  10. sendfile on;
  11. keepalive_timeout 65;
  12. # 开启gzip
  13. gzip on;
  14. gzip_vary on;
  15. gzip_proxied any;
  16. gzip_comp_level 6;
  17. gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
  18. # 负载均衡 这里可以是多个cos桶地址即可
  19. upstream static_env {
  20. server xxx;
  21. server xxx;
  22. }
  23. # map 设置变量映射 第一个变量指的是要通过映射的key值 Accpet 第二个值的是变量别名
  24. map $http_accept $webp_suffix {
  25. # 默认为 空字符串
  26. default "";
  27. # 正则匹配如果Accep含有webp字段 设置为.webp值
  28. "~*webp" ".webp";
  29. }
  30. server {
  31. listen 8888;
  32. absolute_redirect off; #取消绝对路径的重定向
  33. #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
  34. root /usr/share/nginx/html;
  35. location / {
  36. index index.html index.htm;
  37. proxy_set_header Host $host;
  38. try_files $uri $uri/ /index.html;
  39. add_header Cache-Control 'no-cache, max-age=0';
  40. }
  41. # favicon.ico
  42. location = /favicon.ico {
  43. log_not_found off;
  44. access_log off;
  45. }
  46. # robots.txt
  47. location = /robots.txt {
  48. log_not_found off;
  49. access_log off;
  50. }
  51. #
  52. location ~* \.(png|jpe?g)$ {
  53. # Pass WebP support header to backend
  54. # 如果header头部中支持webp
  55. if ($webp_suffix ~* webp) {
  56. # 先尝试找是否有webp格式图片
  57. rewrite ^/(.*)\.(png|jpe?g)$ /$1.webp break;
  58. # 找不到的话 这里捕获404错误 返回原始错误 注意这里的=号 代表最终返回的是@static_img的状态吗
  59. error_page 404 = @static_img;
  60. }
  61. proxy_intercept_errors on;
  62. add_header Vary Accept;
  63. proxy_pass http://static_env;
  64. proxy_set_header Host $http_host;
  65. expires 7d;
  66. access_log off;
  67. }
  68. location @static_img {
  69. #set $complete $schema $server_addr $request_uri;
  70. rewrite ^/.+$ $request_uri break;
  71. proxy_pass http://static_env;
  72. proxy_set_header Host $http_host;
  73. expires 7d;
  74. }
  75. # assets, media
  76. 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)$ {
  77. proxy_pass http://static_env;
  78. proxy_set_header Host $http_host;
  79. expires 7d;
  80. access_log off;
  81. }
  82. error_page 500 502 503 504 /50x.html;
  83. location = /50x.html {
  84. root /usr/share/nginx/html;
  85. }
  86. }
  87. }