Nginx虚拟主机配置详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx虚拟主机配置详解相关的知识,希望对你有一定的参考价值。

nginx虚拟主机配置详解

一、虚拟主机介绍

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功能(WWWFTPEmail等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。

利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程。虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。

 

二、虚拟主机分类

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

1、基于IP的虚拟主机(几乎不用)

实现基于ip的虚拟主机,可以在一块物理网卡上绑定多个lP地址。这样就能够在使用单一网卡的同一个服务器上运行多个基于IP的虚拟主机。设置IP别名也非常容易,只须配置系统上的网络接口,让它监听额外的lP地址。

 

2、基于域名的虚拟主机(经常使用)

基于域名的虚拟主机是最常见的一种虚拟主机。只需配置你的DNS服务器或者编辑/etc/hosts加入虚拟域名,将每个主机名映射到正确的lP地址,然后配置Nginx服务器,令其识别不同的主机名就可以了。这种虚拟主机技术,使很多虚拟主机可以共享同一个lP地址,有效解决了lP地址不足的问题。所以,如果没有特殊要求使你必须用一个基于lP的虚拟主机,最好还是使用基于域名的虚拟主机。

 

3、基于端口的虚拟主机(很少使用)

基于端口的虚拟主机配置,使用端口来区分,浏览器使用域名或ip地址后面加上端口号访问。一般用于公司内部网站,外部网站的管理后台)

 

三、虚拟主机的配置方法

1、基于域名的虚拟主机配置

需要配置的域名:www.long.com  blog.long.com

对应的目录为:wwwblog

创建虚拟主机目录

[[email protected] ~]# mkdir /usr/local/nginx-1.12.1/html/blog

[[email protected] ~]# mkdir /usr/local/nginx-1.12.1/html/www

 

创建一个简单的网页文件

[[email protected] ~]# echo 'www.long.com' >/usr/local/nginx-1.12.1/html/www/index.html

[[email protected] ~]# echo 'blog.long.com' >/usr/local/nginx-1.12.1/html/blog/index.html

 

添加/etc/hosts域名解析

[[email protected] ~]# vim /etc/hosts

192.168.10.10  www.long.com

192.168.10.10  blog.long.com

 

修改nginx主配置文件,在http大括号中添加虚拟主机

[[email protected] ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf

server {

       root   html/www;

       listen       80;

       server_name  www.long.com;

       index  index.html index.htm;

  }

 

server {

       root   html/blog;

       listen       80;

       server_name  blog.long.com;

       index  index.html index.htm;

  }

 

重新加载nginx,然后测试

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload

[[email protected] ~]# curl www.long.com

www.long.com

[[email protected] ~]# curl blog.long.com

blog.long.com

 

2、基于端口的虚拟主机配置

需要配置的端口:80808081

对应的网页目录:80808081

创建虚拟主机目录

[[email protected] ~]# mkdir /usr/local/nginx-1.12.1/html/8080

[[email protected] ~]# mkdir /usr/local/nginx-1.12.1/html/8081

 

创建一个简单的网页文件

[[email protected] ~]# echo 'this is 8080' >/usr/local/nginx-1.12.1/html/8080/index.html

[[email protected] ~]# echo 'this is 8081' >/usr/local/nginx-1.12.1/html/8081/index.html

 

修改nginx主配置文件,在http大括号中添加虚拟主机

[[email protected] ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf

server {

       root   html/8080;

       listen       8080;

       server_name  www.long.com;

       index  index.html index.htm;

  }

 

server {

       root   html/8081;

       listen       8081;

       server_name  www.long.com;

       index  index.html index.htm;

  }

 

重新加载nginx,然后测试

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload

[[email protected] ~]# curl 192.168.10.10:8080

this is 8080

[[email protected] ~]# curl 192.168.10.10:8081

this is 8081

 

3、基于IP的虚拟主机配置

需要配置的IP地址:192.168.10.10192.168.10.11

对应的网页目录:192.168.10.10192.168.10.11

 

创建虚拟主机目录

[[email protected] ~]# mkdir /usr/local/nginx-1.12.1/html/192.168.10.10

[[email protected] ~]# mkdir /usr/local/nginx-1.12.1/html/192.168.10.11

 

创建一个简单的网页文件

[[email protected] ~]# echo '192.168.10.10' >/usr/local/nginx-1.12.1/html/192.168.10.10/index.html

[[email protected] ~]# echo '192.168.10.11' >/usr/local/nginx-1.12.1/html/192.168.10.11/index.html

 

配置一个子网IP地址

[[email protected] ~]# ifconfig eno16777736:1 192.168.10.11/24

 

修改nginx主配置文件,在http大括号中添加虚拟主机

[[email protected] ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf

server {

       root   html/192.168.10.10;

       listen       192.168.10.10:80;

       server_name  www.long.com;

       index  index.html index.htm;

  }

 

server {

       root   html/192.168.10.11;

       listen       192.168.10.11:80;

       server_name  www.long.com;

       index  index.html index.htm;

  }

 

重新加载nginx,然后测试

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload

[[email protected] ~]# curl 192.168.10.10

192.168.10.10

[[email protected] ~]# curl 192.168.10.11

192.168.10.11

 

以上就是三种虚拟主机的配置方法。


以上是关于Nginx虚拟主机配置详解的主要内容,如果未能解决你的问题,请参考以下文章

Nginx-3.nginx配置文件和虚拟主机

LAMP架构(nginx安装,默认虚拟主机,用户认证,域名重定向,nginx配置文件详解)

nginx虚拟主机配置

基于Nginx搭建Web服务器及虚拟主机相关配置详解

Nginx配置详解

Nginx配置详解