***基于域名虚拟主机

1 基于域名的方式,要先有dns服务器,这里为了方便,可以在/etc/hosts文件里面配置

hosts文件里增加一行 192.168.1.150 www.abc.com abc.com www.bbs.com bbs.com

2 [root@aaaa conf]# cp nginx.conf nginx.conf.bak 备份配置文件

3 简化下配置文件     egrep -v "#|^$" nginx.conf >> nginx.conf.1

                              mv nginx.conf.1 nginx.conf


4 root@aaaa conf]# vim nginx.conf

修改主配置文件

user nginx nginx;
worker_processes 1;                    ///worker进程轻易不要调,否则会出问题,worker进程数不要大于cpu核数
events {
   worker_connections 1024;   ///每个进程最大的连接数,可以适当调高一点,最大值是65535
}
http {
   include      mime.types;
   default_type application/octet-stream;
   sendfile       on;
   keepalive_timeout 65;
   server {
       listen      80;          ///监听主机中所有的80端口
       server_name www.abc.com abc.com;     ///客户端要访问网站用到的域名
       location / {
           root  /data1/www/web1;        网站位置
           index index.html index.htm;
       }
       error_page  500 502 503 504 /50x.html;
       location = /50x.html {
           root  html;
       }
   }

 

server {
       listen      80;
       server_name www.bbs.com bbs.com;
       location / {
           root  /data1/www/web2;
           index index.html index.htm;
       }
       error_page  500 502 503 504 /50x.html;
       location = /50x.html {
           root  html;
       }
   }


}
                                                             40,1         Bot

 

5 创建2个站点 ,并输入内容在里面

mkdir -p /data1/www/web1

mkdir -p /data1/www/web2

cd /data1/www/web1    echo"web1111111111" >>index.html

 

cd /data1/www/web2   echo"web2222222" >>index.html

 

6 重启服务 /application/nginx/sbin/nginx -s reload  

7 .查看进程和端口

ps -ef | grep nginx

 

[root@aaaa conf]# netstat -lnt | grep 80
tcp       0     0 0.0.0.0:80                 0.0.0.0:*                  LISTEN     

[root@aaaa conf]# lsof -i :80
COMMAND  PID USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
nginx  56640 root   6u IPv4 118426     0t0 TCP *:http (LISTEN)
nginx  56710 nginx   6u IPv4 118426     0t0 TCP *:http (LISTEN

 

8 验证服务

wKioL1QboNHCUIPCAADYBw0nuvc777.jpg

wKiom1QboLbRqcqZAADQi8NIi8Y920.jpg

 

*********************基于IP

在这个配置文件里我们可以增加上日志,因为在做基于域名的虚拟主机的时候把日志过滤出去了。

1 创建日志目录 mkdir -p /app/logs/

2 编译配置主配置文件nginx.conf

[root@aaaa conf]# vim nginx.conf

 

 

user nginx nginx;
worker_processes 1;
error_log /app/logs/nginx_error.log crit;

pid       logs/nginx.pid;

events {
 use epoll;
   worker_connections 1024;
}
http {
   include      mime.types;
   default_type application/octet-stream;

   log_format commonlog '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';


   sendfile       on;
   keepalive_timeout 65;
   server {
       listen      80;
       server_name 192.168.1.150;
       location / {
           root  /data1/www/web1;
           index index.html index.htm;
       access_log /app/logs/abc_access.log commonlog;
       }


       }

 server {
       listen      80;
       server_name 192.168.1.152;
       location / {
           root  /data1/www/web2;
           index index.html index.htm;

          access_log /app/logs/bbs_access.log commonlog;

       }

       }


}

3 在eth0上在增加一个ip:

 

[root@aaaa network-scripts]# cd /etc/sysconfig/network-scripts/
[root@aaaa network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1
[root@aaaa network-scripts]# vim ifcfg-eth0
ifcfg-eth0   ifcfg-eth0:1 
[root@aaaa network-scripts]# vim ifcfg-eth0:1

DEVICE=eth0
HWADDR=00:0C:29:27:9A:20
TYPE=Ethernet
UUID=6732ab0f-0b32-46ed-aeb9-f7c096302bea
ONBOOT=yes
IPADDR=192.168.1.152
PREFIX=24
NM_CONTROLLED=yes
BOOTPROTO=none

4关闭iptables 重启网卡

5 检验下nginx配置

                 [root@aaaa network-scripts]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.2/conf/nginx.conf test is successful
6 重新启动nginx,并且验证   /application/nginx/sbin/nginx -s reload

 

wKioL1QboX6iEPy3AADZmSWkRFs062.jpg

wKiom1QboWPAmFrYAADRbZY3QBQ249.jpg

 

 

 

********基于端口的更简单了。

vim nginx.conf


                                                                                                      
~                         server {
       listen      8090;
       server_name 192.168.1.150;
       location / {
           root  /data1/www/web1;
           index index.html index.htm;
       access_log /app/logs/abc_access.log commonlog;
       }


       }

 

wKiom1QbobPTzFlCAADjz7L2Mq0821.jpg

wKioL1Qboc7TavOXAADrbobQfoA541.jpg