nginx server_name匹配规则


官网说明:http://nginx.org/en/docs/http/server_names.html


需要记住的是两点:

1)完全匹配,匹配成功则终止搜索

2)不匹配,默认走第1个server

3)如何禁止IP访问和非法域名访问


测试一:完全匹配和默认匹配

nginx配置如下:

worker_processes  1;

events {
    worker_connections  1024;
}

http {

    server {
        listen       80;
        server_name  test1.com;

        location / {
            return 500;
        }
    }

    server {
        listen       80;
        server_name  test2.com;

        location / {
            return 508;
        }
    }

}


1)curl -i "test1.com/"

查询server_name成功,默认匹配到第1个server,返回500

2)curl -i "test2.com/"

查询server_name成功,默认匹配到第2个server,返回508

3)curl -i "test3.com/"

查询server_name失败,默认匹配到第1个server,返回500


测试二:禁止非法域名和IP访问

nginx配置如下:

worker_processes  1;

events {
    worker_connections  1024;
}

http {

    server {
        listen       80;
        server_name  test1.com;
        location / {
            return 500;
        }
    }

    server {
        listen       80 default_server;
        server_name  _;
        return 403;
    }

}

1)curl -i "test1.com/"
查询server_name成功,返回500

2)curl -i "test2.com/"
查询server_name失败,返回403

3)curl -i "127.0.0.1/"
查询server_name失败,返回403


原文出自:http://blog.csdn.net/daiyudong2020/article/details/73863437


End;


你可能感兴趣的:(nginx)