Nginx--虚拟机配置

前言:本博客仅作记录学习使用,部分图片出自网络,如有侵犯您的权益,请联系删除

1、什么是虚拟主机

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置:

  • 基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
  • 基于ip的虚拟主机 (一块主机绑定多个ip地址)
  • 基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

2、基于域名的虚拟主机

(1)配置通过域名区分的虚拟机

 [root@centos ~]# cat /etc/nginx/nginx.conf
 worker_processes auto;
 worker_rlimit_nofile 102400;
 events {
     worker_connections  1024;
 }
 ​
 http {
     include       mime.types;
     default_type  application/octet-stream;
     sendfile        on;
     keepalive_timeout  65;
 ​
     server {
         listen       80;
         server_name  web.testpm.com;    # 配置域名为web.testpm.com
 ​
         location / {
             root   /var/www/nginx;      # 根目录
             index  index.html index.htm;    # 文件
         }
     }
 ​
     server {
         listen       80;
         server_name  web.1000phone.com; # 配置域名为web.1000phone.com
 ​
         location / {
             root   /1000phone/html;     # 网站展示文件根目录
             index  index.html index.htm;    # 展示文件
         }
     }
 }

(2)为配置的两个域名创建目录与index文件

 [root@centos html]# cat /var/www/nginx/index.html
 This is the index.html Home of domain web.testpm.com!\n
 web.testpm.com!
 [root@centos html]# cat /1000phone/html/index.html
 This is the index.html Home of domain web.1000phone.com!\n
 web.1000phone.com!

(3)重新加载配置文件

 # 如果编译安装的执行
 [root@nginx]# /usr/local/nginx/sbin/nginx -s reload
 # 如果 yum 安装的执行
 [root@nginx]# nginx -s reload

(4)客户端配置路由映射

在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行 | (linux:/etc/hosts)

 # 主机IP 配置的域名
 10.0.0.2 web.testpm.com
 10.0.0.2 web.1000phone.com

(5)测试访问

浏览器分别输入http://web.testpm.comhttp://web.1000phone.com

3、基于IP的虚拟主机

(1)为网卡新增一个IP地址

 [root@centos ~]# ifconfig
 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 10.0.0.2  netmask 255.255.255.0  broadcast 10.0.0.255
         inet6 fe80::20c:29ff:fe39:6cf4  prefixlen 64  scopeid 0x20<link>
         ether 00:0c:29:39:6c:f4  txqueuelen 1000  (Ethernet)
         RX packets 99486  bytes 71872175 (68.5 MiB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 74475  bytes 5417252 (5.1 MiB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 [root@centos ~]# ifconfig ens33:1 10.0.0.3/24
 [root@centos ~]# ifconfig
 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 10.0.0.2  netmask 255.255.255.0  broadcast 10.0.0.255
         inet6 fe80::20c:29ff:fe39:6cf4  prefixlen 64  scopeid 0x20<link>
         ether 00:0c:29:39:6c:f4  txqueuelen 1000  (Ethernet)
         RX packets 99522  bytes 71875437 (68.5 MiB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 74505  bytes 5422160 (5.1 MiB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 ​
 ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 10.0.0.3  netmask 255.255.255.0  broadcast 10.0.0.255
         ether 00:0c:29:39:6c:f4  txqueuelen 1000  (Ethernet)

(2)配置通过IP区分的虚拟机

 [root@centos ~]# cat /etc/nginx/nginx.conf
 ...
     server {
         listen       10.0.0.2:80;       # 修改网站监听IP为10.0.0.2:80
         server_name  web.testpm.com;
 ​
         location / {
             root   /var/www/nginx;
             index  index.html index.htm;
         }
     }
 ​
     server {
         listen       10.0.0.3:80;       # 修改此处
         server_name  web.1000phone.com;
 ​
         location / {
             root   /1000phone/html;
             index  index.html index.htm;
         }
     }
 }

(3)重新加载配置文件

nginx -t && nginx -s reload

(4)客户端配置路由映射

 # 主机IP 配置的域名
 10.0.0.2 web.testpm.com
 10.0.0.2 web.1000phone.com
 10.0.0.3 web.1000phone.com

(5)测试访问

4、基于端口的虚拟主机

(1)编辑配置文件并重新加载

 [root@centos ~]# cat /etc/nginx/nginx.conf
 ...
     server {
         listen       80;        # 设置80端口
         server_name  web.testpm.com;
 ​
         location / {
             root   /var/www/nginx;
             index  index.html index.htm;
         }
     }
 ​
     server {
         listen       8080;      # 设置为8080端口
         server_name  web.1000phone.com;
 ​
         location / {
             root   /1000phone/html;
             index  index.html index.htm;
         }
     }
 }
 [root@centos ~]# nginx -s reload

(2)测试访问

致谢

在此,我要对所有为知识共享做出贡献的个人和机构表示最深切的感谢。同时也感谢每一位花时间阅读这篇文章的读者,如果文章中有任何错误,欢迎留言指正。 

学习永无止境,让我们共同进步!!​​​​​​​

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李学不完

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值