配置基于域名解析的虚拟主机
1.准备站点
我们站点统一放到/www/vhosts/下,每个站点根目录名称都和域名相同,具体如下。
新建www.stu31.com的站点根目录
[root@www extra]# mkdir /www/vhosts/www.stu31.com
新建www网站的首页index.html
[root@www extra]# echo "Welconf to www.stu31.com" > /www/vhosts/www.stu31.com/index.html
新建bbs.stu31.com站点根目录
[root@www extra]# mkdir /www/vhosts/bbs.stu31.com
新建bbs网站首页index.html,内容如Welconf to bbs.stu31.com
[root@www extra]# echo "Welconf to bbs.stu31.com" > /www/vhosts/bbs.stu31.com/index.html
新建日志文件目录
# mkdir -p /var/logs/nginx
我们统一将日志存放到/var/logs下,这边是存放nginx日志,所以nginx日志保持在当前的nginx目录下.日志统一存放相对来说比较规范(如果你不习惯,你可以按自己的方式来做)
2.配置nginx虚拟主机
增加nginx主配置文件nginx.conf
先配置nginx日志格式,在nginx.conf找到如下内容,并且将#注释标志去掉
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format main '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
添加虚拟主机配置文件:
先将server段注释掉;
include extra/nginx-vhost.conf;
3.配置虚拟主机配置文件:
[root@www ~]# vim /etc/nginx/extra/nginx-vhost.conf
server {
listen 80;
server_name www.stu31.com;
index index.html index.htm;
root /www/vhosts/www.stu31.com;
access_log /var/log/nginx/www.stu31.com-access.log main;
location / {
}
}
server {
listen 80;
server_name bbs.stu31.com;
index index.html index.htm;
root /www/vhosts/bbs.stu31.com;
access_log /var/log/nginx/bbs.stu31.com-access.log main;
location / {
}
}
配置讲解
server{}:配置虚拟主机必须有这个段。
server_name:虚拟主机的域名,可以写多个域名,类似于别名,比如说你可以配置成server_name www.stu31.com web.stu31.com .这样的话,访问任何一个域名,内容都是一样的
listen 80,监听ip和端口,这边仅仅只有端口,表示当前服务器所有ip的80端口,如果只想监听127.0.0.1的80,写法如下:
listen 127.0.0.1:80
root /www/vhosts/www.stu31.com:站点根目录,你网站文件存放的地方。注:站点目录和域名尽量一样,养成一个好习惯
access_log /var/log/nginx/www.stu31.com-access.log main:访问日志
location /{} 默认uri
4.重启nginx服务并打开站点
nginx -t 检查nginx配置是否ok,命令如下:
[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果看到以上两行ok和successful就表示配置问题,那接下来我们启动nginx
启动nginx
[root@www ~]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
访问www.stu31.com,bbs.stu31.com(我这边DNS已经解析到了172.16.31.40,在测试的情况下,我们可以通过版本hosts即可),绑定host方法如下:
讲如下内容增加到C:\Windows\System32\Drivers\etc\hosts
172.16.31.40 www.stu31.com
172.16.31.40 bbs.stu31.com
以上是windows绑定hosts方式,如下是linux方式
[root@www ~]# cat /etc/hosts
127.0.0.1 www.stu31.com localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.0.1 server.magelinux.com server
172.16.31.40 www.stu31.com
172.16.31.40 bbs.stu31.com
使用浏览器访问这两个站点。我这边使用curl来访问。
[root@www ~]# curl http://www.stu31.com
Welconf to www.stu31.com
[root@www ~]# curl http://bbs.stu31.com
Welconf to bbs.stu31.com
转载于:https://blog.51cto.com/sohudrgon/1596948