虚拟主机是指,在一台服务器中,通过nginx的代理,我们可以访问多个网站。区分不同的网站,可以通过端口、域名两种方式
这里写目录标题
- 一 端口不同区分不同的虚拟主机
- 二 通过域名区分不同的主机名
- 1.配置域名映射
- 2.显示登录效果
一 端口不同区分不同的虚拟主机
[root@localhost sbin]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
进去把注释全部删掉,再复制一份
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 {listen 81;server_name localhost;location / {root html81;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
进去修改这个网页的内容
# 复制一份域名文件
[root@localhost nginx]# cp -r html html81
[root@localhost nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 4月 24 22:44 conf
drwxr-xr-x. 2 root root 40 4月 24 21:04 html
drwxr-xr-x. 2 root root 40 4月 24 22:45 html81
drwxr-xr-x. 2 root root 19 4月 24 21:04 sbin
[root@localhost nginx]# cd html
[root@localhost html]# ll
总用量 8
-rw-r--r--. 1 root root 494 4月 24 21:04 50x.html
-rw-r--r--. 1 root root 612 4月 24 21:04 index.html
[root@localhost html]# vim index.html
[root@localhost html]# pwd
/usr/local/nginx/html
继续改里头的index
[root@localhost html]# cd ..
[root@localhost nginx]# cd html81
[root@localhost html81]# ll
总用量 8
-rw-r--r--. 1 root root 494 4月 24 22:45 50x.html
-rw-r--r--. 1 root root 612 4月 24 22:45 index.html
[root@localhost html81]# vim index.html
配置完后重启服务器
[root@localhost html81]# cd ../
[root@localhost nginx]# cd sbin
[root@localhost sbin]# ./nginx -s reload
[root@localhost sbin]#
进入浏览器访问
二 通过域名区分不同的主机名
1.配置域名映射
进入C:\Windows\System32\drivers\etc
追加到hosts文件末尾
192.168.80.121 www.jd123.com
192.168.80.121 wwww.taobao123.com
配置nginx.conf
然后修改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 www.jd123.com;location / {root jd;index index.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 80;server_name www.taobao.com;location / {root taobao;index taobao.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 82;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 {listen 81;server_name localhost;location / {root html81;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
2.显示登录效果
输入
www.jd123.com
成功解析出来
输入www.taobao123.com
意义是只有一台服务器,但是可以在一个服务器上运行多个网站