虚拟主机:可以在一台服务器部署多个不同页面

实验目的

使用一台nginx服务器,可以分别访问到多个页面,

使用虚拟主机的三种方法:相同ip不同端口,不同ip相同端口,不同域名相同ip

实验步骤

源码安装nginx

nginx官网下载

建议在安装之前检测80端口是否被占用netstat -anpt | grep 80

[root@localhost ~]# tar zxf nginx-1.14.2.tar.gz -C /usr/src
[root@localhost ~]# yum -y install pcre* openssl*
[root@localhost ~]# cd /usr/src/nginx-1.14.2/
[root@localhost nginx-1.14.2]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-pcre
[root@localhost nginx-1.14.2]# make && make install

创建程序用户

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

优化命令路径

[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

安装完成,现在可以通过命令nginx启动服务,并通过ip访问默认页面

启动脚本

为了方便管理服务,写一个服务启动脚本

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig:- 99 20
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
        start)
        $PROG
;;
        stop)
        kill -s QUIT $(cat $PIDF)
;;
        restart)
        $0 stop
        $0 start
;;
        reload)
        kill -s HUP $(cat $PIDF)
;;
        *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit 0

设置执行权限

chmod +x /etc/init.d/nginx

添加为系统服务

chkconfig --add nginx

配置nginx虚拟主机项

基于ip

不同ip相同端口号

由于是实验环境,使用临时的虚接口ip

[root@localhost ~]# ifconfig ens33 192.168.1.6/24
[root@localhost ~]# ifconfig ens33:0 192.168.1.7/24

调整nginx配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在http模块中添加server模块,因为默认有一个server_name的值为localhost的server模块
 server {
        listen          80;
        server_name      192.168.1.7;        //虚拟ip
        location / {
                root    /www/html;        //此目录需要单独创建
                index   index.html index.htm;
                }
 }

使用nginx -t检测配置文件语法问题

创建虚拟ip的网页根目录

[root@localhost ~]# mkdir -p /www/html

编辑虚拟ip的默认网页文件

[root@localhost ~]# vim /www/html/index.html
添加
welcome to mupei's nginx

重启服务进行验证

[root@localhost ~]# systemctl restart nginx
验证,分别使用curl访问两个ip
curl 192.168.1.6
curl 192.168.1.7
访问到不同页面即实验成功

基于端口号

相同ip不同端口号

修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
修改两个server为相同ip不同端口号,并且设置不同的根目录
     server {
        listen          80;
        server_name     192.168.1.7;
        location / {
                root    /www/html;        //使用基于ip的网页根目录即可,也可重新指定
                index   index.html index.htm;
        }
        }
     server {
        listen          8080;
        server_name     192.168.1.7;
        location / {
                root    /www/html1;    //需要单独创建
                index   index.html index.htm;
        }
}

创建第二个网页根目录

[root@localhost ~]# mkdir /www/html1

编辑第二个网页根目录的主页文件

[root@localhost ~]# vim /www/html1/index.html
添加
hai!!!

重启服务验证

[root@localhost ~]# systemctl restart nginx
验证:
[root@localhost ~]# curl 192.168.1.7
welcome to mupei's nginx
[root@localhost ~]# curl 192.168.1.7:8080
hai!!!

基于域名

相同ip端口,不同域名

修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
视情况修改或者添加server模块,修改server_name为域名,网页根目录和基于端口号的一致
     server {
        listen          80;
        server_name     www.mupei.com;
        location / {
                root    /www/html;
                index   index.html index.htm;
        }
        }
     server {
        listen          80;
        server_name     www.feiyi.com;
        location / {
                root    /www/html1;
                index   index.html index.htm;
        }

为了试验效果,不作DNS,直接使用/etc/hosts/文件

[root@localhost ~]# vim /etc/hosts
添加
192.168.1.6 www.mupei.com
192.168.1.6 www.feiyi.com

重启服务验证

[root@localhost ~]# systemctl restart nginx
验证
[root@localhost ~]# curl www.mupei.com
welcome to mupei's nginx
[root@localhost ~]# curl www.feiyi.com
hai!!!

评论




正在载入...
PoweredHexo
HostedAliyun
DNSAliyun
ThemeVolantis
UV
PV
BY-NC-SA 4.0