在3.17日的时候已经写过一篇关于apahce的基于域名、端口、ip地址3种方式的虚拟主机实现。原理是同样的,如今记录nginx的虚拟主机这三种方式的实现。
php
系统版本为rhel5.6,nginx版本为1.1.6。
html
1.基于域名:
nginx
基于域名的方式,要先有dns服务器,这里为了方便,能够在/etc/hosts文件里面配置,把它当成dns就好了,能够参考3.17日那篇博客关于dns的配置或者其余博文也有。这里关于nginx的安装也略去。web
[root@nginx ~]# cat /etc/hosts ///在hosts文件中添加www.xiaowei_1.com、www.xiaowei_2.com两条域名浏览器
# Do not remove the following line, or various programs服务器
# that require network functionality will fail.并发
127.0.0.1localhost.localdomain localhostapp
::1localhost6.localdomain6 localhost6dom
10.10.16.29www.xiaowei_1.comcurl
10.10.16.29www.xiaowei_2.com
[root@nginx conf]# cp nginx.conf nginx.conf_bak ///备份nginx配置文件
[root@nginx conf]# cat nginx.conf
user nobody;
worker_processes 1; ///worker进程轻易不要调,不然会出问题,worker进程数不要大于cpu核数,这里的worker进程数调成了多少,对应的nginx就会生成多少worker进程。
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024; ///每一个进程最大的链接数,能够适当调高一点,最大值是65535,nginx最终的并发链接数是这里的每一个进程链接数乘以上面的worker进程数目
use epoll; ///使用epoll模式,nginx的强大最主要得益于用了epoll模式,固然nginx之因此牛逼最主要得益于它被用作了反向代理,(用作web服务器也很牛)。
}
http { ///将虚拟主机放到http节点里面,能够把配置文件所有放到http节点里面,也能够把虚拟主机的配置文件独立成一个配置文件,在http节点引用它,这里我选择了后者。
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/virtualhost.conf; ///我把虚拟主机的配置文件独立成virtualhost.conf,这个文件在extra目录里面,即这个文件放的位置是/usr/local/nginx/conf/extra/virtualhost.conf。
}
说明:实际生产中在nginx.conf的主配置文件中还能够添加其余配置信息,如添加gzip压缩配置、日志格式等等配置信息(优化配置),这里个人上面配置信息已经够用的了,有机会再贴出来并解释比较全的nginx配置文件。
如今要在/usr/local/nginx/conf目录下面建立extra目录,并在extra目录里面建立virtualhost.conf文件。
[root@nginx conf]# mkdir extra
[root@nginx extra]# cat virtualhost.conf ///这里我作了两个虚拟主机,分别是刚刚在hosts文件中配置的www.xiaowei_1.com域名和www.xiaowei_2.com域名。
server {
listen 80; ///监听主机中全部的80端口
server_name www.xiaowei_1.com; ///定义客户端要访问网站用到的域名
location / {
root html/xiaowei_1; ///定义网站www.xiaowei_1.com所在的位置,即在/usr/local/nginx/html/xiaowei_1目录里面。
index index.html index.php; ///定义索引文件,能够多加几个
}
}
server {
listen 80;
server_name www.xiaowei_2.com;
location / {
root html/xiaowei_2;
index index.html index.php;
}
}
[root@nginx html]# mkdir xiaowei_1 xiaowei_2
[root@nginx xiaowei_1]# cat index.html ///配置网站www.xiaowei_1.com的主页
11111
[root@nginx xiaowei_2]# cat index.html
222222
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload ///重启nginx服务,使上面的配置生效
因为这里我没有配置dns服务器,只是简单的在hosts文件里添加了两个域名信息,因此只能在本机上用域名的方式访问两个域名了,以下
[root@nginx ~]# curl http://www.xiaowei_1.com
11111
[root@nginx ~]# curl http://www.xiaowei_2.com
222222
结果代表,基于域名的虚拟主机配置成功,默认状况下本机的默认网站是virtualhost.conf文件中第一个server节点中对应的网站,以下结果:
[root@nginx ~]# curl http://localhost
11111
好的,到这里基于域名的虚拟主机配置方式配置成功!
2.基于端口:
基于端口也是很简单的,这里我直接上面“基于域名”的基础上在virtualhost.conf配置文件增长了两段基于端口的配置信息,也能够把上面的基于域名的配置信息所有删除,作一个纯基于端口的配置文件
[root@nginx extra]# cat virtualhost.conf
server {
listen 80;
server_name www.xiaowei_1.com;
location / {
root html/xiaowei_1;
index index.html index.php;
}
}
server {
listen 80;
server_name www.xiaowei_2.com;
location / {
root html/xiaowei_2;
index index.html index.php;
}
}
server { ///从这里开始增长下面的两个server字段
listen 8080;
location / {
root html/xiaowei_3; ///本机的ip是10.10.16.29,本机8080端口网站目录是xiaowei_3,下面会建立这个目录。
index index.html index.php;
}
}
server {
listen 8081;
location / {
root html/xiaowei_4;
index index.html index.php;
}
}
[root@nginx html]# mkdir xiaowei_3 xiaowei_4
[root@nginx xiaowei_3]# cat index.html
33333
[root@nginx xiaowei_4]# cat index.html
444444
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload
如今能够在本机或者其余机器的浏览器上输入http://10.10.16.29:8080或者http://10.10.16.29:8081会看到不一样的内容:(若是局域网内有dns服务器,在dns服务器上添加域名www.xiaowei.com对应ip地址10.10.16.29,则在浏览器上输入http://www.xiaowei.com:8080或者http://www.xiaowei.com:8081会有一样的效果)
[root@nginx xiaowei_4]# curl http://10.10.16.29:8080
33333
[root@nginx xiaowei_4]# curl http://10.10.16.29:8081
444444
结果代表经过端口访问也是成功的!
还能够将基于端口的配置信息独立出来成virtualport.conf,一样放在extra目录下,而后在nginx.conf配置文件中添加include extra/virtualport.conf,效果一样是能够经过端口访问的。
到这里,基于端口的虚拟主机配置成功了!
3.基于ip:
基于ip的方式就更简单了,只要将基于域名方式中的域名换成ip地址就能够了。