1、添加一个IP(必须要在同一个网卡上添加)
[root@localhost ~]# ip a #查看本机IP以及网卡名
[root@localhost ~]# ip a add 172.25.0.25 dev ens33 #添加多一个IP
2、修改Nginx配置文件,default(这里只显示上半部分需要修改的内容)
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 172.25.0.1:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /opt/code;
index index.html index.htm;
}
复制一份配置文件
[root@localhost ~]# cp -p /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default1.conf
修改其中的IP以及路径(这里只显示上半部分需要修改的内容)
[root@localhost ~]# vim /etc/nginx/conf.d/default1.conf
server {
listen 172.25.0.25:80; #修改IP
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /opt/app/code5; #修改文件路径
index index.html index.htm;
}
4、在default.conf文件中指定的路径中创建一个名为admin.html文件
[root@localhost ~]# vim /opt/code/admin.html
<html>
<head>
<meta charset="utf-8">
<title>vincen</title>
</head>
<body>
<a>nginx</a>
<a>172.25.0.20</a>
</body>
</html>
在default1.conf文件中指定的路径创建一个名为admin.html文件
[root@localhost ~]# vim /opt/app/code5/admin.html
<html>
<head>
<meta charset="utf-8">
<title>VINCEN</title>
</head>
<body>
<a>NGINX</a>
<a>172.25.0.25</a>
</body>
</html>
5、检查Nginx语法
[root@localhost ~]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
6、重载服务
[root@localhost ~]# nginx -s reload -c /etc/nginx/nginx.conf
7、浏览器访问两个不同IP
访问两个不同IP显示出不同的网页内容,此时Nginx基于IP的虚拟主机成功