云溪的 blog
  • Archive
  • Media
  • Search
  • Tags
  • About
Home » Categories

Nginx

nginx location 优先级

location 优先级介绍 location 匹配方式有以下几种 location = /path/a/exact.png {}: 精确匹配 location ^~ /path/a/ {}: 优先前缀匹配(符合最长匹配原则) location ~ /Path?/ {} : 区分大小写正则匹配(首次匹配原则) location ~* /path?/ {} : 不区分大小写正则匹配(首次匹配原则) location /path/a/test.png {} : 前缀匹配(符合最长匹配原则) location 优先级匹配与配置的先后顺序无关,优先级排列顺序如下 精确匹配 > 优先前缀匹配 > 区分大小写正则匹配=不区分大小写正则匹配 > 前缀匹配 实例说明 location = /path/a/exact.png { [ configuration A ] } location ~ /Path?/ { [ configuration B ] } location ~* /path?/ { [ configuration C ] } 如果理解了优先级,将很容易得出如下结论: www.web.com/path/a/exact.png => 匹配 configuration A www.web.com/Path/a => 匹配 configuration B www.web.com/path/a => 匹配 configuration C 当我们发现能够很好的猜测各种 URL 所匹配各个 location 是否心中有些许窃喜,对 location 优先级的理解已经不再话下了?事实并非如此,关于 location 优先级,还有一件事你需要知道。 ...

May 19, 2021 · 1 min · 云溪

nginx 单站点多证书配置

问题 在同一 server 中同时设置了, a.com, b.com.c.com 三个域名,若三个域名都需要 https 访问,应当如何配置呢? 示例配置如下: server { listen 443 ssl; server_name a.com b.com c.com; ssl_certificate a.crt; ssl_certificate_key a.key; ... } 问题分析:如果在 ssl_certificate 与 ssl_certificate_key 的配置项中可以增加变量,此问题就可以迎刃而解。 变量 ssl_server_name 经过 goole 发现 nginx 在 1.15.10 及以后的版本支持 ssl_server_name 变量,此变量输出的便是所访问 URL 的 host 部分(不包含端口及后面的 path 部分). URL ssl_server_name https://a.com/test a.com https://b.com:83/test b.com https://c.com/test c.com 有了 ssl_server_name 就可以很好的解决我们在文章开头描述的问题参考配置如下: server { listen 443 ssl; server_name a.com b.com c.com; ssl_certificate /crtPath/$ssl_server_name.crt; ssl_certificate_key /keyPath/$ssl_server_name.key; ... } 配置完成重启服务后,就可以用 https 访问对应的 server_name 了 ...

May 18, 2021 · 1 min · 云溪
© 2025 云溪的 blog