虛擬主機
什么是虛擬主機
實現一臺主機,對外提供多個web服務,每個虛擬主機之間是獨立的,互不影響。
怎么樣實現虛擬主機
- 綁定多IP
一塊物理網卡上綁定多個IP地址,這樣就能夠在使用單一網卡的同一個服務器上運行多個基于IP的虛擬主機。
- 實驗
1.1 環境準備,綁定多個IP
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:01
[root@localhost network-scripts]# vim ifcfg-ens33:01
OTPROTO=dhcp
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=0ae9d555-0fa5-427b-a186-ffe9c2e98aa4
DEVICE=ens33:01
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPADDR=192.168.176.4
DNS1=192.168.176.2
復制一份網絡配置文件為ifcfg-ens33:01,修改DEVICE=ens33:01和IPADDR,重啟系統即可
1.2 配置虛擬主機
修改 /usr/local/conf/nginx.conf
server {
listen 80;
server_name 192.168.176.104;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 192.168.176.4;
location / {
root /usr/local/nginx/html4;
index index.html index.htm;
}
}
多ip-104.png
多ip-192.168.176.4.png
- 多端口
- 需求
nginx對外提供兩個端口監聽服務,請求80端口則請求html80目錄下的html,請求8080則是html8080目錄下的html - 配置
server {
listen 80;
server_name 192.168.176.4;
location / {
root /usr/local/nginx/html80;
index index.html index.htm;
}
}
server {
listen 8080;
server_name 192.168.176.4;
location / {
root /usr/local/nginx/html8080;
index index.html index.htm;
}
}
-
測試
多端口-8080.png
多端口-80.png
- 通過域名區分
兩個域名指向同一臺nginx服務器,用戶訪問不同的域名顯示不同的網頁內容。
- 需求
訪問aaa.test.com、bbb.test.com對應192.168.176.4虛擬機,出現不同的網頁 - 配置
2.1 修改主機host
C:\Windows\System32\drivers\etc
192.168.176.4 aaa.test.com
192.168.176.4 bbb.test.com
2.2 配置nginx.conf
server {
listen 192.168.176.4:80;
server_name aaa.test.com;
location / {
root /usr/local/nginx/html80;
index index.html index.htm;
}
}
server {
listen 192.168.176.4:80;
server_name bbb.test.com;
location / {
root /usr/local/nginx/html8080;
index index.html index.htm;
}
}
- 測試
多域名-aaa.test.com.png
多域名-bbb.test.com.png
nginx反向代理
什么是正向代理,什么是反向代理
客戶機通過代理服務器去訪問Internet,幫助內網client去訪問外網中的目的主機(正向代理);反向代理,訪問代理服務器,由代理服務器去決定外網的哪個服務器來處理請求(反向代理)。
為什么要正向代理
方便企業、辦公管理,禁止內網的訪問限制。
為什么要反向代理
負載均衡,解決服務器的壓力。正向代理隱藏真實客戶端,反向代理隱藏真實服務端,
實驗
- 需求
兩個tomcat服務通過nginx代理
aaa.test.com tomcat1 192.168.176.4:8080
bbb.test.com tomcat2 192.168.176.4:8081
nginx 192.168.176.4:80 - 修改nginx.conf
upstream tomcat_server1 {
server 192.168.176.4:8080;
}
upstream tomcat_server2 {
server 192.168.176.4:8081;
}
server {
listen 80;
server_name aaa.test.com;
location / {
proxy_pass http://tomcat_server1;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbb.test.com;
location / {
proxy_pass http://tomcat_server2;
index index.html index.htm;
}
}
-
測試
nginx反向代理-bbb.test.com.png
nginx反向代理-aaa.test.com.png
負載均衡
負載均衡 建立在現有網絡結構之上,它提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增加吞吐量、加強網絡數據處理能力、提高網絡的靈活性和可用性。
負載均衡,英文名稱為Load Balance,其意思就是分攤到多個操作單元上進行執行,例如Web服務器、FTP服務器、企業關鍵應用服務器和其它關鍵任務服務器等,從而共同完成工作任務。