所謂的虛擬主機,在web服務裏面就是一個獨立的網站站點,這個站點對應獨立的域名(也有多是IP或者端口),具備獨立的程序和資源目錄,能夠獨立的對外提供服務。html
基於域名的虛擬主機:用不一樣的域名區分不一樣的虛擬主機(或者對外提供的資源)nginx
基於端口的虛擬主機:用相同的域名不一樣的端口區分不一樣的虛擬主機(或者對外提供的資源)web
基於IP的虛擬主機:用不一樣的IP區分不一樣的虛擬主機(或者對外提供的資源),在生產中不多見的vim
三種類型能夠獨立使用,也能混合使用,根據需求來肯定使用的形式windows
咱們要對nginx的主配置文件進行修改,在修改以前咱們要對nginx.conf 文件進行格式化(這樣提取出咱們想要的文件,便於更好的修改),格式化仍是去除空行和註釋的行 瀏覽器
[root@Nginx /]# cd /opt/nginx/conf/ # 切換到配置文件目錄 [root@Nginx conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf # 去掉空行和註釋 並生成新的文件nginx.conf
新文件的原始內容bash
[root@Nginx conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
咱們只須要修改server區塊的內容便可,修改後的內容以下:(紅色標記修改內容)服務器
[root@Nginx conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { # www.brian.com的server區塊 listen 80; server_name www.brian.com; # 域名 location / { root html/brian; # 站點根目錄 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
這裏咱們也能夠經過添加多個server區塊來配置基於多域名的虛擬主機,具體修改nginx.conf文件 以下:(紅色爲修改內容)app
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { # www.brian.com區塊開始 listen 80; server_name www.brian.com; # 域名 location / { root html/brian; # 站點根目錄 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # server區塊結束 server { # www.brianzjz.com 區塊開始 listen 80; server_name www.brianzjz.com; # 域名 location / { root html/brianzjz; # 站點根目錄 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #server區塊結束 } }
配置文件修改完成,接下來來建立上面指定的站點根目錄和首頁文件dom
建立站點根目錄,根據上面的nginx.conf中指定的:
[root@Nginx conf]# mkdir -p ../html/brian [root@Nginx conf]# mkdir -p ../html/brianzjz
建立index.html的首頁文件
[root@Nginx nginx]# echo "http://www.brian.com" > html/brian/index.html [root@Nginx nginx]# echo "http://www.brianzjz.com" > html/brianzjz/index.html [root@Nginx nginx]# cat html/brian/index.html http://www.brian.com [root@Nginx nginx]# cat html//brianzjz/index.html http://www.brianzjz.com
這樣咱們配置文件和首頁就已經配置完成了,下面就是重啓nginx 而且進行測試了
在重啓nginx服務以前,咱們要先對語法進行檢測:
[root@Nginx nginx]# sbin/nginx -t # 語法檢測,出現ok字樣,沒有問題 nginx: the configuration file /opt/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx-1.6.3//conf/nginx.conf test is successful
平滑重啓nginx服務(reload方法,不中斷業務)
[root@Nginx nginx]# sbin/nginx -s reload
檢查nginx的從新加載狀況
[root@Nginx nginx]# netstat -lntup | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23305/nginx: master [root@Nginx nginx]# ps -ef | grep nginx root 23305 1 0 06:48 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx nginx 24379 23305 0 10:44 ? 00:00:00 nginx: worker process root 24383 23911 0 10:44 pts/2 00:00:00 grep --color=auto nginx [root@Nginx nginx]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 23305 root 6u IPv4 47849 0t0 TCP *:http (LISTEN) nginx 24379 nginx 6u IPv4 47849 0t0 TCP *:http (LISTEN)
測試(由於沒有dns的域名解析服務,咱們只能經過添加hosts的文件來模擬解析):
Linux添加hosts文件: [root@Nginx nginx]# echo "192.168.1.108 www.brian.com www.brianzjz.com" >> /etc/hosts [root@Nginx nginx]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.108 www.brian.com www.brianzjz.com windows添加hosts文件 路徑:C:\Windows\System32\drivers\etc\hosts 打開上面路徑的文件添加,下面內容 保存 192.168.1.108 www.brian.com www.brianzjz.com
Linux上面訪問測試:
[root@Nginx conf]# curl www.brian.com www.brian.com # www.brian.com 主頁面內容 [root@Nginx conf]# curl www.brianzjz.com www.brianzjz.com # www.brianzjz.com 主頁面內容 [root@Nginx conf]#
windows瀏覽器測試:
修改nginx.conf配置文件以下:(紅色標記修改位置)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 10000; # 將80端口修改爲10000 server_name www.brian.com; location / { root html/brian; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 11000; # 將80端口修改爲11000 server_name www.brianzjz.com; location / { root html/brianzjz; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
在重啓nginx服務以前,咱們要先對語法進行檢測:
[root@Nginx nginx]# sbin/nginx -t # 語法檢測,出現ok字樣,沒有問題 nginx: the configuration file /opt/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx-1.6.3//conf/nginx.conf test is successful
平滑重啓nginx服務(reload方法,不中斷業務)
[root@Nginx nginx]# sbin/nginx -s reload
檢查nginx的從新加載狀況
[root@Nginx nginx]# netstat -lntup | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23305/nginx: master [root@Nginx nginx]# ps -ef | grep nginx root 23305 1 0 06:48 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx nginx 24379 23305 0 10:44 ? 00:00:00 nginx: worker process root 24383 23911 0 10:44 pts/2 00:00:00 grep --color=auto nginx [root@Nginx nginx]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 23305 root 6u IPv4 47849 0t0 TCP *:http (LISTEN) nginx 24379 nginx 6u IPv4 47849 0t0 TCP *:http (LISTEN)
windows瀏覽器測試:
既然要配置基於IP的虛擬主機,就須要讓每一個虛擬主機的IP不一樣,這時咱們就要加入輔助的虛擬IP了,命令以下:
[root@Nginx nginx]# ip addr add 192.168.1.111/24 dev ens33
增長完畢後,檢查配置生效結果:
[root@Nginx nginx]# ip add | grep 192.168.1 # 過濾ens33 網卡上的IP inet 192.168.1.108/24 brd 192.168.1.255 scope global dynamic ens33 inet 192.168.1.111/24 scope global secondary ens33 [root@Nginx nginx]# ping 192.168.1.108 # ping檢查 PING 192.168.1.108 (192.168.1.108) 56(84) bytes of data. 64 bytes from 192.168.1.108: icmp_seq=1 ttl=64 time=0.387 ms [root@Nginx nginx]# ping 192.168.1.111 # ping 檢查 PING 192.168.1.111 (192.168.1.111) 56(84) bytes of data. 64 bytes from 192.168.1.111: icmp_seq=1 ttl=64 time=0.050 ms
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.1.108:80; server_name www.brian.com; location / { root html/brian; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.1.111:80; server_name www.brianzjz.com; location / { root html/brianzjz; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
在重啓nginx服務以前,咱們要先對語法進行檢測:
[root@Nginx nginx]# sbin/nginx -t # 語法檢測,出現ok字樣,沒有問題 nginx: the configuration file /opt/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx-1.6.3//conf/nginx.conf test is successful
平滑重啓nginx服務(reload方法,不中斷業務)
[root@Nginx nginx]# sbin/nginx -s reload
檢查nginx的從新加載狀況
[root@Nginx nginx]# netstat -lntup | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23305/nginx: master [root@Nginx nginx]# ps -ef | grep nginx root 23305 1 0 06:48 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx nginx 24379 23305 0 10:44 ? 00:00:00 nginx: worker process root 24383 23911 0 10:44 pts/2 00:00:00 grep --color=auto nginx [root@Nginx nginx]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 23305 root 6u IPv4 47849 0t0 TCP *:http (LISTEN) nginx 24379 nginx 6u IPv4 47849 0t0 TCP *:http (LISTEN)
windows瀏覽器測試: