nginx虚拟主机配置小结

  nginx的安装在lnmp环境搭建中已经介绍过了,配置文件在安装目录下的conf子目录下,主要主要分成四部分:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)、location(URL 匹配特定位置的设置)。可以狭义的理解一个server就是一个虚拟主机。主要有三种配置虚拟主机的方式:基于域名、基于端口、基于ip。下面分别介绍:

一、基于域名:

主要步骤:1.首先在windows本地hosts添加虚拟机ip地址对应的域名解析

      192.168.1.104 z.com

2.对应域名网站目录下新增index.html文件

     3.修改nginx.conf配置文件 添加如下信息保存退出

server {

listen 80;

server_name z.com;#

location /{

root z.com;#相对路径 nginx的安装目录

index index.html;

  }

}

4.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful

5.重启nginx #/app/local/nginx/sbin/nginx -s reload

    6.通过curl命令或者浏览器输入域名访问

# curl -xlocalhost:80 z.com

    <html>

    This is z.com

    </html>

二、基于端口: 使用端口来区分,浏览器使用域名或ip地址:端口号来访问

主要步骤:1.新建目录 mkdir /var/tmp/www 并且在该目录下新建一个index.html文件 添加  

<html>

welcome to z.com'spanel

</html>

2.修改nginx.conf配置文件 添加如下信息保存退出

      server {

   listen 2022;

  server_name localhost;

  location / {

  root /var/tmp/www;

  index index.html;

  }

}

    3.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful

4.重启nginx #/app/local/nginx/sbin/nginx -s reload

    5.通过curl命令或者浏览器输入域名访问

# curl http://192.168.1.104:2022

    <html>

    welcome to z.com'spanel

    </html>

三、基于IP

主要步骤:1.添加ip地址

   #ifconfig |grep "inet addr"

inet addr:192.168.1.104 Bcast:192.168.1.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

   #ifconfig eth0:0 192.168.1.220 netmask 255.255.255.0 up

   # ifconfig |grep "inet addr"

inet addr:192.168.1.104 Bcast:192.168.1.255 Mask:255.255.255.0

inet addr:192.168.1.220 Bcast:192.168.1.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

    2.新建目录 mkdir /app/local/nginx/html/ip并且在该目录下新建一个index.html文件 添加  

<html>

this is from ip

</html>

    3.修改nginx.conf配置文件 添加如下信息保存退出

server {

listen 80;

server_name 192.168.1.220;

location /{

root html/ip;

index index.html;

}

}

   4.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful

5.重启nginx #/app/local/nginx/sbin/nginx -s reload

   6.通过curl命令或者浏览器输入域名访问

# curl http://192.168.1.220  

  <html>

  this is from ip

  </html>