nginx配置虚拟主机的三种方式

Nginx下,一个server标签就是一个虚拟主机。

1、基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站

2、基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台

3、基于ip的虚拟主机,几乎不用。

前提条件需要建立三种主机方式对应的目录以及index.html文件

[root@localhost data]# tree
\.
├── ip(基于ip)
│   └── index.html
├── num(基于端口)
│   └── index.html
└── www(基于域名)
    ├── 1.php
    ├── index2.html
    ├── index.html
    └── index.php

基于域名配置虚拟主机步骤

1.  windows本地hosts添加虚拟机ip地址对应的域名解析

C:\Windows\System32\drivers\etc\hosts文件最后添加

ip    域名     如192.168.159.132   123.com    321.com   num.com

2.    打开nginx配置文件添加server标签

[root@localhost data]# vi /usr/local/nginx/conf/nginx.conf

server {

  listen 80;

  server_name 123.com;

  index index.html;

  root /data/www;

}

3.    测试

浏览器输入123.com

返回/data/www/index.html中内容welcome to 123.com xuni yuming.

基于端口的虚拟主机配置:    使用端口来区分,浏览器使用域名或ip地址:端口号 访问:

1.    nginx配置添加

{

    listen 8000;

    server_name num.com;

    root /data/num;

}

2.    测试

输入:http://123.com:8000/ 

结果:this is number 8000 yuming!

基于ip地址的虚拟主机配置: 通过ip来访问,需要配置多个ip:

1.    配置第二个ip并查看

[root@localhost data]# ifconfig eth0:1  192.168.159.133

[root@localhost data]# ip addr
    eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:0d:68:64 brd ff:ff:ff:ff:ff:ff
   
inet 192.168.159.132/24 brd 192.168.159.255 scope global eth0
    inet 192.168.159.133/24 brd 192.168.159.255 scope global secondary eth0:1

    inet6 fe80::20c:29ff:fe0d:6864/64 scope link 
       valid_lft forever preferred_lft forever

2.    nginx配置添加

server

{

    listen 192.168.159.133:80;

    server_name 123.com;

    root /data/ip;

}

 3.    测试

输入:http://192.168.159.133/

结果:this is ip 192.168.159.133 yuming

猜你喜欢

转载自blog.csdn.net/weixin_39855998/article/details/83181660