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基于端口的多虚拟主机配置也就结束了。