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

2019-10-17 17:14:06王冬梅

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

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

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、最后测试时发生的问题

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

解决方法:

查看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、配置虚拟主机文件时要加上监听的IP地址,每个虚拟主机配置文件都一样。

listen  192.168.2.155:80;

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。