Py学习  »  NGINX

【linux】循序渐进学运维-服务篇-nginx的虚拟主机

辛舒展 • 3 年前 • 238 次点击  

前言

前面两篇文章,我们已经探讨了nginx的安装方式,通过yum和编译来安装,今天我们来探讨下nginx的虚拟主机,本文中的nginx是通过yum的形式安装的

基于端口号的虚拟主机

1. 修改配置文件
[root@zmedu-17 rpm]# find /  -name nginx.conf
/etc/nginx/nginx.conf
  • 1
  • 2

mkdir -p /www/zmgaosh1 ### 创建目录
mkdir -p /www/zmgaosh2

server {
	listen 80;
        location / {
           root /www/zmgaosh1;
	   index index.html index.htm;
  }


}
    
    server {
	listen 8090;
        location / {
           root /www/zmgaosh2;
	   index index.html index.htm;
  }

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

插入的位置如图所示:

在这里插入图片描述

2. 重启测试

[root@zmedu-17 rpm]# systemctl restart nginx

[root@zmedu-17 rpm]# echo "zmgaosh1 " > /www/zmgaosh1/index.html
[root@zmedu-17 rpm]# echo "zmgaosh2 " > /www/zmgaosh2/index.html

在这里插入图片描述
在这里插入图片描述

基于ip的虚拟主机

设置临时ip

[root@zmedu-17 rpm]# ifconfig ens32:1 192.168.1.23/24
[root@zmedu-17 rpm]# ifconfig ens32:2 192.168.1.22/24
在这里插入图片描述

server {
	listen 192.168.1.22:80;
        location / {
           root /www/zmgaosh1;
	   index index.html index.htm;
  }


}
    
    server {
	listen 192.168.1.23:80;
        location / {
           root /www/zmgaosh2;
	   index index.html index.htm;
  }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

测试
在这里插入图片描述

在这里插入图片描述

基于域名的虚拟主机

在这里插入图片描述

server {
	listen 80;
        server_name www.zmgaosh1.com;
        location / {
           root /www/zmgaosh1;
	   index index.html index.htm;
  }


}
    
    server {
	listen 80;
        server_name www.zmgaosh2.com;
        location / {
           root /www/zmgaosh2;
	   index index.html index.htm;
  }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

[root@zmedu-17 rpm]# systemctl restart nginx

在基于域名的虚拟主机设置的时候,一定要注意每一行后面有个分号;

cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.17 www.zmgaosh1.com   www.zmgaosh2.com
  • 1
  • 2
  • 3
  • 4
  • 5

测试:

[root@zmedu-17 zmgaosh3]# curl www.zmgaosh1.com
zmgaosh1 

[root@zmedu-17 zmgaosh3]# curl www.zmgaosh2.com
zmgaosh2
  • 1
  • 2
  • 3
  • 4
  • 5

总结

nginx的虚拟主机和apache的虚拟主机一样,都是三种,设置方式也基本一样,测试方法也一样。

所以说学习运维,我们最重要的是学习一种方法,研究透一个之后,其他的都大同小异。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/72028
 
238 次点击