40 Server_name通配符匹配配置
40 Server_name通配符匹配配置
40.1 通配符匹配
server_name中支持通配符"*",但需要注意的是通配符不能出现在域名的中间,只能出现在首段或尾段,如:
server { listen 80; server_name *.itcast.cn www.itheima.*; ........ }
注意:下面的配置会报错
server { listen 80; server_name www.*.cn www.itheima.c*; ........ }
40.2 演示1
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf .......... server { listen 80; #server_name www.nginx521.cn www.itcast.cn www.itheima.cn; server_name *.itcast.cn www.itheima.*; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8080 default_server; server_name _; default_type text/plain; return 444 'not found server!!!'; } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Windows:C:\Windows\System32\drivers\etc\HOSTS
CentOS:/etc/hosts
# 添加解析 10.0.0.100 www.nginx521.cn 10.0.0.100 www.itcast.cn 10.0.0.100 www.itheima.cn 10.0.0.100 abc.itcast.cn 10.0.0.100 www.itheima.com
浏览器访问:http://www.itcast.cn/

浏览器访问:http://abc.itcast.cn/

浏览器访问:http://www.itheima.cn/

浏览器访问:http://www.itheima.com/

40.3 演示2
第一种方式
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf .......... server { listen 80; server_name *w.itcast.cn; location / { root html; index index.html index.htm; } .......... [root@nginx-100 /usr/local/nginx/conf]# nginx -t nginx: [emerg] server name "*w.itcast.cn" is invalid in /usr/local/nginx/conf/nginx.conf:30 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
第二种方式
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf .......... server { listen 80; server_name www.*.cn; location / { root html; index index.html index.htm; } .......... [root@nginx-100 /usr/local/nginx/conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
第三种方式
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf .......... server { listen 80; server_name www.itcast.c*; location / { root html; index index.html index.htm; } .......... [root@nginx-100 /usr/local/nginx/conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
以上三种方式,都是错误配置,第一种配置会报错,第二三种,匹配失效,注意,千万别误配
———————————————————————————————————————————————————————————————————————————
无敌小马爱学习
浙公网安备 33010602011771号