在Web服务里面虚拟主机就是一个独立的网站站点,这个站点对应独立的域名(也多是IP或端口),具备独立的程序及资源目录,能够独立地对外提供服务供用户访问。html
不一样虚拟主机对应一个独立的域名nginx
不一样虚拟主机端口号不一样。也能够是同一个域名或同一个IP,但端口号不一样shell
不一样虚拟主机IP地址也不一样 windows
切换到nginx配置目录下面浏览器
cd /application/nginx/conf
过滤掉包含#号和空行,生成新的配置文件app
egrep -v "#|^$" nginx.conf.default > nginx.conf
更改配置文件nginx.confcurl
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; # 端口,默认就是80 server_name www.ljmict.com; # 域名 location / { root html/www; index index.html index.htm; } } }
建立域名www.ljmict.com
对应的站点目录及文件ide
mkdir /application/nginx/html/www echo "http://www.ljmict.com" > ../html/www/index.html
查看www.ljmict.com
对应的首页文件index.html
测试
cat ../html/www/index.html # 查看结果 http://www.ljmict.com
检查修改过的nginx文件配置是否有语法问题网站
/application/nginx/sbin/nginx -t
从新启动nginx服务
/application/nginx/sbin/nginx -s reload
客户端测试
在客户端测试以前,须要更改客户端的hosts
文件,MAC
和Linux
的hosts
文件都在/etc/hosts
文件,若是是windows
系统hosts
文件在C:\Windows\System32\drivers\etc
目录下。
echo "172.16.1.10 www.ljmict.com" >> /etc/hosts curl www.ljmict.com # 结果 http://www.ljmict.com
在浏览器中访问
更改nginx配置文件
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 www.ljmict.com; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name bbs.ljmict.com; location / { root html/bbs; index index.html index.htm; } } server { listen 80; server_name blog.ljmict.com; location / { root html/blog; index index.html index.htm; } } }
建立对应的站点目录及文件
mkdir /application/nginx/html/bbs echo "http://bbs.ljmict.com" > /application/nginx/html/bbs/index.html mkdir /application/nginx/html/blog echo "http://blog.ljmict.com" > /application/nginx/html/blog/index.html
从新启动nginx服务
/application/nginx/sbin/nginx -s reload
添加hosts
记录
echo "172.16.1.10 bbs.ljmict.com" >> /etc/hosts echo "172.16.1.10 blog.ljmict.com" >> /etc/hosts
客户端访问
更改nginx配置文件
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 www.ljmict.com; location / { root html/www; index index.html index.htm; } } server { listen 81; # 端口更改为81 server_name bbs.ljmict.com; location / { root html/bbs; index index.html index.htm; } } server { listen 82; # 端口更改为82 server_name blog.ljmict.com; location / { root html/blog; index index.html index.htm; } } }
重启nginx服务
/application/nginx/sbin/nginx -s reload
客户端访问
若是要配置基于IP的虚拟主机,就须要让每一个虚拟主机有不一样的IP地址,在这里我临时在ens37网卡上增长2个不一样的IP
ip addr add 172.16.1.11/24 dev ens37 ip addr add 172.16.1.12/24 dev ens37 # 注意:根据本身的网卡名称来配置IP
更改nginx配置文件
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 172.16.1.10; location / { root html/www; index index.html index.htm; } } server { listen 81; server_name 172.16.1.11; # 临时配置的IP location / { root html/bbs; index index.html index.htm; } } server { listen 82; server_name 172.16.1.12; # 临时配置的IP location / { root html/blog; index index.html index.htm; } } }
从新启动nginx服务
/application/nginx/sbin/nginx -s reload
客户端访问