准备工作:

[root@bogon ~]# mkdir -p /data/nginx{1..3} #-p是用于递归创建使用
[root@bogon ~]# echo "hello nginx1" > /data/nginx1/index.html
[root@bogon ~]# echo "hello nginx2" > /data/nginx2/index.html
[root@bogon ~]# echo "hello nginx3" > /data/nginx3/index.html

一、基于ip地址不同配置虚拟主机

首先,添加IP地址并启动网卡

[root@bogon conf.d]# nmcli connection modify ens33 +ipv4.addresses 192.168.238.141
[root@bogon conf.d]# nmcli connection modify ens33 +ipv4.addresses 192.168.238.151
[root@bogon conf.d]# nmcli connection up ens33

然后,在/data/nginx/conf.d/vhost.conf下配置(注意:要记得重新启动nginx

server {
        listen 192.168.238.131:80;
        server_name localhost;
        location / {
                root /data/nginx1;
                index index.html;
        }
}

server {
        listen 192.168.238.141:80;
        server_name localhost;
        location / {
                root /data/nginx2;
                index index.html;
        }
}

server {
        listen 192.168.238.151:80;
        server_name localhost;
        location / {
                root /data/nginx3;
                index index.html;
        }
}

实现效果

二、基于端口不同配置虚拟主机 

在/data/nginx/conf.d/vhost.conf下配置

server {
        listen 80;
        server_name localhost;
        location / {
                root /data/nginx1;
                index index.html;
        }
}

server {
        listen 81;
        server_name localhost;
        location / {
                root /data/nginx2;
                index index.html;
        }
}

server {
        listen 82;
        server_name localhost;
        location / {
                root /data/nginx3;
                index index.html;
        }
}

实现效果

三、基于域名不同配置虚拟主机

server {
        listen 192.168.238.131:80;
        server_name www.nginx1.com;
        location / {
                root /data/nginx1;
                index index.html;
        }
}

server {
        listen 192.168.238.131:80;
        server_name www.nginx2.com;
        location / {
                root /data/nginx2;
                index index.html;
        }
}

server {
        listen 192.168.238.131:80;
        server_name www.nginx3.com;
        location / {
                root /data/nginx3;
                index index.html;
        }
}

然后,在/etc/hosts下添加IP和域名

[root@bogon ~]# vi /etc/hosts

192.168.238.131   www.nginx1.com
192.168.238.131   www.nginx2.com
192.168.238.131   www.nginx3.com

实现效果

原文地址:https://blog.csdn.net/wzz20021027/article/details/134652381

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_630.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注