1、配置好DNS解析
[root@server ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.2 (Santiago)
[root@server ~]# uname -r
2.6.32-220.el6.i686
[root@server ~]# yum install bind* -y
[root@server ~]# vim /etc/named.conf
[root@server ~]# cat /etc/named.conf
 

 
  
  1. [plain] view plaincopyprint?  
  2. options {    
  3. listen-on port 53 { any; };    
  4. listen-on-v6 port 53 { any; };    
  5. directory "/var/named";    
  6. dump-file "/var/named/data/cache_dump.db";    
  7. statistics-file "/var/named/data/named_stats.txt";    
  8. memstatistics-file "/var/named/data/named_mem_stats.txt";    
  9. allow-query { any; };    
  10. recursion yes;    
  11.     
  12. dnssec-enable yes;    
  13. dnssec-validation yes;    
  14. dnssec-lookaside auto;    
  15.     
  16. /* Path to ISC DLV key */    
  17. bindkeys-file "/etc/named.iscdlv.key";    
  18. };    
  19.     
  20. logging {    
  21. channel default_debug {    
  22. file "data/named.run";    
  23. severity dynamic;    
  24. };    
  25. };    
  26.     
  27. zone "." IN {    
  28. type hint;    
  29. file "named.ca";    
  30. };    
  31.     
  32. zone "sxkeji.com.cn" IN {    
  33. type master;    
  34. file "sxkeji.com.cn.zone";    
  35. };  

[root@server ~]# cp /var/named/named.localhost /var/named/sxkeji.com.cn.zone
[root@server ~]# vim /var/named/sxkeji.com.cn.zone
[root@server ~]# cat /var/named/sxkeji.com.cn.zone
 

 
  
  1. [plain] view plaincopyprint?  
  2. $TTL 1D    
  3. @   IN SOA  sxkeji.com.cn rname.invalid. (    
  4.                     0   ; serial    
  5.                     1D  ; refresh    
  6.                     1H  ; retry    
  7.                     1W  ; expire    
  8.                     3H )    ; minimum    
  9.     NS  @       
  10. @   A   192.168.10.199      
  11. www A       192.168.10.199      
  12. mail    A       192.168.10.199  

[root@server ~]# service named restart

测试DNS解析是否成功
[root@server ~]# host www.sxkeji.com.cn
www.sxkeji.com.cn has address 192.168.10.199
[root@server ~]# host mail.sxkeji.com.cn
mail.sxkeji.com.cn has address 192.168.10.199
[root@server ~]#

2、配置Nginx虚拟主机
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server ~]# grep -vE "#|^$" /usr/local/nginx/conf/nginx.conf
 

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
server {
listen 80;
server_name mail.sxkeji.com.cn;
location / {
root /usr/local/nginx/html/mail.sxkeji.com;
index index.html index.htm;
}
}

include /usr/local/nginx/conf/vhosts/sxkeji.conf;
 

#include这里是模块化的,把虚拟主机独立成一个配置文件
 

#上面斜体server部分是在主配置文件中直接实现虚拟主机
}
[root@server ~]# cat /usr/local/nginx/conf/vhosts/sxkeji.conf #vhosts目录需要自己建立,主页目录也是需要自己建立的
 

 
  
  1. [plain] view plaincopyprint?  
  2. server {    
  3. listen 80;    
  4. server_name www.sxkeji.com.cn;    
  5. access_log logs/sxkeji.com.log;    
  6.     
  7. location / {    
  8. index index.html;    
  9. root /usr/local/nginx/html/sxkeji.com;    
  10. }    

[root@server ~]#

3、测试是否成功
[root@server ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` #重启nginx
[root@server ~]# vim /usr/local/nginx/html/sxkeji.com/index.html
[root@server ~]# vim /usr/local/nginx/html/mail.sxkeji.com/index.html
[root@server ~]# cat /usr/local/nginx/html/mail.sxkeji.com/index.html
mail.sxkeji.com.cn
[root@server ~]# cat /usr/local/nginx/html/sxkeji.com/index.html
www.sxkeji.com.cn
[root@server ~]# elinks --dump 127.0.0.1
hello!
[root@server ~]# elinks --dump mail.sxkeji.com.cn
mail.sxkeji.com.cn
[root@server ~]# elinks --dump www.sxkeji.com.cn
www.sxkeji.com.cn
[root@server ~]#

########如此便可以收工了,下次继续基于端口的虚拟主机配置########
 

Nginx相关

Nginx安装配置:点击打开链接

Nginx虚拟主机配置:点击打开链接