Nginx(二):虛擬主機配置
阿新 • • 發佈:2018-11-12
什麼是虛擬主機?
虛擬主機使用的是特殊的軟硬體技術,它把一臺執行在因特網上的伺服器主機分成一臺臺“虛擬”的主機,每臺虛擬主機都可以是一個獨立的網站,可以具有獨立的域名,具有完整的Intemet伺服器功能(WWW、FTP、Email等),同一臺主機上的虛擬主機之間是完全獨立的。從網站訪問者來看,每一臺虛擬主機和一臺獨立的主機完全一樣。
利用虛擬主機,不用為每個要執行的網站提供一臺單獨的Nginx伺服器或單獨執行一組Nginx程序。虛擬主機提供了在同一臺伺服器、同一組Nginx程序上執行多個網站的功能。
Nginx和Apache一樣支援配置基於IP的虛擬主機,基於域名的虛擬主機,基於埠的虛擬主機這三種。
配置基於IP的虛擬主機
在Nginx配置檔案(nginx.conf)中,分別對10.0.0.133、10.0.0.189、10.0.0.190三個IP配置三個純靜態HTML支援的虛擬主機。
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #第一個虛擬主機 server { listen 10.0.0.133:80; #監聽的IP和埠 server_name10.0.0.133; #主機名稱 access_log logs/host1.access.log main; #訪問日誌檔案存放路徑 location / { root /usr/local/nginx/html/host1; #HTML網頁檔案存放的目錄 index index.html index.htm; #預設首頁檔案,順序從左到右,如果找不到index.html檔案,則查詢index.htm檔案作為首頁檔案 } } #第二個虛擬主機 server { listen 10.0.0.189:80; server_name 10.0.0.189; access_log logs/host2.access.log main; location / { root /usr/local/nginx/html/host2; index index.html index.htm; } } #第三個虛擬主機 server { listen 10.0.0.190:80; server_name 10.0.0.190; access_log logs/host3.access.log main; location / { root /usr/local/nginx/html/host3; index index.html index.htm; } }
配置基於域名的虛擬主機
其實基於域名和基於ip的虛擬主機配置是差不多的,在配置基於ip的虛擬主機上我們只需要修改幾個地方就能變成基於域名的虛擬主機,一個是要修改域名,一個是host檔案
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 192.168.3.121:80; server_name www.bp1.com; #修改這裡 location / { root html; index index.html index.htm index.php; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server{ listen 192.168.3.123:80; server_name www.bp3.com; #修改這裡 access_log logs/bp2.access.log combined; location / { index index.html index.php; root html/bp2; } location ~ \.php$ { root html/bp2; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server{ listen 192.168.3.125:80; server_name www.bp2.com; #修改這裡 location /{ root html/bp3; index index.html index.php; } location ~ \.php$ { root html/bp3; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } #include /opt/nginx/conf/vhosts/www.domain2.com.conf; #這一句是包含另一個nginx虛擬機器主機的配置檔案,其內容類似於上面最後一個server裡面的內容,去掉註釋後其功能相當於新增一臺虛擬主機 }
配置基於埠虛擬主機
如一臺伺服器只有一個IP或需要通過不同的埠訪問不同的虛擬主機,可以使用基於埠的虛擬主機配置。
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #第一個虛擬主機 server { listen 8111; #監聽的IP和埠 server_name localhost; #主機名稱 access_log logs/host1.access.log main; #訪問日誌檔案存放路徑 location / { root /usr/local/nginx/html/host1; #HTML網頁檔案存放的目錄 index index.html index.htm; #預設首頁檔案,順序從左到右,如果找不到index.html檔案,則查詢index.htm檔案作為首頁檔案 } } #第二個虛擬主機 server { listen 8112; server_name localhost; access_log logs/host2.access.log main; location / { root /usr/local/nginx/html/host2; index index.html index.htm; } } #第三個虛擬主機 server { listen 8113; server_name localhost; access_log logs/host3.access.log main; location / { root /usr/local/nginx/html/host3; index index.html index.htm; } }