Nginux 虛擬主機
一:關於Nginx
-
一款高性能、輕量級Web服務軟件
-
穩定性高
-
系統資源消耗低
-
對HTTP併發連接的處理能力高
單臺物理服務器可支持30000 ~ 50000個併發請求
二:Nginx虛擬主機應用
-
Nginx支持的虛擬主機有三種
-
基於域名的虛擬主機
-
基於IP的虛擬主機
-
基於端口的虛擬主機
-
通過" server{} " 配置段實現
三:基於域名
1、安裝依賴包
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0 ‘關閉防火牆’
[root@localhost ~]# yum install gcc gcc-c++ make pcre-devel zlib-devel -y ‘安裝依賴包’
2、創建運行用戶
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
3、掛載共享文件,並解壓
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.10.29/share /abc
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
4、編譯安裝
[root@localhost opt]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module ‘開啓stub_status狀態統計模塊’
[root@localhost nginx-1.12.2]# make && make install
5、檢查nginx語法是否正確
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ‘優化路徑, 便於系統識別命令’
[root@localhost nginx-1.12.2]# nginx -t
[root@localhost init.d]# chkconfig --add nginx ‘添加到nginx服務’
[root@localhost init.d]# chkconfig --level 35 nginx on ‘開機自啓’
[root@localhost init.d]# service nginx start
[root@localhost init.d]# netstat -ntap | grep nginx ‘開啓nginx’
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 44549/nginx: master
6、 修改 nginx.conf 配置文件 ,指定訪問位置並添加 stub_status 配置代碼, 修改 server{ }區域
[root@localhost nginx]# cd conf
[root@localhost conf]# mv nginx.conf nginx.conf.bak
[root@localhost conf]# vim nginx.conf.bak
35 server { ‘Web服務的監聽配置’
36 listen 80; ‘監聽地址’
37 server_name www.kg.com; (1)‘修改監聽域名’
38
39 charset utf-8; (2)‘修改字符集’
40
41 #access_log logs/host.access.log main;
42
43 location / { ‘根目錄配置’
44 root html; ‘網站根目錄的位置’
45 index index.html index.htm; ‘默認首頁’
46 }
47
48 localtion /status { ‘訪問位置爲/status ’
49 stub_status on; ‘打開狀態統計功能’ (3)‘增加這一段’
50 access_log off; ‘關閉此位置的日誌記錄’
51 }
52 }
[root@localhost conf]# grep -v "#" nginx.conf.bak > nginx.conf
[root@localhost conf]# vim nginx.conf ‘與上面的配置文件內容是一樣的’
Nginx配置完成了,接下來配置DNS
DNS解析
1、安裝DNS軟件包
[root@localhost conf]# yum install bind -y
2、修改配置文件
[root@localhost conf]# vim /etc/named.conf
[root@localhost conf]# vim /etc/named.rfc1912.zones
[root@localhost conf]# cd /var/named/
[root@localhost named]# ls
data named.ca named.localhost slaves
dynamic named.empty named.loopback
[root@localhost named]# cp -p named.localhost kg.com.zone
[root@localhost named]# vim kg.com.zone
3、 準備每個網站目錄和測試首頁
[root@localhost named]# cd /var
[root@localhost var]# mkdir -p www/kg www/ac
[root@localhost var]# cd www
[root@localhost www]# ls
ac kg
[root@localhost www]# echo "this is ac web" > ac/index.html
[root@localhost www]# echo "this is kg web" > kg/index.html ‘創建兩個測試網頁’
4、把 server{ } 代碼段全部去掉,加入兩個新的server{} 段,對應2個域名
[root@localhost named]# cp -p kg.com.zone ac.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kg.com; ‘kg域名’
charset utf-8;
access_log logs/www.kg.com.access.log;
location / {
root /var/www/html/kg;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.ac.com; ‘ac域名’
charset utf-8;
access_log logs/www.ac.com.access.log;
location / {
root /var/www/html/ac;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost named]# nginx -t '檢查配置文件是否有語法錯誤'
最後在客戶機win10 上 輸入兩個域名 進行訪問。
四:基於端口
1、修改/usr/local/nginx/conf/nginx.conf文件
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.34.154:80;
server_name www.kg.com;
charset utf-8;
access_log logs/www.kg.com.access.log;
location / {
root /var/www/html/kg;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.34.154:8080;
server_name www.kg.com;
charset utf-8;
access_log logs/www.kg8080.com.access.log;
location / {
root /var/www/html/kg8080;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、建立站點首頁並重啓nginx
[root@localhost named]# cd /var/www
[root@localhost www]# mkdir kg8080
[root@localhost www]# ls
kg kg8080
[root@localhost www]# echo "this is kg8080 web" > kg8080/index.html
[root@localhost www]# service nginx restart ‘重啓服務’
3、在win10客戶端輸入不同端口登錄網址
五:基於IP地址
1、修改/usr/local/nginx/conf/nginx.conf文件
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.34.154:80;
server_name www.kg.com;
charset utf-8;
access_log logs/www.kg.com.access.log;
location / {
root /var/www/html/kg;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.34.144:80;
server_name www.ac.com;
charset utf-8;
access_log logs/www.ac.com.access.log;
location / {
root /var/www/html/ac;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、重啓nginx
service nginx stop
service nginx start