[原创]nginx多个host的简单配置

Nginx是什么?

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.(from http://wiki.nginx.org/Chs)

Nginx一般来说, 在windows下是无法体现其性能和优点的, 但是在windows下学习nginx的配置是一个不错的方法.

nginx的官方网站: http://nginx.org/, 目前最新版是1.0.1

本文的例子下载地址: https://files.cnblogs.com/icejoywoo/nginx-1.0.1.zip

运行方法:

  1. 修改hosts文件, 添加两个域名解析到本地
  2. 运行nginx.exe
  3. 在浏览器中输入localhost, 查看效果
  4. 输入icejoywoo.com, 查看效果
  5. 输入icejoywoo.org, 查看效果

首先认识一下hosts文件

windows xp下的路径

C:\WINDOWS\system32\drivers\etc\hosts

linux下的路径

/etc/hosts

用文本编辑器打开这个文件, 我在windows下使用的是notepad++, linux下可以使用emacs或vim

一般可以看到这样一句(linux下的这行会有些不同)

127.0.0.1 localhost

这个是用来解析localhost的, 一般来说, 域名解析的顺序是这样的.

  1. 在本地的hosts文件处解析
  2. DNS服务器解析

可以从顺序中看到hosts文件的优先级比DNS服务器高.

我们在hosts中添加两行

127.0.0.1 icejoywoo.org
127.0.0.1 icejoywoo.com

这样, 我们就可以在本地解析两个域名

完整的hosts文件如下

# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost
127.0.0.1 icejoywoo.org
127.0.0.1 icejoywoo.com

nginx.conf的配置如下

配置的效果是, 使用localhost或IP直接访问时候, 返回500, 服务器内部错误.

使用icejoywoo.org域名访问的是html/org目录

使用icejoywoo.com域名访问的是html/com目录

默认主页为index.html和index.htm

#user nobody;
worker_processes
1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections
1024;
}


http {
include mime
.types;
default_type application
/octet-stream;

sendfile
on;

#keepalive_timeout 0;
keepalive_timeout
65;

#gzip on;

server
{
listen
80 default;
server_name _
;
return 500;
}

server {
listen
80;
server_name icejoywoo
.com;

location
/ {
root html
/com;
index index
.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page
500 502 503 504 /50x.html;
location
= /50x.html {
root html
;
}
}
server {
listen
80;
server_name icejoywoo
.org;

#charset koi8-r;

#access_log logs/host.access.log main;

location
/ {
root html
/org;
index index
.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page
500 502 503 504 /50x.html;
location
= /50x.html {
root html
;
}

}
}
为什么Nginx的性能要比Apache高得多?这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的 Squid、Memcached都采用的是epoll网络I/O模型。

  处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。下面用一个比喻来解析Apache采用的select模型和Nginx采用的epoll模型进行之间的区别:

   假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。而epoll版 宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。如果来了10000个人,都要 找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之 一,select和epoll的性能谁的性能更高,同样十分明了。(摘自Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创])
原文地址:https://www.cnblogs.com/icejoywoo/p/2038756.html