Nginx基於IP,埠,域名配置虛擬主機
阿新 • • 發佈:2018-12-20
Nginx(發音同 engine x)是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,並在一個BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好。Nginx同Apache httpd一樣,Nginx也提供基於IP,基於埠以及域名方式的形式來配置虛擬主機。
一、什麼是虛擬主機
虛擬主機是使用特殊的軟硬體技術,把一臺真實的物理伺服器主機分割成多個邏輯儲存單元。每個邏輯單元都沒有物理實體,但是每一個邏輯單元都能像真實的物理主機一樣在網路上工作,具有單獨的IP地址(或共享的IP地址)、獨立的域名以及完整的Internet伺服器(支援WWW、FTP、E-mail等)功能。
虛擬主機的關鍵技術在於,即使在同一臺硬體、同一個作業系統上,執行著為多個使用者開啟的不同的伺服器程式,也互不干擾。而各個使用者擁有自己的一部分系統資源(IP地址、文件儲存空間、記憶體、CPU等)。各個虛擬主機之間完全獨立,在外界看來,每一臺虛擬主機和一臺單獨的主機的表現完全相同。所以這種被虛擬化的邏輯主機被形象地稱為“虛擬主機”。
二、基於埠的虛擬主機
1、準備環境#當前環境# more /etc/issueRed Hat Enterprise Linux Server release 6.3 (Santiago)Kernel \r on an \m# uname -rm2.6.32-279.el6.x86_64 x86_64# nginx -vnginx version: nginx/1.8.0# 建立3個目錄用於存放不同形式虛擬主機index.html檔案# mkdir -p /website/baseport# mkdir -p /website/baseip# mkdir -p /website/basedomain# vi /website/baseport/index.html <!DOCTYPE html> <html><head><title>Base port sample</title></head><body><h1>This is an based port website sample(prot:8080).</h1></body></html>2、配置nginx.conf#第一個虛擬主機server { listen 80; server_name localhost; location / { root html; index index.html index.htm; }#第二個虛擬主機 server { listen 8080; server_name localhost; location / { root /website/port; index index.html index.htm; } }3、驗證 # nginx -t #語法檢查# service nginx reload #服務過載# curl http://192.168.1.120:8080 #驗證基於埠訪問<!DOCTYPE html> <html> <head><title>Base port sample</title></head><body><h1>This is an based port website sample(prot:8080).</h1></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
三、基於IP的虛擬主機
1、先新增IP# ifconfig|grep "inet addr" inet addr:192.168.1.120 Bcast:192.168.1.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0# ifconfig eth0:0 192.168.1.220 netmask 255.255.255.0 up #新增IP到eth0:0# ifconfig|grep "inet addr" inet addr:192.168.1.120 Bcast:192.168.1.255 Mask:255.255.255.0 inet addr:192.168.1.220 Bcast:192.168.1.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.02、配置nginx.conf#第一個虛擬主機server { listen 80; server_name localhost; location / { root html; index index.html index.htm;#第二個虛擬主機 server { listen 192.168.1.220:80; server_name localhost; location / { root /website/baseip; index index.html index.htm; } }3、驗證 # nginx -t #語法檢查 Author:Leshami # service nginx reload #服務過載 Blog :http://blog.csdn.net/leshami# curl http://192.168.1.220 #驗證基於IP訪問<!DOCTYPE html><html><head><title>Base ip sample</title></head><body><h1>This is an based ip website sample.</h1></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
四、基於域名的虛擬主機
1、修改/etc/hosts檔案# echo "192.168.1.120 bbs.ycdata.net bbs192.168.1.120 mail.ycdata.net mail> ">>/etc/hosts2、配置nginx.conf#第一個虛擬主機server { listen 80; server_name mail.ycdata.net; location / { root html; index index.html index.htm; }#第二個虛擬主機 server { listen 80; server_name bbs.ycdata.net; location / { root /website/baseport; index index.html index.htm; } }3、驗證# curl http://mail.ycdata.net<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html># curl http://bbs.ycdata.net<!DOCTYPE html><html><head><title>Base port sample</title></head><body><h1>This is an based port website sample(prot:8080).</h1></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66