您的位置:首页 > 运维架构 > Nginx

nginx基于域名的虚拟主机实战配置

2016-06-26 14:38 771 查看
实验环境:
操作系统:CentOS release 6.8 (Final)
Web服务器:nginx-1.10.1
工具:VMware Workstation 10.0.1 build-1379776
实战任务:配置nginx.conf文件
本节内容在生产场景中是最常用到的,因此,系统工程师、运维工程师、Linux运维等专业技术人员要优先并且熟练掌握。
# mkdir /data0/www/{www,bbs,blog} -p #在www目录下分别建立三个网站目录,注意“/{”该之间不能有空格
[root@localhost www]# for n in www blog bbs;do echo "$n">/data0/www/$n/index.html;done #将www,blog,bbs分别写入三个目录中的index.html文件中;
[root@localhost /]# tree /data0/www #显示树状目录结构
[root@localhost /]# chown -R nginx.nginx /data0/www #授权
[root@localhost conf]# echo www >/data0/www/www/index.html
#将www写入index.html文件中。
[root@localhost conf]# /application/nginx/sbin/nginx -t #检查语法
nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful
[root@localhost conf]# /application/nginx/sbin/nginx -s reload #平滑重启
[root@localhost conf]# lsof -i :80 #检查80端号,开启了8个进程
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1644 root 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3304 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3305 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3306 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3307 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3308 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3309 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3310 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)
nginx 3311 nginx 6u IPv4 11420 0t0 TCP *:http (LISTEN)

配置hosts文件测试
如果域名没有做正式DNS解析,我们可以在我位的笔记本电脑上编辑hosts文件,添加如下内容在本地进行host解析。
Host文件的通用路径为:
%systemroot%\system32\drivers\etc\hosts #开始——》运行
#host文件一般比喻为本地的DNS文件,其功能是把指定域名解析成对应的IP,多个域名可以对应一个IP,默认情况下hosts文件中的配置解析优先于DNS服务器。公司里做开发测试等环节会普遍应用这个host文件,简单而方便。
如果经常使用该文件,可以在桌面或任务栏建立个hosts快捷方式,省得每次找起来费劲,同时也有小软件或插件,可以实现帮你快速修改。
需要加入的域名和你配置的机器的对应解析:
192.168.222.135 www.51cto.com
192.168.222.135 bbs.51cto.com blog.51cto.com

在nginx.conf文件配置:
#将下面的#去掉,注意main类型,与错误日志文件类型一致
user nginx nginx;
worker_processes 8; #设置了同时8个进程任务
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server{
listen 80;
server_name www.51cto.com 51cto.com #域名
location / {
root /data0/www/www; #站点目录
index index.html index.htm;
access_log /app/logs/www_access.log main;
}
}
###
server{
listen 80;
server_name bbs.51cto.com
location / {
root /data0/www/bbs;
index index.html index.htm;
access_log /app/logs/bbs_access.log main;
}
}
###
server{
listen 80;
server_name blog.51cto.comm
location / {
root /data0/www/blog;
index index.html index.htm;
access_log /app/logs/blog_access.log main;
}
}
[root@localhost ~]# echo www.51cto.com > /data0/www/www/index.html
[root@localhost ~]# echo bbs.51cto.com > /data0/www/bbs/index.html
[root@localhost ~]# echo blog.51cto.com > /data0/www/blog/index.html
[root@localhost conf]# ../sbin/nginx –t #检查语法
nginx: [warn] server name "http://sky9896.blog.51cto.com/" has suspicious symbols in /application/nginx-1.10.1/conf/nginx.conf:83
nginx: [warn] server name "http://sky9896.blog.51cto.com/" has suspicious symbols in /application/nginx-1.10.1/conf/nginx.conf:97
#在nginx.conf配置域名中:server_name http://sky9896.blog.51cto.com/;
不需要添加http,只要改成:server_name sky9896.blog.51cto.com;即可
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful
[root@localhost sbin]# ./nginx -s reload #平滑重启
测试页面:








[root@localhost log]# pwd
/app/log
[root@localhost log]# ll #下面是对应的三个站点目录日志文件
总用量 12
-rw-r--r-- 1 root root 2708 6月 26 14:31 bbs_access.log
-rw-r--r-- 1 root root 3358 6月 26 14:32 blog_access.log
-rw-r--r-- 1 root root 848 6月 26 14:31 www_access.log
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息