Nginx 虚拟主机配置的三种方式(三)(基于域名)

168 篇文章 2 订阅
120 篇文章 0 订阅

https://blog.csdn.net/liupeifeng3514/article/details/79007051

 

3、Nginx基于域名的虚拟主机配置

使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问。

3.1 假设服务器有个IP地址为192.168.2.155

[root@localhost ~]# ifconfig ens33:5 192.168.2.155/24 up
[root@localhost ~]# ifconfig
ens33:5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.155  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.2 192.168.2.155对应的域名如下,配置主机的host文件便于测试

[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts|grep 192.168.2.155
192.168.2.155 www.oa.com
192.168.2.155 www.bbs.com
192.168.2.155 www.test.com
  • 1
  • 2
  • 3
  • 4
  • 5

3.3 建立虚拟主机存放网页的根目录,并创建首页文件index.html

[root@localhost ~]# cd /data/www/
[root@localhost www]# mkdir www.oa.com
[root@localhost www]# mkdir www.bbs.com
[root@localhost www]# mkdir www.test.com
[root@localhost www]# echo www.oa.com > www.oa.com/index.html
[root@localhost www]# echo www.bbs.com > www.bbs.com/index.html
[root@localhost www]# echo www.test.com > www.test.com/index.html
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.4 修改nginx.conf,将虚拟主机配置文件包含进主文件

[root@localhost /]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@localhost conf]# vim nginx.conf
  • 1
  • 2
  • 3
  • 4
  • 5

在nginx.conf文件末尾加入以下配置

# 在http段中找到以下内容并删除每行前面的“#”
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

# 配置文件结尾的最后一个“}”之前加入以下语句,如下所示
include vhost/*.conf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.5 编辑每个域名的配置文件(每个虚拟主机的配置文件)

[root@localhost conf]# cd vhost/
[root@localhost vhost]# cat www.oa.com.conf
    server {
        listen       192.168.2.155:80;
        server_name  www.oa.com;

        access_log   /data/logs/www.oa.com.log main;
        error_log    /data/logs/www.oa.com.error.log;

        location / {
            root   /data/www/www.oa.com;
            index  index.html index.htm;
        }
    }

[root@localhost vhost]# cat www.bbs.com.conf
    server {
        listen       192.168.2.155:80;
        server_name  www.bbs.com;

        access_log   /data/logs/www.bbs.com.log main;
        error_log    /data/logs/www.bbs.com.error.log;

        location / {
            root   /data/www/www.bbs.com;
            index  index.html index.htm;
        }
    }

[root@localhost vhost]# cat www.test.com.conf
    server {
        listen       192.168.2.155:80;
        server_name  www.test.com;

        access_log   /data/logs/www.test.com.log main;
        error_log    /data/logs/www.test.com.error.log;

        location / {
            root   /data/www/www.test.com;
            index  index.html index.htm;
        }
    }

[root@localhost vhost]# cat /data/www/www.oa.com/index.html
www.oa.com
[root@localhost vhost]# cat /data/www/www.bbs.com/index.html
www.bbs.com
[root@localhost vhost]# cat /data/www/www.test.com/index.html
www.test.com
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

3.6 创建日志文件,否则无法启动nginx

[root@localhost /]# mkdir -p /data/logs
[root@localhost /]# touch /data/logs/www.oa.com.log
[root@localhost /]# touch /data/logs/www.oa.com.error.log
[root@localhost /]# touch /data/logs/www.bbs.com.log
[root@localhost /]# touch /data/logs/www.bbs.com.error.log
[root@localhost /]# touch /data/logs/www.test.com.log
[root@localhost /]# touch /data/logs/www.test.com.error.log
[root@localhost /]# ls /data/logs/
www.oa.com.error.log  www.bbs.com.error.log  www.test.com.error.log
www.oa.com.log        www.bbs.com.log        www.test.com.log
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.7 先测试配置文件然后再启动nginx

[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 启动nginx
[root@localhost sbin]# ./nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.8 测试文件

[root@localhost vhost]# curl http://www.oa.com
www.oa.com
[root@localhost vhost]# curl http://www.bbs.com
www.bbs.com
[root@localhost vhost]# curl http://www.test.com
www.test.com
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

附:配置过程中的问题

1、最后测试时发生的问题

[root@localhost ~]# curl http://www.oa.com
curl: (7) Failed connect to www.oa.com:80; 拒绝连接
  • 1
  • 2

决解方法:
查看Nginx是否在监听相应的端口。

[root@localhost ~]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.155:80        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp6       0      0 :::111                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::23                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1、配置虚拟主机文件时要加上监听的IP地址,每个虚拟主机配置文件都一样。

 listen       192.168.2.155:80;
  • 1

2、配置完成后要重启服务器

--------------------- 作者:B8613A 来源:CSDN 原文:https://blog.csdn.net/liupeifeng3514/article/details/79007051?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值