nginx的虛擬主機配置跟apache的配置類似也有IP與域名配置兩種,下面我們就實踐一下;
一、基於域名的虛擬主機的配置;
1、我們在此複習一下DNS的配置;
[root@mgmserver /]# hostname
mgmserver.com
[root@/]#yum install bind*
[root@mgmserver /]#rpm -ivh /var/cdiso/Server/caching-nameserver-9.3.6-4.P1.el5.i386.rpm
[root@mgmserver /]# cd /var/named/chroot/etc/
[root@mgmserver etc]# cp -p named.caching-nameserver.conf named.conf
[root@mgmserver etc]#vi named.conf
其他的不用修改只在視圖裏面添加zone就可以了;
view localhost_resolver {
match-clients { any; };
match-destinations { any; };
recursion yes;
include "/etc/named.rfc1912.zones";
zone "mgmserver.com" {
type master;
file "mgmserver.com.zone";
allow-update { any;};
};
zone "0.168.192.in-addr.arpa" {
type master;
file "192.168.0.rev";
allow-update { any;};
};
[root@mgmserver etc]# cd ../var/named/
下面有很多模板文件。Named.ca就是根區域的數據庫文件,localhost.zone這個是正向解析的數據庫文件,named.local這個是反向解析的數據庫文件。
[root@mgmserver named]# cp localhost.zone mgmserver.com.zone
[root@mgmserver named]# cp named.local 192.168.0.rev
編輯 這兩個文件
[root@mgmserver named]#vi mgmserver.com.zone
$TTL 86400
@ IN SOA mgmserver.com. root.mgmserver.com. (
1997022700 ; Serial
28800 ; Refresh
14400 ; Retry
3600000 ; Expire
86400 ) ; Minimum
IN NS mgmserver.com.
IN MX 10 mail.mgmserver.com.
mail IN A 192.168.0.28
server IN A 192.168.0.33
www IN A 192.168.0.28
web IN CNAME www.mgmserver.com.
[root@mgmserver named]#vi 192.168.0.rev
$TTL 86400
@ IN SOA mgmserver.com. root.mgmserver.com. (
42 ; serial (d. adams)
3H ; refresh我
15M ; retry
1W ; expiry
1D ) ; minimum
IN NS mgmserver.com.
28 IN PTR mgmserver.com.
33 IN PTR server.mgmserver.com.
簡單的配置一下,我們要使用的www.mgmserver.com 與web.mgmserver.com這兩個域名來測試;
[root@mgmserver named]# named-checkconf /var/named/chroot/etc/named.conf
[root@mgmserver named]# service named restart
停止 named: [確定]
啓動 named: [確定]
我們到另外一臺機器測試
到這裏我們的爲nginx的配置www.mgmserver.com 與web.mgmserver.com這兩個域名都可以使用過了;
2、配置nginx配置文件,添加兩個虛擬主機
[root@mgmserver conf]# vi nginx.conf
[root@mgmserver www.mgmserver.com_web]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
[emerg]: "server" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:45
configuration file /usr/local/nginx/conf/nginx.conf test failed
第一次出現錯誤提示,查找原來是少了一個大括號,添加上就可以了;在檢測就OK了;
[root@mgmserver www.mgmserver.com_web]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@mgmserver www.mgmserver.com_web]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
啓動nginx
[root@mgmserver conf]# cd /var/web/
[root@mgmserver web]# ls
web.mgmserver.com_web www.mgmserver.com_web
在這兩個目錄裏面分別建立一個index.html文件保存
到另外一個server2003上面測試
兩個靜態頁面已基本OK;
二、基於IP的虛擬之際的配置;
[root@mgmserver conf]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:00:6C:BD:F3:00
inet addr:192.168.0.28 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::200:6cff:febd:f300/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:19200 errors:0 dropped:0 overruns:0 frame:0
TX packets:9925 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1680955 (1.6 MiB) TX bytes:1308926 (1.2 MiB)
Interrupt:50 Base address:0x4000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:269 errors:0 dropped:0 overruns:0 frame:0
TX packets:269 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:24484 (23.9 KiB) TX bytes:24484 (23.9 KiB)
[root@mgmserver ~]# ifconfig eth0:1 192.168.0.37 broadcast 192.168.0.255 netmask 255.255.255.0 up
[root@mgmserver ~]# route add -host 192.168.0.37 dev eth0:1
[root@mgmserver ~]# ifconfig eth0:2 192.168.0.38 broadcast 192.168.0.255 netmask 255.255.255.0 up
[root@mgmserver ~]# route add -host 192.168.0.38 dev eth0:2
[root@mgmserver ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:00:6C:BD:F3:00
inet addr:192.168.0.28 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::200:6cff:febd:f300/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20209 errors:0 dropped:0 overruns:0 frame:0
TX packets:10625 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1768186 (1.6 MiB) TX bytes:1390606 (1.3 MiB)
Interrupt:50 Base address:0x4000
eth0:1 Link encap:Ethernet HWaddr 00:00:6C:BD:F3:00
inet addr:192.168.0.37 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:50 Base address:0x4000
eth0:2 Link encap:Ethernet HWaddr 00:00:6C:BD:F3:00
inet addr:192.168.0.38 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:50 Base address:0x4000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:281 errors:0 dropped:0 overruns:0 frame:0
TX packets:281 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:25797 (25.1 KiB) TX bytes:25797 (25.1 KiB)
從另外一臺win2003上測試
結果都是通的;通過使用ifconfig與route添加的ip地址別名在服務器重啓後就會消失了,可以將這兩條語句添加到/etc/rc.local 文件中,讓系統開機時自動運行;
#vi /etc/rc.local
ifconfig eth0:1 192.168.0.37 broadcast 192.168.0.255 netmask 255.255.255.0 up
route add -host 192.168.0.37 dev eth0:1
ifconfig eth0:2 192.168.0.38 broadcast 192.168.0.255 netmask 255.255.255.0 up
route add -host 192.168.0.38 dev eth0:2
保存就可以了;
下面我們在nginx的配置文件中nginx.conf分別對192.168.0.28 ,192.168.0.37,192.168.0.38三個IP配置虛擬主機,
server {
listen 192.168.0.28:80;
server_name 192.168.0.28;
access_log logs/192.168.0.28.access.log main;
location / {
root /var/web/192.168.0.28;
index index.html index.htm;
}
}
server {
listen 192.168.0.37:80;
server_name 192.168.0.37;
access_log logs/192.168.0.37.access.log main;
location / {
root /var/web/192.168.0.37;
index index.html index.htm;
}
}
server {
listen 192.168.0.38:80;
server_name 192.168.0.38;
access_log logs/192.168.0.38.access.log main;
location / {
root /var/web/192.168.0.38;
index index.html index.htm;
}
}
[root@mgmserver ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
[root@mgmserver ~]# ps -ef |grep nginx
root 5284 4213 0 13:39 pts/2 00:00:00 vi nginx.conf
root 5356 4053 0 13:49 pts/1 00:00:00 grep nginx
[root@mgmserver ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@mgmserver ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
在/var/web下面分別建立三個目錄
192.168.0.28 192.168.0.37 192.168.0.38
分別在三個目錄中放一個index.html 文件 分別寫上自己的IP地址;
從win2003上面訪問;
上面就是nginx基於域名、ip訪問的配置,掌握住格式,就很好配置了。
結合apache配置:
1.httpd.conf
Listen 8801
Listen 8802
nginx.conf
include /usr/local/nginx/conf/vhost.conf
2.httpd-vhosts.conf
<VirtualHost *:8801>
ServerAdmin [email protected]
DocumentRoot "/var/www/a"
<Directory "/var/www/a">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ServerName www.a.com:8801
ServerAlias www.a.com:8801
ErrorLog "logs/a.com-error_log"
DirectoryIndex index.html index.html.var index.php
CustomLog "logs/a.com-access_log" common
</VirtualHost>
NameVirtualHost *:8802
<VirtualHost *:8802>
ServerAdmin [email protected]
DocumentRoot "/var/www/b"
<Directory "/var/www/b">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ServerName www.b.com:8802
ServerAlias www.b.com:8802
ErrorLog "logs/b.com-error_log"
DirectoryIndex index.html index.html.var index.php
CustomLog "logs/b.com-access_log" common
</VirtualHost>
3.nginx vhost.conf 配置
server {
listen 81;
server_name www.a.com;
access_log logs/a.access.log main;
location / {
root /var/www/a;
index index.html index.htm index.php;
}
location ~ .*\.(php?|cgi|pl|py)$ {
proxy_pass http://127.0.0.1:8801;
}
}
server {
listen 81;
server_name www.b.com;
access_log logs/b.access.log main;
location / {
root /var/www/b;
index index.html index.htm index.php;
}
location ~ .*\.(php?|cgi|pl|py)$ {
proxy_pass http://127.0.0.1:8802;
}
}