Nginx的应用之虚拟主机

开始前请确保selinux关闭,否则当配置完虚拟主机后,尽管权限或者网站目录都正确,访问的结果也是403

nginx的虚拟主机有三种方式:

一、基于域名的虚拟主机

(1)创建对应的web站点目录以及程序代码

[root@web01 ~]# mkdir /data/www/{game,video}
[root@web01 ~]# echo "game" > /data/www/game/index.html
[root@web01 ~]# echo "video" > /data/www/video/index.html

(2)配置不同域名的虚拟主机

[root@web01 ~]# cat /etc/nginx/conf.d/game.conf
server {
    listen       80;
    server_name  game.com;
    root /data/www/game;
    index index.html;
    ...
}
[root@web01 ~]# cat /etc/nginx/conf.d/video.conf
server {
    ...
    listen       80;
    server_name  video.com;
    root /data/www/video;
    index index.html;
}

配置完虚拟主机后最好重启或重载nginx服务

(3)修改hosts文件进行访问测试

vim /etc/hosts
127.0.0.1 game.com video.com

curl game.com
game

curl video.com
video

 

二、基于IP的虚拟主机

  1、基于多网卡多IP的方式

server {
    ...
    listen 10.0.0.10:80;
    ...
}

server {
    ...
    listen 10.0.0.11:80;
    ...
}

  2、基于单网卡多IP的方式

#添加一个IP
[root@web01 ~]# ip addr add 10.0.0.11/24 dev eth0

# 虚拟机配置方案
[root@web01 ~]# cat /etc/nginx/conf.d/addr1.conf
server {
    ...
    listen 10.0.0.10:80;
    ...
}

[root@web01 ~]# cat /etc/nginx/conf.d/addr2.conf
server {
    ...
    listen 10.0.0.11:80;
    ...
}

 

三、基于端口的虚拟主机

#仅修改listen监听端口即可, 但不能和系统端口出现冲突

[root@web01 ~]# cat /etc/nginx/conf.d/port1.conf
server {
    ...
    listen 80;
    ...
}

[root@web01 ~]# cat /etc/nginx/conf.d/port2.conf
server {
    ...
    listen 81;
    ...
}

[root@web01 ~]# cat /etc/nginx/conf.d/port3.conf
server {
    ...
    listen 82;
    ...
}

 

posted @ 2019-08-25 22:48  zh_Revival  阅读(197)  评论(0编辑  收藏  举报