:Nginx 基于ip的虚拟主机配置实例
http {


      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                    '$status $body_bytes_sent "$http_referer" '
                                    '"$http_user_agent" "$http_x_forwarded_for"';
      server {
              listen           80 default;
              server_name      _;
              access_log       logs/default.access.log main;
 
              server_name_in_redirect off;
              location / {
                            index     index.html index.htm;
                            root      /var/www/default/htdocs;
                          }
                  }
       
       server {
              listen           80;
              server_name      10.5.10.34;
              access_log       logs/34.access.log main;
 
              location / {
                            index     index.html index.htm;
                            root      /www/nginx/34;
                               }
                   }

        server {
              listen           80;
              server_name      10.5.10.35;
              access_log       logs/35.access.log main;
 
              location / {
                            index     index.html index.htm;
                            root      /www/nginx/35;
                               }
                    }
        }
PS:
①基于ip的虚拟主机server的红色部分一定要有否则访问10.5.10.34和35是同一个目录(参看http://wiki.nginx.org/ServerBlockExample).
②由于红色部分中access.log 类型为main 故nginx.conf中默认的日志格式一定要打开(即去掉前面的“#”),否则会出现如下错误信息"2013/03/02 01:22:10 [emerg] 31055#0: unknown log format "main" in /usr/local/nginx/conf/nginx.conf:80"
 
②基于域名的虚拟主机配置实例
http{
       server{
               listen        80;
               server_name    www.a.com;
               access_log     a.com.access.log     combined;
             
               location / {
                            index index.html index.htm;
                            root   /www/nginx/34;
                               }
                  }

       server{
               listen        80;
               server_name    www.b.net;
               access_log     b.net.access.log     combined;
             
               location / {
                            index index.html index.htm;
                            root   /www/nginx/35;
                                }
                  }

       server{
               listen        80;
               server_name    www.c.org;
               access_log     c.org.access.log     combined;
             
               location / {
                            index index.html index.htm;
                            root   /www/nginx/36;
                                }
                    }    

}
 
以下是我通过chrome和elinks访问的日志信息。
Nginx  虚拟主机的配置_nginx
 
③基于端口的虚拟机主机实例

http{
       server{
               listen             80;
               server_name        10.5.10.34;
               access_log         port.80.access.log combined;
           
              location / {
                            index   index.html index.htm;
                            root    /www/nginx/34;
                          }
              }

       server{
               listen             8080;
               server_name        10.5.10.34;
               access_log         port.8080.access.log combined;
           
              location / {
                            index   index.html index.htm;
                            root    /www/nginx/35;
                          }
              }
     
       server{
               listen             8081;
               server_name        10.5.10.34;
               access_log         port.8081.access.log combined;
           
              location / {
                            index   index.html index.htm;
                            root    /www/nginx/36;
                          }
              }

}
#elinks 10.5.10.34
#elinks 10.5.10.34:8080
#elinks 10.5.10.34:8081
以下是我用elinks访问基于port虚拟主机的access日志

Nginx  虚拟主机的配置_nginx_02

 
ps:基于域名和基于端口的虚拟主机不需要像基于ip的添加那段红色代码便可以正常访问。 
快乐学习,快乐分享!