server_name
用来匹配请求中的host参数,并提供uri解析。意味着使用server_name可以在同一个端口上,为数个不同域名的站点或者域名同时提供服务。支持域名,通配符,正则表达式与其他变量。
server {
listen 80;
server_name example.org www.example.org;
...
}
server {
listen 80;
server_name *.example.org;
...
}
server {
listen 80;
server_name mail.*;
...
}
server {
listen 80;
server_name ~^(?<user>.+)\.example\.net$;
...
}
以上示例都是可用的。并且会按照全匹配→前方有通配符→后方有通配符→正则表达式的顺序进行匹配。
其中其他变量还支持
- IP地址 类似使用域名访问 与localhost效果不同
- “” 请求中没有指定host
当servername设置为 localhost 或者“”等无效域名时,nginx会默认使用第一个server_name进行解析。
location的匹配规则
说完了域名,来说说域名后面的部分是怎么匹配的。
当server_name完成对请求的初步筛选之后,就轮到location部分来进行域名下,剩余部分的匹配。
location的语法:
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
location的优先级
按照以下的顺序进行匹配:
- = 为最高优先级的精确匹配。
- 精确匹配
- ^~ 从开头开始进行正则匹配,使用最长优先匹配原则。
- ~* 等正则表表达式匹配,使用最长优先匹配原则。
root与alias的区别
当使用nginx在本地索引文件时,有两种写法:
location /webpath/ {
root /path/;
}
location /webpath/page {
alias /path/;
}
- root 使用的路径其实是父级路径,也就是说如果访问路径是:http://example.com/webpath/page ,在本地上实际查到的文件为 /path/webpath/page。
- alias 是location路径的映射,也就是说如果访问路径是:http://example.com/webpath/page ,在本地上实际查到的文件为 /path/page。
※ alias在配置不当的情况下,可能形成无限循环的路径。
proxy_pass的使用
location除了可以在本地索引资源外,还可以使用proxy_pass请求远端资源。
nginx的proxy配置参数繁多,这里单单只介绍proxy_pass的用法。
location /webpath1/ {
proxy_pass http://example.com; # 实际请求到远端为 http://example.com/webpath1/
}
location /webpath2/ {
proxy_pass http://example.com/webpath4/; # 实际请求到远端为 http://example.com/webpath4/childpath
}
location /webpath3/ {
proxy_pass example-server/webpath5/; #需要配合upstream模块使用 在upstream里定义example-server的地址
}
- proxy_pass的地址可以直接写远端地址,当末尾不加 “ / “ 的时候,nginx会将location的匹配路径也拼接到远端地址的末尾,效果类似于拼接。
- proxy_pass的地址写远端地址,当末尾加 “ / “ 的时候,nginx不会将location匹配的路径拼接到远端地址末尾,实际效果类似于替换了路径。
- proxy_pass的地址用upstream模块定义的服务器来代替远端地址,并且可以加子路径。
用upstream进行负载均衡
上文刚刚介绍了upstream模块,该模块主要用来从远端请求资源来提供服务。在反向代理、负载均衡、4层端口转发中都使用了upstream模块。
示例:
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
上文中upstream模块定义了名为myapp1的服务,包含了3个主机,可以被下文中的配置调用。当服务中包含多个主机时,就会默认启用负载均衡的功能,如果只包含一台主机,则只有反向代理或者端口转发的效果。
如果upstream模块与调用不在http服务块里,而是在stream服务块里,则为4层的端口转发。
stream {
upstream nacos-grpc {
server 192.168.xxx.xxx:9848;
}
server {
listen 9848;
proxy_pass nacos-grpc;
}
}
负载均衡算法(不包含nginx+)
Round Robin - 轮询(权重)这是nginx默认的负载均衡算法,使用此算法不需要加任何的参数,只要加入服务器就行。
upstream backend {
# no load balancing method is specified for Round Robin
server backend1.example.com;
server backend2.example.com;
}
Least Connections - 最少连接,nginx会将新连接指向连接数最少的服务器上。
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
IP HASH - 根据IP进行流量的负载均衡,会将来自相同IP的请求发送到同一个服务器上,优点是可以保持会话。
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}
通用HASH - 根据用户定义的键值对进行负载均衡,通常用来解析uri参数,增加缓存服务的命中率。
upstream backend {
hash $request_uri consistent;
server backend1.example.com;
server backend2.example.com;
}
随机 - 随机选择服务器,如果指定了两个参数,会根据另一个参数先选择多个可用目标,再从里面随机选择一个。
upstream backend {
random two least_conn;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server backend4.example.com;
}
upstream的server参数
在使用upstream模块中添加服务器的时候,还可以添加以下几个比较常用的参数:
- backup - 当其他所有服务器不可用的时候,此服务器才参与负载均衡
- down - 该服务器不参与负载均衡
- weight=number - 该参数定了服务器的权重,权重越高,被访问的次数越多,可以简单的理解为每 (所有服务器的weight之和)次请求之中,该服务器会被访问 (weight)次。
- maxfails=_number - 负载均衡的health check参数, 请求多少次失败之后,将该服务器设为不可用。默认值为1。
- failtimeout=_time - 当该服务器不可用之后,在多长时间之内不使用该服务器。默认值为10s。
示例:
upstream backend {
server backend1.example.com backup ;
server backend2.example.com down;
server backend3.example.com weight=5 max_fails=3 fail_timeout=90s;
server backend4.example.com weight=7 max_fails=3 fail_timeout=90s;
server backend4.example.com weight=3 max_fails=9 fail_timeout=90s;
}