centos6.5 -nginx虛擬主機配置

***基於域名虛擬主機

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章