13. nginx的server模块
nginx的server模块¶
- 指令后可以跟多个域名,第一个是主域名
语法:server_name_in_redirect on|off
默认:off
- 泛域名仅支持在最前或者最后
server_name *.xxx.com
- 正则表达式:加~前缀
server_name ~^www.\d+.xxx.com$
server模块操作¶
- server_name_in_redirect on|off默认是关闭
server { server_name primary.abc.com second.abc.com; server_name_in_redirect on; return 302 /redirect; } 重定向 [root@localhost vhosts]# curl primary.abc.com -I HTTP/1.1 302 Moved Temporarily Server: nginx/1.14.2 Date: Sat, 27 Jun 2020 08:30:06 GMT Content-Type: text/html Content-Length: 161 Location: http://primary.abc.com/redirect Connection: keep-alive [root@localhost vhosts]# curl second.abc.com -I HTTP/1.1 302 Moved Temporarily Server: nginx/1.14.2 Date: Sat, 27 Jun 2020 08:30:10 GMT Content-Type: text/html Content-Length: 161 Location: http://primary.abc.com/redirect Connection: keep-alive
server_name中的变量使用¶
- 匿名变量小括号做分组
server{ server_name ~^(www\.)?(.+)$; location / { root /sites/$2; } } $2取第二个括号的变量
- 有名变量
server{ server_name ~^(www\.)?(?<dom>.+)$; location / { root /sites/$dom; } }
- 其他形式
.xxx.com 可以匹配xxx.com *.xxx.com _匹配所有 server_name _; "" 匹配没有传递HOST头部
server的匹配顺序¶
-
精确匹配
-
*在前的泛域名
-
*在后的泛域名
-
按文件中的顺序匹配正则表达式域名
-
default server