3-Ngnix配置基于端口的多虚拟主机

Nginx配置文件在/usr/local/nginx/conf下,文件名为nginx.conf

1.Ngnix配置基于端口的多虚拟主机

-->进入/usr/local/nginx/conf目录

命令:

cd /usr/local/nginx/conf

pwd

[root@cent79-2 conf]# cd /usr/local/nginx/conf/
[root@cent79-2 conf]# pwd
/usr/local/nginx/conf
[root@cent79-2 conf]# 

-->编辑nginx.conf

命令:

vi nginx.conf

-->找到http块,在其下添加基于多端口的(前提:端口不能被占用)server{}块(与其它server并行),样例内容如下,并保存

 server {
        listen 8080;
        location / {
          root html/ztj;
          index index.html index.htm;
        }
    }

    server {
        listen 8081;
        location / {
          root html/ztj1;
          index index.html index.htm;
        }
    }

-->在/usr/local/nginx/html目录下,创建ztj和ztj1目录,并创建测试html文件

命令:

mkdir ztj
mkdir ztj1
echo "I am 8080" > ztj/index.html
echo "I am 8081" > ztj1/index.html

[root@cent79-2 html]# mkdir ztj
[root@cent79-2 html]# mkdir ztj1
[root@cent79-2 html]# echo "I am 8080" > ztj/index.html
[root@cent79-2 html]# echo "I am 8081" > ztj1/index.html
[root@cent79-2 html]# ls
50x.html  index.html  ztj  ztj1
[root@cent79-2 html]# cd ztj
[root@cent79-2 ztj]# ls
index.html
[root@cent79-2 ztj]# cd ../ztj1/
[root@cent79-2 ztj1]# ls 
index.html
[root@cent79-2 ztj1]# 

-->测试nginx.conf文件语法的正确性

命令:

nginx -t

[root@cent79-2 html]# 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

命令:

nginx -s reload

[root@cent79-2 html]# nginx -s reload
[root@cent79-2 html]# 

-->查看端口(8080/8081)

命令:

netstat -antulp |grep 8080

netstat -antulp |grep 8081

[root@cent79-2 conf]# netstat -antulp |grep 8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      4300/nginx: master  
[root@cent79-2 conf]# netstat -antulp |grep 8081
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      4300/nginx: master  
[root@cent79-2 conf]# 

-->验证:

->http://ip:8080

->http://ip:8081

至此,Ngnix基于端口的多虚拟主机配置也就结束了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值