所谓的虚拟主机,在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浏览器测试: