1、在/etc/nginx/conf.d/添加两个虚拟主机server1.conf、server2.conf
server1.conf:
server {
listen 443;
server_name 127.0.0.1;
location / {
root /home/testzq/app/code1;
index index.html;
}
}
server2.conf:
server {
listen 443;
server_name 127.0.0.1;
location / {
root /home/testzq/app/code2;
index index.html;
}
}
两配置文件的区别:
[root@zq conf.d]# diff server1.conf server2.conf
6c6
< root /home/testzq/app/code1;
---
> root /home/testzq/app/code2;
访问页面编写:
[root@zq app]# cat /home/testzq/app/code1/index.html
<html>
<head>
<meta charset="utf-8">
<title> server 1</title>
</head>
<body>
<h1>server 1</h1>
</body>
</html>
[root@zq app]# cat /home/testzq/app/code2/index.html
<html>
<head>
<meta charset="utf-8">
<title> server 2</title>
</head>
<body>
<h1>server 2</h1>
</body>
</html>
2、测试nginx配置文件及加载配置
[root@zq conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: [warn] conflicting server name "127.0.0.1" on 0.0.0.0:443, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@zq conf.d]# nginx -s reload -c /etc/nginx/nginx.conf
nginx: [warn] conflicting server name "127.0.0.1" on 0.0.0.0:443, ignored
3、访问https://ip/,看到的是 server 1,优先级取的是server1.conf
把server1.conf改名server3.conf,重加载nginx -s reload -c /etc/nginx/nginx.conf,再访问看到的是server 2,此时访问的是server2.conf
4、结论
Nginx读取配置文件的时候是按照文件名顺序进行读取的,server1.conf比server2.conf靠前。