标签:nginx 配置虚拟主机
安装nginx请参考,nginx编译安装的博文
1:配置nginx虚拟主机,同一个端口80,多个不同的域名。nginx默认主配置文件内容如下
[root@zxl-nginx conf]# cat nginx.conf user nginx; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } include ./conf.d/*.conf; //此行添加是方面配置引用多个虚拟主机的配置文件。 }
2:需求使用同一个端口80,不同域名访问、
访问www.zxl.com 内容显示为www.zxl.com 访问www.bbs.com内容显示为www.bbs.com
在nginx主配置文件目录下创建子目录方便引用
[root@zxl-nginx conf]# mkdir conf.d
zxl.com.conf配置文件内容如下:
[root@zxl-nginx conf.d]# cat zxl.com.conf server { listen 80; server_name www.zxl.com zxl.com; location / { root /data/zxl; index index.html index.htm; access_log logs/zxl.access.log; error_log logs/zxl.error.log; } }
bbs.com.conf配置文件内容如下:
[root@zxl-nginx conf.d]# cat bbs.com.conf server { listen 80; server_name www.bbs.com bbs.com; location / { root /data/bbs; index index.html index.htm; access_log logs/bbs.access.log; error_log logs/bbs.error.log; } }
创建www.zxl.com和www.bbs.com访问目录文件
[root@zxl-nginx ~]# ls -ld /data/{zxl,bbs} drwxr-xr-x 2 root root 4096 Dec 18 10:08 /data/bbs drwxr-xr-x 2 root root 4096 Dec 18 12:36 /data/zxl
bbs和zxl目录下文件相应的内容如下:
[root@zxl-nginx ~]# cat /data/zxl/index.html <h1> This is a site www.zxl.com test! </h1> [root@zxl-nginx ~]# cat /data/bbs/index.html <h1> This is a site www.bbs.com test! </h1>
检查nginx语法查看是nginx相关配置文件是否有问题,nginx检查配置文件语法大法好!
出现ok以及is successful是正确的
[root@zxl-nginx ~]# /usr/local/nginx/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@zxl-nginx ~]# /usr/local/nginx/sbin/nginx [root@zxl-nginx ~]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 3476 root 10u IPv4 15998 0t0 TCP *:http (LISTEN) nginx 3477 nginx 10u IPv4 15998 0t0 TCP *:http (LISTEN)
配置hosts文件
[root@zxl-nginx ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1 www.zxl.com zxl.com 127.0.0.1 www.bbs.com bbs.com
测试访问www.zxl.com和www.bbs.com结果如下:
[root@zxl-nginx ~]# elinks http://www.zxl.com --dump This is a site www.zxl.com test! [root@zxl-nginx ~]# elinks http://www.bbs.com --dump This is a site www.bbs.com test!
本文出自 “流鼻涕” 博客,请务必保留此出处http://noodle.blog.51cto.com/2925423/1726415
标签:nginx 配置虚拟主机
原文地址:http://noodle.blog.51cto.com/2925423/1726415