您的位置:首页 > 运维架构 > Nginx

linux下nginx实现虚拟主机(3种方法:基于域名、基于端口、基于ip地址)

2014-08-05 17:24 1271 查看
在3.17日的时候已经写过一篇关于apahce的基于域名、端口、ip地址3种方式的虚拟主机实现。原理是一样的,现在记录nginx的虚拟主机这三种方式的实现。

系统版本为rhel5.6,nginx版本为1.1.6。

1.基于域名:

基于域名的方式,要先有dns服务器,这里为了方便,可以在/etc/hosts文件里面配置,把它当成dns就行了,可以参考3.17日那篇博客关于dns的配置或者其他博文也有。这里关于nginx的安装也略去。
[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 localhost
::1localhost6.localdomain6 localhost6
10.10.16.29www.xiaowei_1.com
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地址就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息